0

I am using SQL to generate Serial number in SQL server 2008

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

Above SQL will generate SrNo in the ascending order

1 2 3 4 5

but I want to generate it in descending order.

5 4 3 2 1

2 Answers 2

2

You need to sort the ORDER BY column DESC

SELECT SrNo FROM
(SELECT ROW_NUMBER() OVER (ORDER BY YOUR_COLUMN ) AS SrNo FROM YOUR_TABLE) X
ORDER BY SrNo DESC
Sign up to request clarification or add additional context in comments.

2 Comments

Isn't any idea without using sub query?
Mostly No I guess !! Please anybody correct me if I am wrong (by providing example)
1
select * FROM (

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

 )r

 ORDER BY SNO Desc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.