Raed Alsaleh Raed Alsaleh - 3 months ago 9
SQL Question

Add value to row number in SQL server 2012

I want to add specific value to the row number but this statement doesn't work :

SELECT (ROW_NUMBER()+1) OVER(ORDER BY col_1 value DESC) as Row FROM table


what is the correct syntax to do it ?

Answer

You have to put the addition after the window function, but before the alias.

SELECT ROW_NUMBER() OVER (ORDER BY col_1, value DESC) + 1 as Row   FROM table

... or, you can put it at the beginning if it seems clearer:

SELECT 1 + ROW_NUMBER() OVER (ORDER BY col_1, value DESC) as Row   FROM table

I also added a comma between col1 and value. I assume those are 2 different columns, and that it was a typo. Just mentioning for completeness.