all groups > sql server programming > october 2007 >
You're in the

sql server programming

group:

Select most recent date, single ID from a list a ID numbers (with duplicates) and dates?



RE: Select most recent date, single ID from a list a ID numbers (with ML
10/29/2007 11:48:03 AM
sql server programming: Here's a guess (since you haven't posted any DDL):

select ID
,max(DateOf) as DateOf
from <table>
group by ID


ML

---
Matija Lah, SQL Server MVP
Re: Select most recent date, single ID from a list a ID numbers (with duplicates) and dates? Lord Kelvan
10/29/2007 11:50:37 AM
ok if the date is unique since id isnt unique this should work fine

select *
from table
where date in (
select max(date)
from table
group by id)

i tested it on one of my talbes that does the exact thing it is a modd
of a query i used to get the latest thing works fine as far as i know

regards kelvan
Re: Select most recent date, single ID from a list a ID numbers (with duplicates) and dates? Russell Fields
10/29/2007 2:49:06 PM
Mark,

SELECT ID, MAX(DateOf) as DateOf
FROM MyTable
GROUP BY ID
ORDER BY ID

RLF

[quoted text, click to view]

Select most recent date, single ID from a list a ID numbers (with duplicates) and dates? mark4asp
10/29/2007 6:35:10 PM
Select most recent date, single ID from a list a ID numbers (with
duplicates) and dates?

Suppose I had a table with this data:

ID DateOf

499 2004-03-05
499 2006-11-13
500 2003-09-09
1529 2005-01-10
1529 2004-03-25
1529 2004-08-31
1529 2006-10-27
1742 2004-05-06
1742 2006-10-03
1743 1900-01-01
2017 2005-10-10
2295 2004-08-25
2295 2005-05-17
2524 2007-10-15
2524 2007-09-19
2524 2006-10-20
2770 2005-12-06

What query on the above table will give me the result set below?

ID DateOf

499 2006-11-13
500 2003-09-09
1529 2006-10-27
1742 2006-10-03
1743 1900-01-01
2017 2005-10-10
2295 2005-05-17
2524 2007-10-15
AddThis Social Bookmark Button