Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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?
Couple of CASE expressions would do this:
CASE
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
Add a comment
Required, but never shown
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.
Explore related questions
See similar questions with these tags.