Skip to content

Instantly share code, notes, and snippets.

@adrian-enspired
Last active Jan 24, 2020
Embed
What would you like to do?

given

create table foo (a int, b int);
insert into foo values (1,1),(1,2),(2,1),(2,2),(2,3);

I want to order first by a=1 DESC and second by b, ASC if a=1 and DESC if a<>1.

for example,

select a,b from foo order by a=1 DESC, b ASC;

produces

a b
1 1
1 2
2 1
2 2
2 3

but I'm trying for

a b
1 1
1 2
2 3
2 2
2 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment