Results 1 to 5 of 5
  1. #1
    wyatt is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2012
    Posts
    3

    Crazy DISTINCT with left outer join problem

    I have the following query in MSACCESS. I need a list of my items combined with items that are in different warehouses.

    Code:
    select * from items left outer join itemswarehouse on items.partnum=itemswarehouse.partnum
    This works fine but gives duplicate items with the exception of a quantity column. I only care to display just one item per row. Ideally I am looking for this kind of code (MSACCESS doesnt support this kind of thing) because I only care about the first record.

    Code:
    select DISTINCT(items.partnum) * from items left outer join itemswarehouse on items.partnum=itemswarehouse.partnum
    My items table is about 25 fields but my itemswarehouse is only 3 fields and only contains rows if items exist in a different warehouse (hence outer join)



    Any ideas how to do a DISTINCT on items.partnum only using the above query?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Don't understand what you want. If you want only a list of partnum values why join tables? If you want the data from Items table to display with the data from ItemsWarehouse table why do you want DISTINCT?

    Maybe you need to build a report with Grouping and Sorting.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    wyatt is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2012
    Posts
    3
    I agree it looks weird. I didnt want to get into explaining. but it's because of arggggh Crystal Reports. The report I have needs the columns from itemswarehouse to be present for other reasons.

    I think the only solution is to aggregate every single column using either first() or min() or other aggregate functions which would be a pain in the neck since I have 28 columns at least. I thought maybe there is a simpler way of doing it with sub selects and joins.

    Here is what it will look like strung out with all the columns.

    Code:
    SELECT first( items.partnum), first(items.desc), sum(itemswarehouse.onhand_qty), etc etc rest of columns 
    FROM items 
    LEFT JOIN itemswarehouse ON items.partnum=itemswarehouse.partnum
    GROUP BY items.partnum

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Prompts question - Why are you compelled to use CrystalReports?

    Grouping by partnum means partnum does not need First function. How would using aggregate function on other fields (other the Sum of quantity) provide meaningful output? Do you just need the fieldnames and the data is ignored?

    The aggregate query looks like easiest way to get the result you want.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    wyatt is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2012
    Posts
    3
    Quote Originally Posted by June7 View Post
    Prompts question - Why are you compelled to use CrystalReports?

    Grouping by partnum means partnum does not need First function. How would using aggregate function on other fields (other the Sum of quantity) provide meaningful output? Do you just need the fieldnames and the data is ignored?

    The aggregate query looks like easiest way to get the result you want.
    Access is just my database file. I don't program with it. This is a legacy app I have to deal with including CR.

    I bit the bullet and used the aggregate query.

    Thanks for your help.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 02-02-2011, 01:00 PM
  2. Left join problem
    By e.badin in forum Queries
    Replies: 5
    Last Post: 01-17-2011, 08:03 AM
  3. Outer left join leaves out null values
    By mschles4695 in forum Queries
    Replies: 1
    Last Post: 12-22-2010, 11:43 PM
  4. Problem with Left Join & VB6
    By msrdjan1 in forum Queries
    Replies: 0
    Last Post: 03-30-2010, 01:48 AM
  5. Dynamic Query Outer Join Problem
    By mjack003 in forum Queries
    Replies: 0
    Last Post: 07-21-2006, 01:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums