Saturday, August 3, 2013

How to use RowNumber in SQL Query?

Some time we need to display auto row number in select query. For that ROW_NUMBER() function will help you to display auto row number in the output result. The below query will help you to display row number in select query.


select row_number()Over(Order BY Value ) as RankNumber,userid  from MyRankTable


How to get Nth value from the sql table?

Some time we need to get top 4th rank from the table or top 5th person in the table. For that the below query will help you to get result dynamically. Based on the parameter passing the result will display.



declare @Position int
set @Position=2

select RankNumber,userid  
from  (
select row_number()Over(Order BY Value ) as RankNumber,userid 
from MyRankTable 
) MyResult
where RankNumber =@Position


No comments: