How to determine position of row in sql result-set?

前端 未结 5 837
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 23:08

i have a sql query:

select id, name from table order by name

result looks like this:

52 arnold 
33 berta 
34 chris 
47 dori         


        
5条回答
  •  盖世英雄少女心
    2020-12-18 23:40

    The answer depends on which version of SQL you are using.

    If you are using MSSQL 2005 you can use the the new analytical function ROW_NUMBER () which generates the sequential number in order which we define in order by clause. The syntax of ROW_NUMBER () function is:

    ROW_NUMBER () OVER (ORDER BY )

    or

    ROW_NUMBER () OVER (PARTITION BY )

    If you are using an older version of SQL Server you can create a temp table with an identity column and select the results from that.

提交回复
热议问题