I would like to know how to use multiple order columnes with case. For example case 1, order by FirstName, GivenName case 2 order by GivenName, FirstName.

I tried this, but it looks like the colon introduce an error

Order by case when 1 then FirstName, GivenName else GivenName, FirstName end


Case is supposed to return one value, not a tuple. Beside, you should compare 1 with something. If you want conditional ordering you can do that with two case statements:

Order by case when x = 1 then FirstName else GivenName end
       , case when x = 1 then GivenName else FirstName end
  • Excelente.... It worked.... All that I needed was to use two separate case statements whitin order by – otrovagomas Nov 4 '15 at 18:14

For multiple conditional ORDER BY statements:

ORDER BY CASE category WHEN 0 THEN concat(cName,', ',bName) END DESC,
         CASE category WHEN 1 THEN concat(aName,', ',bName,', ',cName) END DESC,
         CASE category WHEN 2 THEN concat(bName,', ',aName,', ',cName) END DESC";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.