Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Limiting the found set
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00527254
Message ID:
00527624
Views:
32
>>> What if you wanted records 50 - 75 of a 100 record set >>>

Here's an idea that doesn't use temp tables. A little convoluted, admittedly. This statement would give you scores 51-75 ("next 25"):

SELECT * FROM
(SELECT TOP 25 fScore, cId
FROM
(SELECT TOP 75 fScore, cId
FROM TESTS
ORDER BY fUnits) TopMax
ORDER BY fScores DESC) TopMin
ORDER BY fUnits

This looks quite complicated, which might make you think it's quite lengthy. But keep in mind that the two outermost Selects are processing a very small set of data. In fact, in my tests against a table of 600,000 records, with no index on the fScore field, there were no recorded differences timewise between this entire statement and the simple "TOP 75" inner Select.

Notes: The inner SELECT grabs the top 75 records by score. The next SELECT grabs the last 25 of those 75. (We could stop here, but they would pre presented upside down -- that is, you'd really be gettiing 75-51.) The final loop inverts this last subset to set the order right again.

Unfortunately, "TOP @MinRecords" doesn't work -- the TOP statements must include an actual number and not a local variable. But, you could create a stored procedure that accepts the desired number, construct a query statement, and use EXECUTE to fire it off.

HTH.
The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform