Skip to content

Instantly share code, notes, and snippets.

@adrian-enspired
Last active January 24, 2020 17:13
Show Gist options
  • Save adrian-enspired/b0bf6e0d17adef81c5b871739c870440 to your computer and use it in GitHub Desktop.
Save adrian-enspired/b0bf6e0d17adef81c5b871739c870440 to your computer and use it in GitHub Desktop.

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