Postgresql一个表的多个计数

Chu*_*ydb 23 sql postgresql group-by count

从我表中的两列开始,我想获得这些列中值的统一计数.例如,两列是:

表:报告

|   type        |   place   |  
 ----------------------------------------- 
|   one         |   home    |  
|   two         |   school  |  
|   three       |   work    |  
|   four        |   cafe    |  
|   five        |   friends |  
|   six         |   mall    |  
|   one         |   work    |  
|   one         |   work    |  
|   three       |   work    |  
|   two         |   cafe    |  
|   five        |   cafe    |  
|   one         |   home    |  
Run Code Online (Sandbox Code Playgroud)

如果我这样做:SELECT类型,按类型从报告组计数(*)

我明白了:

|   type        |   count   |  
-----------------------------  
|   one         |   4       |  
|   two         |   2       |  
|   three       |   2       |  
|   four        |   1       |  
|   five        |   2       |  
|   six         |   1       | 
Run Code Online (Sandbox Code Playgroud)

我试图得到这样的东西:(一个最右边的列与我的类型组合在一起,多个列与每个地方的计数值)我得到:

|   type        |   home    |   school  |   work    |   cafe    |   friends |   mall    |  
-----------------------------------------------------------------------------------------  
|   one         |   2       |           |   2       |           |           |           |  
|   two         |           |   1       |           |   1       |           |           |  
|   three       |           |           |   2       |           |           |           |  
|   four        |           |           |           |   1       |           |           |  
|   five        |           |           |           |   1       |   1       |           |  
|   six         |           |           |           |           |           |   1       |  
Run Code Online (Sandbox Code Playgroud)

对于像这样的每个地方运行如上所述的计数的结果:

SELECT type, count(*) from reports where place  = 'home'
group by type
SELECT type, count(*) from reports where place  = 'school'
group by type
SELECT type, count(*) from reports where place  = 'work'
group by type
SELECT type, count(*) from reports where place  = 'cafe'
group by type
SELECT type, count(*) from reports where place  = 'friends'
group by type
SELECT type, count(*) from reports where place  = 'mall'
group by type
Run Code Online (Sandbox Code Playgroud)

这有可能与postgresql?

提前致谢.

pra*_*arg 49

你可以case在这种情况下使用-

SELECT type, 
       sum(case when place  = 'home' then 1 else 0 end) as Home,
       sum(case when  place  = 'school' then 1 else 0 end) as school,
       sum(case when  place  = 'work' then 1 else 0 end) as work,
       sum(case when  place  = 'cafe' then 1 else 0 end) as cafe,
       sum(case when  place  = 'friends' then 1 else 0 end) as friends,
       sum(case when  place  = 'mall' then 1 else 0 end) as mall
  from reports
 group by type
Run Code Online (Sandbox Code Playgroud)

它应该解决你的问题

@ST Mohammed,要获得这样的类型,我们可以简单地在外部查询中使用usingafter groupwherecondition,如下所示 -

select type, Home, school, work, cafe, friends, mall from (
SELECT type, 
       sum(case when place  = 'home' then 1 else 0 end) as Home,
       sum(case when  place  = 'school' then 1 else 0 end) as school,
       sum(case when  place  = 'work' then 1 else 0 end) as work,
       sum(case when  place  = 'cafe' then 1 else 0 end) as cafe,
       sum(case when  place  = 'friends' then 1 else 0 end) as friends,
       sum(case when  place  = 'mall' then 1 else 0 end) as mall
  from reports
 group by type
 )
 where home >0 and School >0 and Work >0 and cafe>0 and friends>0 and mall>0
Run Code Online (Sandbox Code Playgroud)


Tom*_*eif 18

回答praktik garg是正确的,没有必要使用else 0:

SELECT type, 
       sum(case when place  = 'home' then 1 end) as home,
       sum(case when  place  = 'school' then 1 end) as school,
       sum(case when  place  = 'work' then 1 end) as work,
       sum(case when  place  = 'cafe' then 1 end) as cafe,
       sum(case when  place  = 'friends' then 1 end) as friends,
       sum(case when  place  = 'mall' then 1 end) as mall
 from reports
 group by type
Run Code Online (Sandbox Code Playgroud)

您还可以使用以下更短的语法:

SELECT type, 
       sum((place  = 'home')::int) as home,
       sum((place  = 'school')::int) as school,
       sum((place  = 'work' )::int) as work,
       sum((place  = 'cafe' )::int) as cafe,
       sum((place  = 'friends')::int) as friends,
       sum((place  = 'mall')::int) as mall
 from reports
 group by type
Run Code Online (Sandbox Code Playgroud)

这将起作用,因为当满足条件时,布尔值true被转换为1.


小智 11

您也可以使用filter子句:

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
Run Code Online (Sandbox Code Playgroud)


小智 8

您可以使用filtercount像这样:

select type,
       count(1) FILTER (where place = 'home') as home,
       count(1) FILTER (where place = 'school') as school,
       count(1) FILTER (where place = 'work') as work,
       count(1) FILTER (where place = 'cafe') as cafe,
       count(1) FILTER (where place = 'friends') as friends,
       count(1) FILTER (where place = 'mall') as mall
from reports
group by type
Run Code Online (Sandbox Code Playgroud)