tags:

views:

42

answers:

3

is this possible using T-SQL?

A: 

One way is to do something like the following...

SELECT * FROM (
    SELECT TOP x * FROM (
        SELECT TOP y fields
        FROM table
        WHERE conditions
        ORDER BY table.field  ASC) as foo
    ORDER by field DESC) as bar
ORDER by field ASC

x is the number of rows you want returned and y is x+offset.

http://josephlindsay.com/archives/2005/05/27/paging-results-in-ms-sql-server/

JoshVarga
thats complicated :X
Alan
If you post code or XML, **please** highlight those lines in the text editor and click on the "code" button (101 010) on the editor toolbar to nicely format and syntax highlight it!
marc_s
+1  A: 

If you're using SQL Server 2005 or greater, check out the ROW_NUMBER function: http://msdn.microsoft.com/en-us/library/ms186734.aspx

joelt
Yes!Thanks a lot!
Alan
A: 

Hey, by the asnwer of joelt about row_number(). I did it. Its like this:

SELECT allianceId, position, points from (select ROW_NUMBER() over (Order by Points DESC) as position, points, allianceId from Alliance) as somethingx where position >= @alliancePosition - 5 and position <= @alliancePosition + 5;

Alan