• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

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
0
jfmckenzie
Asked:
jfmckenzie
  • 2
1 Solution
 
Anthony PerkinsCommented:
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now