Results 1 to 11 of 11
  1. #1
    Pbear88 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2017
    Posts
    26

    Most recent date query


    I’m trying to design a query that returns the most recent date for a given criteria. I’ve tried using the Max variable in the Group by section in the query, but that does not work. I think I might need to use the DMax function, but I’m not sure on the proper syntax for the query I’m trying to run.

    There are two tables, “PatientsInfo” and “NutritionInfo”. They are linked by PatientID(one-to-many). I’m trying to return the Lastname, Firstname (from the “PatientsInfo” table), and the most recent DateNutr, and VitDlevel, where VitDlevel is >0 and <20.1. DateNutr and VitDlevel are from the “NutritionInfo” table.

    Expected outcome would look something like:

    Doe|John|6/12/18|14.5
    Smith|Jane|3/7/18|18.0

    But I’m getting:

    Doe|John|6/12/18|17.0
    Doe|John|2/14/18|14.5
    Smith|Jane|3/7/18|18.0
    Smith|Jane|12/17/17|18.0
    Smith|Jane|3/14/17|12.6

    Any thoughts or suggestions?
    Thank you!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    you can only use 2 fields, name,Max(date)
    (otherwise you get the max of 3 fields)

    so:
    Q1 get the max date
    select name,Max(date) from table

    then Q2, get all the data using Q1:
    select * from table ,Q1 where Q1.name = table.name



  3. #3
    Pbear88 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2017
    Posts
    26
    Thanks for the quick reply. The Max date in the table for a person might not have the correct criteria for the VitDLevel. It’s not recorded every time a new record is entered.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,931
    The Max date in the table for a person might not have the correct criteria for the VitDLevel
    modify Q1 to

    Q1 get the max date for a given range
    select name,Max(date) from table where VitDlevel is >0 and <20.1

  5. #5
    Pbear88 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2017
    Posts
    26
    It's still not working. the max date in the table might not be the max date with the correct VitDlevel criteria. So once I try to add the specific criteria to the query, I get multiple results. If I can only use 2 fields for the Max function, I think I need to tackle this from another angle.

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,931
    Are you responding to post #2 or #4?

  7. #7
    Pbear88 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2017
    Posts
    26
    All of the above. Tried them all and no luck

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,931
    Provide the sql you used for post#4 together with some example data used in that query that illustrates the problem

  9. #9
    Pbear88 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2017
    Posts
    26
    SELECT NutritionInfo.PatientID, Max(NutritionInfo.NutriDate) AS MaxOfNutriDate
    FROM NutritionInfo
    GROUP BY NutritionInfo.PatientID, NutritionInfo.VitDlevel
    HAVING (((NutritionInfo.VitDlevel)>1 And (NutritionInfo.VitDlevel)<20.1));

    That gives multiple entries of a PatientID where I'm expecting the most recent date for the given VitDlevel criteria:

    PatientID MaxOfNutriDate
    8900001 10/8/2018
    8900001 11/13/2018
    8900002 11/5/2018
    8900002 1/2/2019

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,931
    this

    GROUP BY NutritionInfo.PatientID, NutritionInfo.VitDlevel
    HAVING (((NutritionInfo.VitDlevel)>1 And (NutritionInfo.VitDlevel)<20.1));


    should be this
    WHERE (((NutritionInfo.VitDlevel)>1 And (NutritionInfo.VitDlevel)<20.1))
    GROUP BY NutritionInfo.PatientID

  11. #11
    Pbear88 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2017
    Posts
    26
    That did it! Thank you so much, Ajax.

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

Similar Threads

  1. Query for most recent date
    By benjamin.m.winchester in forum Queries
    Replies: 3
    Last Post: 12-22-2015, 01:33 PM
  2. Replies: 3
    Last Post: 03-11-2014, 07:32 PM
  3. Most recent Date query
    By SmartestIdiot in forum Queries
    Replies: 1
    Last Post: 01-11-2014, 07:56 AM
  4. Query Help Returning Most Recent Date's Value
    By cperry88 in forum Queries
    Replies: 1
    Last Post: 01-08-2014, 03:03 PM
  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