SQL to output line number in results of a query

后端 未结 5 479
忘了有多久
忘了有多久 2021-01-07 17:38

I would like to generate a line number for each line in the results of a sql query. How is it possible to do that?

Example: In the request

select di         


        
5条回答
  •  隐瞒了意图╮
    2021-01-07 18:23

    You can use the ROW_NUMBER function for this. Check the syntax for this here http://msdn.microsoft.com/en-us/library/ms186734.aspx

    SELECT FirstName, LastName, ROW_NUMBER() OVER(ORDER BY FirstName) AS 'Row#'    
    FROM Sales.vSalesPerson;
    

    For your query,

    SELECT client_name, ROW_NUMBER() Over (Order By client_name) AS row_number 
    FROM   (select distinct client_name from deliveries) SQ
    

    will work.

提交回复
热议问题