Tuesday, August 16, 2011

SELECT THE LATEST DATE RECORDS

Table REFILLED:
PatientID Date_refilled
3 7/6/2011
3 7/6/2011
3 2/15/2011
1 8/11/2011
1 8/2/2011
1 8//7/2011

Question: How to select the all latest date records of a patient

Output should be :
PatientID Date_refilled

1 8/11/2011
3 7/6/2011
3 7/6/2011


Here is the select command :

SELECT PatientID, date_refilled
FROM REFILLED AS t
WHERE (date_refilled =
(SELECT MAX(date_refilled) AS Expr1
FROM REFILLED
WHERE (PatientID = t.PatientID)))
ORDER BY PatientID