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 surgery date. 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
GROUP BY,MIN(). - Gordon LinoffSELECTquery that you use to get the data that you've posted? Include all of yourJOINs. - gr1zzly be4r22-Nov-10the most recentInvestigationDatewould be2011-02-07but from the data you pasted it looks like it should be2005-01-22. Are you sure that yourJOINis correct? - gr1zzly be4r