Jenish Maharjan Jenish Maharjan - 2 months ago 5
SQL Question

id in desceding S.N in ascending

I want my id to be in descending order but want my S.No to be in ascending order.Im new in this.

select ROW_NUMBER() OVER(order BY id) [S.N], id,itemname,unit,price
from items
ORDER BY id desc , [S.N]


Picture of the database

Answer

Just add desc to your window function:

SELECT
    ROW_NUMBER() OVER(order BY  id desc) [S.N], 
    id, 
    itemname,
    unit,
    price 
from items 
ORDER BY id desc
Comments