SQL Query To Get The Row Number

  1. -- Using Row_Number  
  2. SELECT ROW_NUMBER() OVER(ORDER BY UserName DESCAS Row  
  3. ,UserName  
  4. ,LoweredUserName  
  5. --,ROUND(SalesYTD,2,1) AS "Sales YTD"  
  6. FROM aspnet_Users  
  7. WHERE IsAnonymous IS NOT NULL  
  8.    
  9. -- Using CTE  
  10. WITH ServiceOrders AS  
  11. (  
  12. SELECT ROW_NUMBER() OVER (ORDER BY CreatedOn) AS RowNumber  
  13. ,ServiceOrderID  
  14. ,DueDate  
  15. FROM ServiceOrder  
  16. )  
  17. SELECT ServiceOrderID, DueDate, RowNumber  
  18. FROM ServiceOrders  
  19. WHERE RowNumber BETWEEN 10 AND 20;  
  20.    
  21. -- Using Partition  
  22.    
  23. SELECT ROW_NUMBER() OVER(PARTITION BY SvcReqID ORDER BY ServiceTypeDesc DESCAS Row  
  24. ,ServiceOrderID  
  25. ,DueDate  
  26. ,SvcReqID  
  27. FROM ServiceOrder  
  28. WHERE SvcReqCancellationTypeID IS NOT NULL  
  29. ORDER BY ServiceTypeDesc