如何在sql server中使用ROW_NUMBER()函数以相反的顺序生成序列号

时间:2017-02-17 07:20:35

标签: sql-server sql-server-2008-r2

我正在使用SQL在SQL Server 2008中生成序列号

SELECT  ROW_NUMBER() OVER ( ORDER BY Column ) AS SrNo

以上SQL将按升序生成SrNo

  

1 2 3 4 5

但我想按降序生成它。

  

5 4 3 2 1

2 个答案:

答案 0 :(得分:2)

您需要对ORDER BY列DESC进行排序

SELECT SrNo FROM
(SELECT ROW_NUMBER() OVER (ORDER BY YOUR_COLUMN ) AS SrNo FROM YOUR_TABLE) X
ORDER BY SrNo DESC

答案 1 :(得分:1)

select * FROM (

  SELECT  ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS SNO, ColumnName FROM table

 )r

 ORDER BY SNO Desc