I am trying to do a conditional ORDER BY
in SQL but I am getting an error: column "ended" does not exist
pointing to the 'case when "ended"' part of the query.
Here is my query:
SELECT (EXTRACT(EPOCH FROM SUBMISSION_DEADLINE)
-EXTRACT(EPOCH FROM now())) AS "datediff"
, (EXTRACT(EPOCH FROM SUBMISSION_DEADLINE)>EXTRACT(EPOCH FROM now())) AS "ended", *
FROM imdown_api_v1_contest
ORDER BY "ended" DESC, case when "ended" = true then "datediff" else "-datediff" END ASC;
I need to grab the contests that haven't ended first and order those by the one ending soonest first. Then I need all the ones that have ended and order those by the ones that ended most recently.