Forum Moderators: coopster

Message Too Old, No Replies

SQL Query for all but three rows in database

SQL Query

         

Jose52

10:38 pm on Feb 28, 2006 (gmt 0)

10+ Year Member



How can I do an sql query where I retrived all the rows in a table except the last 3 (or whatever number) entered.

coopster

1:09 am on Mar 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I suppose you could run a COUNT(*) aggregate function query first, then use the returned value to subtract 3 to know the LIMIT (assuming the database you are using has some form of LIMIT clause).

LifeinAsia

7:27 pm on Mar 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Clunky version- something like:

SET @MaxRows = (SELECT COUNT(*) FROM Whatever) - 3;

SELECT TOP @MaxRows FROM Whatever;

The specifics of the code will depend on your DB.

Jose52

7:16 am on Mar 4, 2006 (gmt 0)

10+ Year Member



Thank you LifeinAsia. I will try that. It makes sense on paper.