Wednesday, March 7, 2012

Maximum Date in a group of records

hi all,
i got is table:
Id StartDate EndDate
a 19/03/2001 18/03/2002
a 19/03/2002 18/04/2002*
b 13/08/2000 12/08/2001
b 13/08/2001 12/08/2002
b 13/08/2002 10/07/2002*

Sort command and groupins i am ok but i need to select only the records that
has the latest enddate. (See *)
any ideas? thanks in advance
rashidThis query returns row(s) with the latest Enddate for each ID but the row
for ID='B' isn't the one you highlighted.

SELECT id, startdate, enddate
FROM SomeTable AS S
WHERE enddate =
(SELECT MAX(enddate)
FROM SomeTable
WHERE id = S.id)

Hope this helps.

--
David Portas
SQL Server MVP
--

No comments:

Post a Comment