Results 1 to 3 of 3
  1. #1
    cardgage is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jan 2011
    Posts
    50

    Query help - pulling field associated with max value from another field

    Good morning everyone,



    I'm sure this question has been asked before, but I was unable to find an answer while searching these forums. I have two tables in my database - one that holds patient data (tblPatient) and another that stored medications for each patient (tblMeds); these are joined by the common field PatientID. Here is the structure of the tables:

    tblPatient tblMeds
    PatientID - AutoNumber MedID - AutoNumber
    LastName - Text PatientID - Number
    FirstName - Text Medication - Lookup
    BirthDate - Date/Time MedDate - Date/Time


    The "Medication" field in tblMeds uses a lookup table to select between 5 different medications. I'm trying to create a query that pulls the "Medication" associated with the Max value in the "MedDate" field for each patient. When I create a query to pull the Max "MedDate" and PatientID, everything works fine. However, once I try to pull in "Medication" into this query I get the Max date for each Medication (i.e., multiple medications for each patient).

    I would like the query to only pull the last medication that was entered for the patient based on the Max of "MedDate" for each "PatientID." Any help with this would be greatly appreciated!

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    This can be done in a series of two queries:

    1. Using tblMeds, create an Aggregate Query that pulls the Max MedDate for each PatientID. This query should only have the PatientID field and MedDate fields. You will be Grouping By PatientID and taking the Maximum of the MedDate field.

    2. Now, create a new query joining the Query you just created above with tblMeds, joining on BOTH the PatientID and MedDate fields. Now, you can return whatever fields you want from tblMeds, and it will only return the records from the Maximum MedDate for each Patient.

  3. #3
    cardgage is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jan 2011
    Posts
    50
    Awesome, thanks...worked like a charm!

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

Similar Threads

  1. Replies: 1
    Last Post: 03-09-2014, 06:30 PM
  2. Replies: 2
    Last Post: 12-31-2013, 03:41 PM
  3. Replies: 3
    Last Post: 04-03-2013, 12:05 PM
  4. pulling field names from table
    By murfeezlaw in forum Queries
    Replies: 6
    Last Post: 11-27-2012, 04:30 PM
  5. Pulling field data from another record's field?
    By PeachTosser25 in forum Access
    Replies: 7
    Last Post: 02-06-2012, 01:14 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