Results 1 to 5 of 5
  1. #1
    cowboy is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2010
    Posts
    291

    Query a unique record based off of 2 criteria

    I have records that have tracking numbers and multiple records can have the same tracking number. I am trying to query to only have 1 of each record shown. I have a sub query that is a totals query and is set to group by the tracking number and provide the Max of a due date as shown below.


    Click image for larger version. 

Name:	sq_MostRecentDate.png 
Views:	10 
Size:	8.8 KB 
ID:	38514

    Then I have a query to inner join on the tracking number and max of due date to choose only those records from the filter query above, but add in all the other fields from the parent project as shown below.

    Click image for larger version. 

Name:	q_OneSubEach.png 
Views:	10 
Size:	13.6 KB 
ID:	38515

    And that works pretty well until there are multiple records with the same due date, then multiple records get queried as show below.

    Click image for larger version. 

Name:	q_OneSubEach-data.png 
Views:	10 
Size:	20.1 KB 
ID:	38516

    How do I filter by a second criteria to make sure I am only returning one record for each? I think my second criteria will be the larger of the sID, which is the auto number key for the submissions.
    (One thing that I thought of while previewing this post is to run another total query on the results above and group by pID and then Max of sID and then run another inner join query to pull back in the extra fields from the project.)

    Any help would be appreciated.

    Here is a simplified version of the database if that is helpful:
    unique records.zip

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Go ahead and try that.

    Also, review http://allenbrowne.com/subquery-01.html#TopN
    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
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I have a sub query
    What you show is a stacked query (where a query uses another query as if it were a table). A subquery is something else as you may note from the posted link.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    cowboy is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2010
    Posts
    291
    Using that link on the Subqueries, I think I was able to fumble together what I want and with just the 1 query with subquery and no the stacked queries. Thank you for the help. This is what I ended up with:

    Code:
    SELECT q_AllSub.*FROM q_AllSub
    WHERE q_AllSub.sID IN
    
    
    (SELECT TOP 1 sID
    FROM q_AllSub AS Dupe
    WHERE Dupe.pID = q_AllSub.pID
    ORDER BY Dupe.Due DESC, Dupe.sID DESC)
    
    
    ORDER BY q_AllSub.Due;

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Glad you were able to solve it. Good luck with your db.

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

Similar Threads

  1. Replies: 1
    Last Post: 10-03-2018, 03:24 AM
  2. Replies: 5
    Last Post: 09-05-2016, 09:07 AM
  3. Replies: 2
    Last Post: 10-20-2014, 03:09 PM
  4. Query Criteria: Unique Number
    By efleming in forum Access
    Replies: 1
    Last Post: 05-24-2011, 03:16 PM
  5. lost - linking date field based on a record with smaller Unique ID
    By stan.chernov@gmail.com in forum Queries
    Replies: 7
    Last Post: 09-16-2010, 02:22 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