Skip to content

Instantly share code, notes, and snippets.

@adrian-enspired
Last active January 24, 2020 17:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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