I have my ORDER BY
clause as following:
...
ORDER BY CASE WHEN boolean_column is true THEN text_column END ASC,
CASE WHEN boolean_column is false THEN text_column END DESC
Is it somehow possible to replace the second CASE
in an ELSE
? It feels odd to have two conditions instead of a regular if else/when else
as you normally would do.
You can use this trick to shorten the logic:
ORDER BY (CASE WHEN boolean_column THEN text_column END) ASC,
text_column DESC
It is still two order by
keys though.
If the type of text_column is numeric. you can try this.
ORDER BY CASE WHEN boolean_column is true THEN text_column
ELSE -1 * text_column END ASC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With