This question already has answers here:
Oracle dynamic DESC and ASC in order by
(2 answers)
Closed 16 days ago.
I want to order query according to input value. If input value is str1 then I want to order by 1 ASC and if it is str2, I want to order it by 2 DESC.
I wrote something like that:
ORDER BY CASE WHEN 'input' = 'str1' THEN 1 ASC WHEN 'input' = 'str2' THEN 2 DESC END
How do I need to modify it to get desired result?
I can do ORDER BY 'input' but it's sorting only in ascending order.
# Answer 1
The Link provided by @user7294900 contains the solution for your question.
Basically, all you have to do is implement the CASE section correctly.
SELECT * FROM your_table
WHERE SOMETHING = 'something'
ORDER BY
CASE WHEN INPUT = 'str1' THEN 1 END,
CASE WHEN INPUT = 'str2' THEN 2 END DESC;