Results 1 to 8 of 8
  1. #1
    timPM is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2015
    Posts
    10

    Eliminate duplicate query record by using most recent date

    So I'm an Access beginner, and I'm taking a college class in Database Management. I've completed my assignment however I wanted to explore and take it a bit further.

    I have a basic query that returns results for patients in a certain city. However because I want to include doctor information in the query as well, duplicate records are showing up because patients can have more than one visit.

    In my query I just want to eliminate the duplicate visit records and show the most recent visit only. So for example I found this question posted on another forum at stackoverflow, however the answers were all using SQL code and I haven't been introduced to it yet so I was confused. I was wondering if there was a way to do this without using SQL code, if not can someone explain what the code would do?

    Sorry it's a bit out of alignment ...



    |--ID--|-Type-|-Code-|--Date--|
    | 1 | A | 11 |11/07/13|
    | 2 | A | 11 |11/07/13|
    | 2 | B | 12 |10/07/13| <-- don't want this record from ID=2 as
    | 3 | A | 11 |11/07/13| there is a more recent date for it
    | 4 | A | 10 |11/07/13| than 10/07/2013
    | 5 | A | 11 |11/07/13|

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It sounds like you want something like this:

    http://www.baldyweb.com/LastValue.htm

    All queries are "SQL code"; the design grid you're probably familiar with is just a graphical user interface that allows you to create it. The first query in my link is a totals query; add the ID and date fields to a query, click on the "E" in the ribbon to make it a totals query, and change the Group By to Max for the date field.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    what happens if you get two records with the same date? Or can't that happen?

    Code:
    |--ID--|-Type-|-Code-|--Date--|
    |   1    |   A    |   11   |11/07/13|
    |   1    |   B    |   12   |11/07/13|

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by Ajax View Post
    what happens if you get two records with the same date? Or can't that happen?
    Is that directed to me or the OP? If me and it was my db, the date field would contain both date and time to avoid the resulting problems.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    Sorry - the OP

  6. #6
    timPM is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2015
    Posts
    10
    Yay! Doing those two queries worked from your link pbaldy. I created the first query to find the most recent visit date for each patient in a specific city. Then created a second with my first query as well as all the other tables I wanted fields from, joined all the matching fields together, however I did run into the problem that Ajax brought up! I have a patient who visited twice in the same day, so it shows a duplicate record for him. Is the only way around this to add another piece of information such as time?

  7. #7
    timPM is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2015
    Posts
    10
    Nevermind! Once I made the second query a totals query, the duplicate record merged into one because the records were identical! After getting some insight from you guys and walking myself through it it all is starting to make sense now, thank you guys again for the help!

  8. #8
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help and welcome to the site by the way!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 6
    Last Post: 01-08-2018, 12:39 PM
  2. Eliminate Duplicate (mirrored) Results
    By fauowls in forum Access
    Replies: 3
    Last Post: 03-21-2013, 03:39 PM
  3. Replies: 3
    Last Post: 03-01-2013, 12:41 PM
  4. Get Recent Record without Id or date
    By pyog in forum Queries
    Replies: 5
    Last Post: 04-19-2011, 09:43 AM
  5. Replies: 4
    Last Post: 05-04-2010, 03:33 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