I currently have this SQL:
select ROW_NUMBER() OVER (order by priority DESC NULLS LAST,
created_at DESC) as index
, id
, title
, ago(created_at)
, priority
, user_id
from post
order by priority DESC NULLS LAST
, created_at DESC;
As you can see, I need to have ROW_NUMBER()
and that needs the
(order by priority desc nulls last, created_at desc) as index
This is causing me to repeat the same long
order by priority desc nulls last, created_at desc
twice.
If I need to have even more columns in the order by, that will make it even longer.
Is there a way to prevent this repetition? I tried using an alias after the ORDER BY
but that doesn't seem to be supported.
Just order by the first column:
SELECT ROW_NUMBER() OVER (ORDER BY priority DESC NULLS LAST, created_at DESC) AS index
, id
, title
, ago(created_at)
, priority
, user_id
FROM post
ORDER BY 1 ASC; -- 1 = first column
See also the manual