I need a DISTINCT list of records based on one field and a date
I am trying to formulate an SQL statement that will return a list of records that are distinct on one field, but if a duplicate record (based on the one field) is present with a newer date in the date field, then it should be selected as the "distinct" record.
ie.
Field 1 Field 2 Field 3
A 3/28/11 Jimmy
A 4/11/11 Tom
B 3/29/11 Harry
C 4/12/11 Tom
C 3/30/11 Jimmy
Woul开发者_开发知识库d produce:
A 4/11/11 Tom
B 3/29/11 Harry
C 4/12/11 Tom
The "Distinct" field is Field 1.
SELECT A.Field1, A.Field2, A.Field3
FROM myTable A
INNER JOIN (
SELECT B.Field1, MAX(B.Field2) AS MaxDate
FROM myTable B
GROUP BY B.Field1) AS B
ON A.Field1 = B.Field1 AND A.Field2 = B.MaxDate
ORDER BY A.Field1
Note: I haven't tried this query & am writing it of what it should be.
精彩评论