SQL Most Recent Date after Date (1 Viewer)

lucour

Registered User.
Local time
Today, 14:11
Joined
Mar 7, 2001
Messages
60
I am working on a SQL query to extract patient data. I have 3 tables. One contains unique records for patient data (ie: First Name, Last Name, Date of Birth...). The second table is the Surgery table, that captures the date and results that the patient has surgery. The third table is the investigations table, that captures the investigations done after surgery. The key in the Patient table is called HUN, and it links the Surgery and Investigation tables in a 1 to many relationship.

What I need to find out, is what the most recent date of the investigation is after each surgery. One patient can have multiple surgeries, and I need to know the most recent investigation date following each.

Here is my data (Note: this is fictional data). There are 2 surgery dates: May 11, 2005 and November 22, 2010. Within these I need to know the most recent investigation date following the respective surgery dates. The result should be 2 records:

242424 11-May-05 2011-07-19

and...

242424 22-Nov-10 2011-02-07

HUN Surgery Date Investigation Date
242424 11-May-05 2005-01-22
242424 11-May-05 2006-03-29
242424 11-May-05 2007-03-05
242424 11-May-05 2008-04-01
242424 11-May-05 2009-06-04
242424 11-May-05 2009-06-19
242424 11-May-05 2010-05-21
242424 11-May-05 2011-02-07
242424 11-May-05 2011-02-15
242424 11-May-05 2011-07-19
242424 11-May-05 2012-06-12
242424 11-May-05 2012-09-18
242424 11-May-05 2013-04-04
242424 11-May-05 2013-10-30
242424 11-May-05 2014-10-07
242424 11-May-05 2015-09-09
242424 22-Nov-10 2005-01-22
242424 22-Nov-10 2006-03-29
242424 22-Nov-10 2007-03-05
242424 22-Nov-10 2008-04-01
242424 22-Nov-10 2009-06-04
242424 22-Nov-10 2009-06-19
242424 22-Nov-10 2010-05-21
242424 22-Nov-10 2011-02-07
242424 22-Nov-10 2011-02-15
242424 22-Nov-10 2011-07-19
242424 22-Nov-10 2012-06-12
242424 22-Nov-10 2012-09-18
242424 22-Nov-10 2013-04-04
242424 22-Nov-10 2013-10-30
242424 22-Nov-10 2014-10-07
242424 22-Nov-10 2015-09-09
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:11
Joined
May 7, 2009
Messages
19,242
not possible.
surgery table and investigation table can be linked to patient table.
investigation table dates cannot be distinguished which surgery it belongs.
need extra field for this table as to which date surgery has been done.
 

Users who are viewing this thread

Top Bottom