April 24, 2009 at 8:00 am
Hi I need some help in writing the query
ID DATEBEN
19391227200412011
193912272004120123
193912272004120124
[highlight=#ffff11]193912272009070122[/highlight]
193933272004120122
1939332720041201224
193933272004120122
1[highlight=#ffff11]93933272009080122[/highlight]
There are 3 columns and ID ,date and Ben.I want to select only those IDs with recent DATE ...for example in the ID 19391227 the recent date is 20090701.....Help me in writing this logic....
Thanks
Sudheer
April 24, 2009 at 8:17 am
Do you mean you want the most recent date for each ID? If so, then Max(Date) and Group By ID should do what you need.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
April 24, 2009 at 8:20 am
I need rest of the columsn too......which are not same ......so i cannot do group by on those coloumns.......
April 24, 2009 at 8:23 am
;with CTE (Row, ID, MostRecent, Ben) as
(select row_number() over (partition by ID order by Date desc),
ID,
Date,
Ben)
select ID, MostRecent, Ben
from CTE
where Row = 1;
Try that, see if it works.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
April 24, 2009 at 10:08 am
GSquared ... think you left out the FROM table clause
;with CTE (Row, ID, MostRecent, Ben) as
(select row_number() over (partition by ID order by Date desc),
ID,
Date,
Ben FROM yourtablesname)
select ID, MostRecent, Ben
from CTE
where Row = 1;
April 24, 2009 at 11:18 am
Yep. Completely missed that one.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply