Junnel
Junnel

Reputation: 107

SQL order by 0 first and then descend number/values

I have a weird situation where i need to order 0 first and then descends a column.

Let's say i have column that looks like this

Status
------
3
4
0
5
1
2
4
0
2

And Now i need to order it by

Status
------
0
0
5
4
4
3
2
2
1

Is this possible for SQL? I've been trying to test for 2 days now but i am stuck.

Upvotes: 2

Views: 7062

Answers (1)

jarlh
jarlh

Reputation: 44796

Use a case expression to first sort 0's and then the rest. After that sort by status desc:

order by case when Status = 0 then 0 else 1 end, status desc

Upvotes: 14

Related Questions