Sort by value - ORDER BY

I am trying to sort a query using ORDER BY, where I need to sort 3 columns one by one. And the third column matters.

For example: if I have 3 columns a, b and c I need to use ORDER BY a, b desc, c = '3' asc, c

What I want to do is the first sort order is a, then b in desc order, then I want the values ​​that have 3 to be sorted, and then the rest of the values ​​that are not 3.

+4
source share
3 answers
 ORDER BY
  a,
  b desc,
  CASE WHEN c = 3 THEN 0 ELSE 1 END,
  c
+9
source

Assuming SQL Server, try using CASE:

ORDER BY a, b desc, CASE c WHEN '3' THEN 0 ELSE 1 END, c ASC
+1
source
ORDER BY
a,
b desc,
CASE c when 3 then 1 else 0 end
0

All Articles