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
 Transact-SQL (2000)
 SELECTing the 2 most recent rows

Author  Topic 

n_hancock
Starting Member

1 Post

Posted - 2004-05-24 : 21:06:12
I have a table with the primary key OfferID and Sequence_number.
The other fields involved are status, and create_date.

I want to build a SELECT statement to return IDs where, for each OfferID, the status of record max(sequence_number) is 'R', AND the status is 'R' of the record before as well, ie (max(sequence_number)-1).

Eventually this will report on dates within a certain range, but I need to get the SELECT working first.

Any ideas?

Nigel Hancock

srinivasanr
Starting Member

15 Posts

Posted - 2004-05-25 : 02:48:50
Nigel,

Hope this snippet will work out...

SELECT a.OfferID, a.sequence_number
FROM offer a
WHERE a.status = 'R'
GROUP BY a.OfferID, a.sequence_number
HAVING a.sequence_number IN (SELECT TOP 2 b.sequence_number FROM offer b
WHERE a.OfferId = b.OfferID ORDER BY b.sequence_number DESC)

Check Out

Wallops!!!
Go to Top of Page
   

- Advertisement -