-2

I have the table below with the Patientid, the vaccine name that they received, and the date they received it. I am trying to write an SQL query to only show the most recent given date for each patient.

patientId   vaccinename  givenDate
100          Influenza   7/23/2013
100          Influenza   10/14/2014
101          Influenza   11/24/2009
101          Influenza   10/14/2013
101          Influenza   10/22/2014
102          Influenza   10/24/2013
102          Influenza   10/8/2014
Makyen
  • 31,849
  • 12
  • 86
  • 121
MBotros
  • 11
  • 3
  • Note that dates in SQL ascribe to a specific format, and SO doesn't like it when you don't try. – Strawberry Dec 08 '14 at 21:59
  • It's a pretty easy SQL statement. At least try to make something. If it's totally wrong, don't worry about it. If you are sure it's wrong (because you get errors), you never know that maybe you're close to the correct version ! – tvCa Dec 08 '14 at 22:29

1 Answers1

1

Pretty straightforward, just group by the patient and vaccine and return the max date for the most recent.

SELECT patientID, vaccinename, MAX(givenDate)
FROM patient_info
GROUP BY patientID, vaccinename

SQL Fiddle: http://sqlfiddle.com/#!2/c6eee9/1

Michael McGriff
  • 793
  • 10
  • 20