I have a conditional query as such:
- If a column (order) is not 0, order by value 1 to 99999 (max)
- I need to order all 0s in order by last name.
So if I have a table as such
ID Order Last Name
1 0 Manner
2 1 Brock
3 0 Lester
4 0 Annual
5 0 Greatly
The results I expect are:
Brock
Annual
Greatly
Lester
Manner
Here is my query. What is happening every time is that I am getting last name sort, without the non-order 0 going first:
select c.last_name
from person_reports crt
join person c
where c.org_id = 1000 and crt.reports_to_id = 100389 and c.id = crt.contact_id
order by c.last_name, case preference_num when 0 then 9999999 else preference_num end
Results of my current work:
Annual
Brock
Greatly
Lester
Manner
Thanks for any help
You can simply reverse the ORDER BY clause to:
And you can avoid using the magic ‘9999999’ if you add one more segment at the beginning: