How to get row number in PostgreSQL when the results are ordered by some column?
e.g.
SELECT 30+row_number() AS position, *
FROM users
ORDER BY salary DESC
LIMIT 30
OFFSET 30
I supposed that the query would return list like this:
position | name | salary
31 | Joy | 4500
32 | Katie| 4000
33 | Frank| 3500
Actually i have to duplicate the ORDER
clause into the query to make it functional:
SELECT 30+row_number(ORDER BY salary DESC) AS position, *
FROM users
ORDER BY salary DESC
LIMIT 30
OFFSET 30
Is there any other way how to return ordered and numbered results without necessity of duplicating the code?
I know this can be solved by incrementing some variable in the app itself, but i wanna do this at the database layer and return to the app already numbered results...