Link to home
Start Free TrialLog in
Avatar of jfmckenzie
jfmckenzieFlag for Australia

asked on

How to: Select Last Record for Each Record ID

My current query retrieves the following data

<Current Resultset>

id             LastContactDate

1              10-jan-06
1              11-jan-06
1              12-jan-06
2              10-feb-06
2              11-feb-06
2              12-feb-06

I need to be able to select only the latest date for each record id.

<Required Resultset>

id             LastDateContact

1              12-jan-06
2              12-feb-06


Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi jfmckenzie,

SELECT ID, MAX(LastContactDate)LastContactDate
FRoM urTable
GROUP BY ID


Aneesh R!
Avatar of jfmckenzie

ASKER

Thanks guys. Both comments correct, not sure how to allocate points so just accepted first answer.
>>not sure how to allocate points <<
See here from the EE Help:

More than one Expert helped solve my problem. What do I do?
https://www.experts-exchange.com/Databases/Microsoft_SQL_Server/help.jsp#hi69