Results 1 to 8 of 8
  1. #1
    majahops is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    5

    Query that selects lab results based on date of labs relative to another lab

    I have a pretty simple query that I wish to create, but I just haven't been able to do it. I'm really hoping that somebody can help.

    I'm replacing the names of the tests that I actually want here, for security purposes... but let's suppose this is the scenario.



    I'm analyzing patient's who have had a test called "PROCALCITONIN" performed. I only want to see the result for the first PROCALCITONIN test that a patient had performed. I also want to select the HEMOGLOBIN and GFR that the patient had most recently preceeding their first PROCALCITONIN test.

    To make things easier, all of my information is in a single table. So let's suppose I have the following table with only 1 patient (of course, my actual table has thousands of patients) with the following information:

    The first table is what I am starting with and the second table is the table I'd like to create with a query:

    Patient ID Test Name Test Value Test Date
    1 Procalcitonin 4 1/1/2013
    1 Hemoglobin 11.3 12/30/2012
    1 Procalcitonin 3.6 1/4/2013
    1 GFR 60 1/3/2013
    1 GFR 47 12/30/2012
    1 GFR 58.3 12/26/2012
    1 Hemoglobin 11.8 12/22/2012
    1 Hemoglobin 11.2 1/7/2013
    1 GFR 60 1/2/2013
    1 Hemoglobin 11.8 12/28/2012
    Patient ID Test Name Test Value Test Date
    1 Procalcitonin 4 1/1/2013
    1 GFR 47 12/30/2012
    1 Hemoglobin 11.3 12/30/2012

    Any information that anybody might be able to give me on how to pull this off would mean the world to me! Thanks so much!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Criteria that must consider values of data in other records of same table is never easy and your criteria is especially complicated. Requires a unique ID field - autonumber type will serve. Try:

    Query1: MinDateProcal
    SELECT Table1.[Patient ID], Table1.[Test Name], Min(Table1.[Test Date]) AS [MinOfTest Date]
    FROM Table1
    WHERE (((Table1.[Test Name])="Procalcitonin"))
    GROUP BY Table1.[Patient ID], Table1.[Test Name];

    Query2: TestsProcalMinDate
    SELECT Table1.ID, Table1.[Patient ID], Table1.[Test Name], Table1.[Test Value], Table1.[Test Date], MinDateProcal.[MinOfTest Date]
    FROM MinDateProcal RIGHT JOIN Table1 ON MinDateProcal.[Patient ID] = Table1.[Patient ID]
    WHERE (((Table1.[Test Date])<=[MinOfTest Date]));

    Query3:
    SELECT * FROM TestsProcalMinDate WHERE ID IN (SELECT TOP 1 Dupe.ID FROM TestsProcalMinDate As Dupe WHERE Dupe.[Patient ID]=TestsProcalMinDate.[Patient ID] AND Dupe.[Test Name]=TestsProcalMinDate.[Test Name] AND Dupe.[Test Date]<=TestsProcalMinDate.[MinOfTest Date] Order BY Dupe.[Test Date] DESC);


    I don't see any reason to actually create second table. Just use the final query for whatever you intended to do with the table.
    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.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I agree with June7 based on the data you have shown.
    If you have a lot more data, different patients and different tests, then I would suggest

    3 tables
    Code:
    tblPatient --->tblPatientTest<---tblTest
    Patient Info     PatientTest          list of tests and descriptions
                         PaitientID
                         TestID
                         testDate
                         testValue
    where tblPatienttest is a Junction table

  4. #4
    majahops is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    5
    June7, first off, thank you so much for your response. I did have one problem though. When I tried to create the second query, I got the following error:

    "The specific field 'Table1.ID' could refer to more than one table listed in the FROM clause of your SQL statement"

    Any idea where we may have gone wrong here?

    Thank you again!!!


    Quote Originally Posted by June7 View Post
    Criteria that must consider values of data in other records of same table is never easy and your criteria is especially complicated. Requires a unique ID field - autonumber type will serve. Try:

    Query1: MinDateProcal
    SELECT Table1.[Patient ID], Table1.[Test Name], Min(Table1.[Test Date]) AS [MinOfTest Date]
    FROM Table1
    WHERE (((Table1.[Test Name])="Procalcitonin"))
    GROUP BY Table1.[Patient ID], Table1.[Test Name];

    Query2: TestsProcalMinDate
    SELECT Table1.ID, Table1.[Patient ID], Table1.[Test Name], Table1.[Test Value], Table1.[Test Date], MinDateProcal.[MinOfTest Date]
    FROM MinDateProcal RIGHT JOIN Table1 ON MinDateProcal.[Patient ID] = Table1.[Patient ID]
    WHERE (((Table1.[Test Date])<=[MinOfTest Date]));

    Query3:
    SELECT * FROM TestsProcalMinDate WHERE ID IN (SELECT TOP 1 Dupe.ID FROM TestsProcalMinDate As Dupe WHERE Dupe.[Patient ID]=TestsProcalMinDate.[Patient ID] AND Dupe.[Test Name]=TestsProcalMinDate.[Test Name] AND Dupe.[Test Date]<=TestsProcalMinDate.[MinOfTest Date] Order BY Dupe.[Test Date] DESC);


    I don't see any reason to actually create second table. Just use the final query for whatever you intended to do with the table.

  5. #5
    majahops is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    5
    Nevermind, I hadn't created an autonumber ID that's why that didn't work. So the second query worked. Trying the third query now. Thanks so much again!!!

  6. #6
    majahops is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    5
    June7,

    So the first two queries worked great. In the second query, I only have lab values that preceed (or are equal to) the date for my earliest Procalcitonin. That is SO great. The thing is, when I try to run the third query, the system just hangs and hangs and hangs.

    Any ideas? I'm so close thanks to you!!!

    Quote Originally Posted by June7 View Post
    Criteria that must consider values of data in other records of same table is never easy and your criteria is especially complicated. Requires a unique ID field - autonumber type will serve. Try:

    Query1: MinDateProcal
    SELECT Table1.[Patient ID], Table1.[Test Name], Min(Table1.[Test Date]) AS [MinOfTest Date]
    FROM Table1
    WHERE (((Table1.[Test Name])="Procalcitonin"))
    GROUP BY Table1.[Patient ID], Table1.[Test Name];

    Query2: TestsProcalMinDate
    SELECT Table1.ID, Table1.[Patient ID], Table1.[Test Name], Table1.[Test Value], Table1.[Test Date], MinDateProcal.[MinOfTest Date]
    FROM MinDateProcal RIGHT JOIN Table1 ON MinDateProcal.[Patient ID] = Table1.[Patient ID]
    WHERE (((Table1.[Test Date])<=[MinOfTest Date]));

    Query3:
    SELECT * FROM TestsProcalMinDate WHERE ID IN (SELECT TOP 1 Dupe.ID FROM TestsProcalMinDate As Dupe WHERE Dupe.[Patient ID]=TestsProcalMinDate.[Patient ID] AND Dupe.[Test Name]=TestsProcalMinDate.[Test Name] AND Dupe.[Test Date]<=TestsProcalMinDate.[MinOfTest Date] Order BY Dupe.[Test Date] DESC);


    I don't see any reason to actually create second table. Just use the final query for whatever you intended to do with the table.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    The query works with the sample data you posted. I just deleted all records dated later than the first procalcitronin test and it still works.

    If you want to provide db for analysis, follow instructions at bottom of my post.
    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.

  8. #8
    majahops is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    5
    I really appreciate your help. Your way did work on smaller data sets, but mine was just too large and it hung up on the third query. Clearly yours did work though. I figured out how to do it, but it was a much rougher way than your design. Thank you so much for your help, again. It really does mean the world!!!

    Quote Originally Posted by June7 View Post
    The query works with the sample data you posted. I just deleted all records dated later than the first procalcitronin test and it still works.

    If you want to provide db for analysis, follow instructions at bottom of my post.

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

Similar Threads

  1. Replies: 4
    Last Post: 12-17-2013, 08:31 AM
  2. Replies: 3
    Last Post: 12-14-2013, 06:16 PM
  3. Create Message Box based on query results
    By stanley721 in forum Queries
    Replies: 7
    Last Post: 02-14-2013, 10:04 AM
  4. Replies: 1
    Last Post: 04-23-2012, 10:40 AM
  5. Open form based on query results
    By RobbertH in forum Queries
    Replies: 1
    Last Post: 02-10-2010, 08:13 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