Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
nishanthnair
Starting Member
6 Posts |
Posted - 2009-05-02 : 07:25:30
|
| I have a table like this The LastEvent column is DateTime type.Id EventName LastEvent1 Name1 4/29/091 Name2 4/22/091 Name3 3/29/092 Name12 1/2/092 Name13 4/25/092 Name14 2/29/093 Name32 1/2/083 Name33 3/11/093 Name34 4/29/09Now here, I need a SQL Query to take out the row for each id with the latest event. So the out put will beId EventName LastEvent1 Name1 4/29/092 Name13 4/25/093 Name34 4/29/09As you can see, for each id, these rows are the latest event(MAX(LastEvent)).It will be helpful if any one of you experts can help me with the query to get this output.Thanks,NishanthCheers,Nishanth |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-05-02 : 07:41:03
|
[code]select t1.id, t1.eventName, t1.lastEventfrom table t1join(select id, max(lastEvent) as lastEvent from from table group by id) t2 on t1.id=t2.id and t1.lastEvent=t2.lastEvent[/code]Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
nishanthnair
Starting Member
6 Posts |
Posted - 2009-05-02 : 10:08:23
|
| Thanks a lot webfred. That self join did the trick.Cheers,Nishanth |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-05-02 : 15:36:26
|
| [code]select id,eventname,lasteventfrom(Select *,ROW_NUMBER() Over (Partition by id order by lastevent desc) as Seqfrom table)ZWhere Z.seq =1[/code] |
 |
|
|
nishanthnair
Starting Member
6 Posts |
Posted - 2009-05-03 : 08:03:47
|
| Sodeep,Thanks a lot for your query... That is more helpful as the first query will return all the rows when the lastevent field is the same for all ids.Cheers,Nishanth |
 |
|
|
nishanthnair
Starting Member
6 Posts |
Posted - 2009-05-03 : 10:05:31
|
| I have one more question. In the table I have one more column EventCount. Now in the unique row which we selected I need a SUM(COUNT) of all the other rows with the same id. Can any one of you help me with this query please?Cheers,Nishanth |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-05-03 : 13:33:37
|
[code]declare @Sample table(id int,EventName varchar(255), LastEvent datetime, EventCount int)insert @Sampleselect 1, 'Name1','20090429',1 union allselect 1, 'Name2','20090429',2 union allselect 1, 'Name3','20090329',3 union allselect 2, 'Name12','20090102',12 union allselect 2, 'Name13','20090425',13 union allselect 2, 'Name14','20090228',14 union allselect 3, 'Name32','20080102',32 union allselect 3, 'Name33','20090311',33 union allselect 3, 'Name34','20090429',34--select * from @Sampleselect Z.id, Z.eventname, Z.lastevent, Z2.SumEventCountfrom(Select *, ROW_NUMBER() Over (Partition by id order by lastevent desc) as Seq from @Sample)Zjoin(select id, sum(EventCount) as SumEventCount, ROW_NUMBER() Over (Partition by id order by max(lastevent) desc) as Seq from @Sample group by id)Z2 on Z.id=Z2.idWhere Z.seq =1[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|