Home > DeveloperSection > Forums > How to use ROW_NUMBER in sqlserver
Ankit Singh

Total Post:341

Points:2389
Posted on    November-26-2015 9:59 PM

 Database SQL Server  SQL  SQL Server 2008 
Ratings:


 1 Reply(s)
 330  View(s)
Rate this:
 I want to use ROW_NUMBER in sql server Please  help me.


aditya kumar Patel

Total Post:250

Points:1782
Posted on    November-26-2015 10:07 PM

ROW_NUMBER is a function built-in to SQL Server that will return a row number for each record in your result set. You can further change resulting row number to reset the row number based on some value in the result set.

PARTITION BY – If you supply this parameter, then the row number will reset based on the value changing in the columns supplied. This is kinda like using a GROUP BY.
ORDER BY – This is the specified order of the row number that you would like. If you wanted the order of the row number to increment by an employee name (alphabetically), you do that here.


select ROW_NUMBER() over (ORDER BY emp_name) AS RowNumber,Emp_ID,Emp_Name,Emp_Sal from Employee

select ROW_NUMBER() over (PARTITION BY emp_sal ORDER BY emp_name) AS RowNumber,Emp_ID,Emp_Name,Emp_Sal from Employee


Don't want to miss updates? Please click the below button!

Follow MindStick