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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Query Help

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-07-19 : 21:13:04
Kwang writes "Hi! First, thanks for the great site! I have found many great tips and appreciate what you are doing. (<--not just ass kissing so you'll help me with my problem...)

Anyway, I've searched through your site and I coundn't come up with anything that might solve my problem.

I have this table:


eventID eventDate
------- ---------
1 6/1/2001
1 6/2/2001
1 6/3/2001
1 6/4/2001
1 6/5/2001
3 6/1/2001
3 6/2/2001
3 6/3/2001
3 6/4/2001
4 6/1/2001
4 6/2/2001
4 6/3/2001


What I need to do is return a recordset which contains the 2 latest records for each eventID. So the resulting recordset would be like


eventID eventDate
------- ---------
1 6/4/2001
1 6/5/2001
3 6/3/2001
3 6/4/2001
4 6/2/2001
4 6/3/2001


I have hammered together a solution by using a cursor to step through each distinct eventID and inserting into a copy of the table the top 2 records for each eventID.

I was hoping there would be a better way to do this. Any help will be greatly appreciated.

I am using Windows 2000 and SQL 2000 with all the latest updates.

Thanks!

Kwang Kim"
   

- Advertisement -