SqlExceptionHelper : ERROR: function count(character varying, integer) does not exist

前端 未结 1 1820
感动是毒
感动是毒 2021-01-23 03:59

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\'          


        
相关标签:
1条回答
  • 2021-01-23 04:44

    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;   
    
    0 讨论(0)
提交回复
热议问题