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
jfmckenzieAsked:
Who is Participating?
 
Anthony PerkinsConnect With a Mentor Commented:
You need to requery the database with something like this:

Select id, MAX(LastContactDate)
From YourTableName
Group by id
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
Hi jfmckenzie,

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


Aneesh R!
0
 
jfmckenzieAuthor Commented:
Thanks guys. Both comments correct, not sure how to allocate points so just accepted first answer.
0
 
Anthony PerkinsCommented:
>>not sure how to allocate points <<
See here from the EE Help:

More than one Expert helped solve my problem. What do I do?
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/help.jsp#hi69
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.