@Espo thanks to reality check - added sub-selection to fix this.
I think the simplest answer is:
select userid, points from users where points in (select distinct top N points from users order by points desc)
If you want to put this in a saved process that takes N as a parameter, then you will either need to read the SQL in the variable, or execute it, or do the count trick:
declare @SQL nvarchar(2000) set @SQL = "select userID, points from users " set @SQL = @SQL + " where points in (select distinct top " + @N set @SQL = @SQL + " points from users order by points desc)" execute @SQL
or
SELECT UserID, Points FROM (SELECT ROW_NUMBER() OVER (ORDER BY points DESC) AS Row, UserID, Points FROM Users) AS usersWithPoints WHERE Row between 0 and @N
Strike> Both examples assume SQL Server and have not been tested.
Rob allen
source share