CASE inside ORDER BY with multiple columns

I want to conditionally order a query

ORDER BY 
CASE
WHEN TRUE THEN users.name, users.lastname END

but it raises syntax error syntax error near or at ",".

Examle below works like a charm

ORDER BY 
CASE
WHEN TRUE THEN users.name END

I've also tried

ORDER BY 
CASE
WHEN TRUE THEN "users.name, users.lastname" END

which raises "users.name, users.lastname" does not exist

    ORDER BY 
CASE
WHEN TRUE THEN "users"."name", "users"."lastname" END

this raises syntax error at ","

how to put multiple columns inside CASE inside ORDER BY?


Solution 1:

You need to use ROW constructor in order to compose few columns together

ORDER BY 
CASE
WHEN TRUE THEN ROW(users.name, users.lastname) END

if there is more than 1 value inside ROW() you can skip ROW keyword. Here is shorter version with syntactic sugar:

ORDER BY 
CASE
WHEN TRUE THEN (users.name, users.lastname) END

Solution 2:

Just use two case expressions:

ORDER BY (CASE WHEN TRUE THEN users.name END),
         (CASE WHEN TRUE THEN users.lastname END)