0

Having IDs in range (-12 to 700, no 0).

I want to output a table sorted by IDs with IDs 1 to 700 coming first and then from -1 to -12.

Avoiding dealing with unions and splitting the sets by >0 and <0, is there a way to sort it like I want?

1 Answer 1

5

Couple of CASE expressions would do this:

ORDER BY CASE WHEN YourColumn > 0 THEN 1 ELSE 2 END ASC,
         CASE WHEN YourColumn > 0 THEN YourColumn END ASC,
         YourColumn DESC; --As already inferred it is now less than 0

DB<>Fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.