Results 1 to 14 of 14
  1. #1
    India62 is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    8

    How to fetch latest records from table

    I have table with date and different lab values for each patient.
    Some lab values repeats every month, some every quarter and some semiannually.
    I need query to have all values in one row for each patient with only latest or last values.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It's difficult to read that. Are you looking for the first or both of these:

    http://www.baldyweb.com/LastValue.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    India62 is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    8
    Please see attached what i am looking for

  4. #4
    India62 is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    8
    Sorry please see attached

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I think you want 3 of the first query, for A, B & C. Then the second query would join them all.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    India62 is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    8
    Thanks
    But not sure what it means?
    You mean I should have four queries and then join them ?
    Please explain little more

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Can you post a sample database with actual data (nothing personal of course)?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    India62 is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    8
    Here it is.
    there 3 Patients ID
    When I run query, I need for each patiet ID last result (latest) from each field
    There are more fields in actual database
    Thanks for help

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I tried to open your db but Access says it is in inconsistent state and tries to recover it but fails. Do Compact & Repair and replace the upload.

    Does each lab test have its own field? That would not be fully normalized data structure but does make this type of output easy. I suspect you should be able to do a Totals query with grouping on the patient ID. Use the Max function in the Totals row for each of the data fields.
    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.

  10. #10
    India62 is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    8
    I don’t know it opens on my computer
    Attached is in table structure in excel format.
    What I need is when I run query all yellow highlighted value should appear for that particular patient ID
    Hope it helps to explain
    Thanks

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I misunderstood that you want the maximum value of each lab test. That would have been easy. Getting the latest lab result is complicated because the lab results are not all on same date. I used DFirst function, like:
    SELECT DISTINCT Lab.ID, DFirst("[Dry Weight]","Lab","Not IsNull([Dry Weight]) And ID='" & [ID] & "'") AS DW, DFirst("[KG]","Lab","Not IsNull([KG]) And ID='" & [ID] & "'") AS KG
    FROM Lab
    ORDER BY Lab.ID;

    I normally avoid DFirst and DLast (also First and Last) because requires records to be sorted (in this case by descending date order) and can't rely on that records have been entered into table in the correct order nor that the order will be maintained. However, your data sample shows the correct order. If this does not reflect the actual situation, will have to do an intermediate query to perform the correct sort and then use that query as source for my suggestion.

    EDIT: I did try to use DLookup first but it was late last night and wasn't seeing it but think after a good night's sleep might have it.
    SELECT Lab.ID, DLookUp("[Dry Weight]","Lab","[Date]=#" & DMax("[Date]","Lab","ID='" & [ID] & "' And Not IsNull([Dry Weight])") & "#") AS LastDW FROM Lab;

    As you can see, either method will mean a very long query after you build the expression for each lab test.
    Last edited by June7; 05-18-2011 at 11:31 AM.
    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.

  12. #12
    India62 is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    8
    Thanks
    I will try and will let you know. But can we sort date with another update query every time before we run this quey ?
    I may not able to work on this today or tommorow but will let you know by Friday.

  13. #13
    India62 is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    8
    I just tried in my sample database and it works ( yes I do have to find way to sort data before running this query). But you are right it will be long query.
    Thanks any way this was very useful.
    I will update as SLOVED once done with final check

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    As I said, build a query that does the sort and save it. Then use that query as source for the DFirst query. You mention update query - why, what needs to be updated?
    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.

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

Similar Threads

  1. How do you get the latest date?
    By radicrains in forum Queries
    Replies: 6
    Last Post: 10-27-2010, 08:59 PM
  2. Replies: 18
    Last Post: 08-22-2010, 05:22 AM
  3. Replies: 6
    Last Post: 04-18-2010, 03:41 AM
  4. Need Query to fetch last row in a table
    By gunapriyan in forum Queries
    Replies: 10
    Last Post: 02-20-2010, 12:46 AM
  5. Latest Date Records
    By Rick West in forum Queries
    Replies: 1
    Last Post: 09-25-2009, 11:16 AM

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