Results 1 to 9 of 9
  1. #1
    inverted is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    7

    Finding the earliest date and then get the relevant value of another column

    I want to make a query that returns the earliest lab result(which is a row in the table with the earliest date), the serial # and the value for test id1,2 and 3.

    Sample data would look like:


    This means that a patient could have many rows (because he can take many labs on diff dates) but there is a unique identifier for every lab result on a different date.
    Thus for the same patient on the same date could be only one row.
    For a patient could be many rows with diff dates.

    PAT_SERIAL_NO LAB_DATA_DATE_ID LAB_DATE TEST_ID_1 TEST_ID_2
    123345 11223332423123222 17/4/1990 45.3 23.2
    123345 33242345634675743 21/7/2001 20.1 11.1
    543232 43546547567487686 17/4/1990 45.3

    So for this sample data the required output would be:

    PAT_SERIAL_NO LAB_DATA_DATE_ID LAB_DATE TEST_ID_1 TEST_ID_2
    123345 11223332423123222 17/4/1990 45.3 23.2
    543232 43546547567487686
    17/4/1990

    45.3


    I attached the table for further info.
    Click image for larger version. 

Name:	sdf g.JPG 
Views:	18 
Size:	104.8 KB 
ID:	31810

  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
    This may help:

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

    but your various test fields look like a normalization mistake:

    Fundamentals of Relational Database Design -- r937.com
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    inverted is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    7
    Quote Originally Posted by pbaldy View Post
    This may help:

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

    but your various test fields look like a normalization mistake:

    Fundamentals of Relational Database Design -- r937.com
    Thank you very much.
    It works great.
    Now, in case I also want to add the condition of making sure that this min_lab_test im getting doesn't have a null value on TEST_ID_1.
    In another words, I want to get the earliest date where the value for TEST_ID_1 is not Null.

  4. #4
    inverted is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    7
    Thank you very much.
    It works great.
    Now, in case I also want to add the condition of making sure that this min_lab_test im getting doesn't have a null value on TEST_ID_1.
    In another words, I want to get the earliest date where the value for TEST_ID_1 is not Null.

  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
    You can add that criteria to the first query, but I want to reiterate that the design is not normalized.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    inverted is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    7
    Quote Originally Posted by pbaldy View Post
    You can add that criteria to the first query, but I want to reiterate that the design is not normalized.
    Yeah I agree with you.
    How would you redesign the table? How would you separate it?

  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
    Did you read the second link in post 2? In my view, your test ID's are no different than invoice line items, so should be records in a related table.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    inverted is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    7
    Yes thank you very much, helped me a lot.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem, glad it helped.
    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: 2
    Last Post: 08-31-2016, 09:29 AM
  2. Replies: 4
    Last Post: 02-25-2016, 08:18 AM
  3. select the 2 earliest date records for each id
    By lbrannon in forum Queries
    Replies: 10
    Last Post: 07-14-2015, 04:10 PM
  4. Default earliest date and latest
    By Compufreak in forum Access
    Replies: 3
    Last Post: 01-10-2013, 07:17 AM
  5. Selecting Earliest and latest date
    By kstyles in forum Queries
    Replies: 10
    Last Post: 12-31-2010, 03:04 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