-- Using Row_Number
SELECT ROW_NUMBER() OVER(ORDER BY UserName DESC) AS Row
,UserName
,LoweredUserName
--,ROUND(SalesYTD,2,1) AS "Sales YTD"
FROM aspnet_Users
WHERE IsAnonymous IS NOT NULL
-- Using CTE
WITH ServiceOrders AS
(
SELECT ROW_NUMBER() OVER (ORDER BY CreatedOn) AS RowNumber
,ServiceOrderID
,DueDate
FROM ServiceOrder
)
SELECT ServiceOrderID, DueDate, RowNumber
FROM ServiceOrders
WHERE RowNumber BETWEEN 10 AND 20;
-- Using Partition
SELECT ROW_NUMBER() OVER(PARTITION BY SvcReqID ORDER BY ServiceTypeDesc DESC) AS Row
,ServiceOrderID
,DueDate
,SvcReqID
FROM ServiceOrder
WHERE SvcReqCancellationTypeID IS NOT NULL
ORDER BY ServiceTypeDescDifferent ways to get the row number from table in SQL Server.
