Results 1 to 4 of 4
  1. #1
    madrock is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    2

    help with SQL syntax for duplicates

    Hello!



    I have three columns: UNIQID, ID, and QUANTITY.

    I'd like to write a query such that where there is a duplicate in ID, choose the UNIQID (or the entire row) that corresponds to the minimum QUANTITY for that duplicate.

    For example, if:

    UNIQID ID QUANTITY
    1 123 100
    2 123 50
    3 444 100
    4 654 75
    5 654 100

    I'd like the final result to be:

    UNIQID ID QUANTITY
    2 123 50
    3 444 100
    4 654 75

    Any thoughts? I've tried Totals Queries and Duplicate Queries, both of which get me part of the way there, but I can't get the final result I'm looking for.

    thank you.

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Welcome to the forum!

    You would first use an aggregate query to get the Min quantity for each ID


    query name: qryMinQty
    SELECT ID, Min(Quantity) as MinOfQty
    FROM tablename
    GROUP BY ID

    Then create another query that joins the original table (I call it tablename below) to the aggregate query by both the ID and quantity

    SELECT tablename.UNIQID, qryMinQty.ID, qryMinQty.MinOfQuantity
    FROM tablename INNER JOIN qryMinQty ON (tablename.Quantity = qryMinQty.MinOfQuantity) AND (tablename.ID = qryMinQty.ID);

  3. #3
    madrock is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    2

    great!

    jzwp11 - thank you so much, it works like a charm. I also see now how I could alternatively do it without directly inputting SQL code:

    1. Run a Totals Query choosing Group By for the ID and Min for QUANTITY.
    2. Run a Select Query with the original table and the Totals Query with inner joins between IDs and QUANTITIES.

    I had been trying to include my UNIQID in my aggregate/totals query, which is where I was messing up. I really appreciate your help.

  4. #4
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Glad that worked for you; good luck!

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

Similar Threads

  1. Getting rid of duplicates
    By anemoskkk in forum Access
    Replies: 1
    Last Post: 04-17-2011, 11:24 AM
  2. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10:47 AM
  3. duplicates
    By Roberta in forum Queries
    Replies: 5
    Last Post: 09-10-2010, 07:31 PM
  4. Duplicates in Query
    By Dega in forum Queries
    Replies: 1
    Last Post: 05-02-2010, 05:09 PM
  5. combo box - no duplicates
    By pkg206 in forum Access
    Replies: 1
    Last Post: 11-12-2009, 10:41 AM

Tags for this Thread

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