Friday, March 23, 2012

Question on Top N clause

Top N clause retrieve the first N rows of the table. How can I retrieve the second N rows? Generally , is there any solution to retrieve the nth N rows?Daniel Anderson has an article at 4GuysFromRolla which contains a general solution:Paging through Records using a Stored Procedure.

You could also do it non-generically and unattractively and probably inefficiently like this (will select records 11-20):

SELECT TOP 10 * FROM
(SELECTTOP 10 * FROM
(SELECT TOP 20 * FROM division ORDER BY div_code ASC ) AS myTop20
ORDER BY div_code DESC) AS myTop10Desc
ORDER BY div_code ASC

Terri|||Thank you very much.|||tmorton.. that's just sexy!

No comments:

Post a Comment