I have a spring application and I have a native query with this syntax:
select
COUNT(DISTINCT person.id,(CASE WHEN salary_person.rating = \'Satisfactory\'
Postgres does not support count()
with more than one column. You can however simply turn both columns into a single column of an anonymous record type, by using something like: (col_one, col_two)
- that is a single column of an an anonymous record type.
select COUNT(DISTINCT (person.id,(CASE WHEN salary_person.rating = 'Satisfactory' THEN 1 END))) AS totalSatisfactory,
COUNT(DISTINCT (person.id,(CASE WHEN salary_person.rating = 'Unsatisfactory' THEN 1 END))) AS totalUnsatisfactory
from person
join salary_person on person.id = salary_person.person_id;
Note the parentheses around the two columns.
However, in Postgres you have a more elegant way to do what you want, by using conditional aggregation with the filter
clause:
select COUNT(DISTINCT person.id) filter (where salary_person.rating = 'Satisfactory') AS totalSatisfactory,
COUNT(DISTINCT person.id) filter (where salary_person.rating = 'Unsatisfactory') AS totalUnsatisfactory
from person
join salary_person on person.id = salary_person.person_id;