I've learned how to use SELECT DISTINCT
with ORDER BY
with the help of the question :
How to use DISTINCT and ORDER BY in same SELECT statement?
And I've learned to use ORDER BY CASE
from this question:
How do I return rows with a specific value first?
But I have a query that is hard to write:
I have a table CUSTOMER
with a column NAME
, I want to get all the distinct names of the customers, and I want to find name 'Hamza' first.
SELECT DISTINCT "CUSTOMER"."NAME",
Max("CUSTOMER"."NAME")
FROM "CUSTOMER"
WHERE
"CUSTOMER".id >= 2
GROUP BY "CUSTOMER"."NAME"
ORDER BY CASE WHEN ("CUSTOMER"."NAME" = 'HAMZA' ) THEN 1 ELSE 2 END
The ERROR:
ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 15: ORDER BY CASE WHEN ("CUSTOMER"."NAME" = 'HAMZA' ) THEN 1 EL...
_________________.^
SQL state: 42P10
Character: 372
Edit
It's solved with:
SELECT "CUSTOMER"."NAME", -- distinct keyword removed here
Max("CUSTOMER"."NAME")
FROM "CUSTOMER"
WHERE
"CUSTOMER".id >= 2
GROUP BY "CUSTOMER"."NAME"
ORDER BY
CASE WHEN ("CUSTOMER"."NAME" = 'HAMZA' ) THEN 1 ELSE 2 END,
CASE WHEN ("CUSTOMER"."NAME" = 'GIORNO' ) THEN 1 ELSE 2 END