Postgresql Multiple counts for one table

前端 未结 3 1908
夕颜
夕颜 2021-01-30 10:56

From two columns in my table I want to get a unified count for the values in these columns. As an example, two columns are:

Table: reports

|   type           


        
3条回答
  •  梦如初夏
    2021-01-30 11:46

    You can use filter clause as well:

    SELECT
      type,
      sum(1) FILTER (WHERE place = 'home') AS home,
      sum(1) FILTER (WHERE place = 'school') AS school,
      sum(1) FILTER (WHERE place = 'work') AS work,
      sum(1) FILTER (WHERE place = 'cafe') AS cafe,
      sum(1) FILTER (WHERE place = 'friends') AS friends,
      sum(1) FILTER (WHERE place = 'mall') AS mall
    FROM
      reports
    GROUP BY 
      type
    

提交回复
热议问题