一个表的Postgresql多重计数
从表中的两列中,我希望获得这些列中值的统一计数。 例如,两列为: 表:报告一个表的Postgresql多重计数,sql,postgresql,count,group-by,Sql,Postgresql,Count,Group By,从表中的两列中,我希望获得这些列中值的统一计数。 例如,两列为: 表:报告 | type | place | ----------------------------------------- | one | home | | two | school | | three | work | | four | cafe | | five
| 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 |
如果我这样做:
从报告中选择类型、计数(*)
按类型分组
我得到:
| type | count |
-----------------------------
| one | 4 |
| two | 2 |
| three | 2 |
| four | 1 |
| five | 2 |
| six | 1 |
我试图得到这样的结果:(最右边的一列和我的类型分组在一起,多个列和每个位置的计数值)
我得到:
这将是对每个这样的地方进行上述计数的结果:
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
postgresql是否可以实现这一点
提前感谢。在这种情况下,您可以使用
案例
-
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
它应该能解决你的问题
@S T穆罕默德,
要获得这样的类型,我们只需在外部查询中使用use
aftergroup
或where
条件,如下所示-
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
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
您还可以使用以下更短的语法:
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
这将起作用,因为当满足条件时,布尔值
true
被转换为1
。您也可以使用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
要获得空(null)列而不是零,请将总和包装在
nullif
calls中。对失败案例使用count
和null
而不是0
(完全省略ELSE 0
)也可以,您不必更改为sum
。sql语句中的最后一个逗号(就在“from”关键字之前)会导致错误。如何获取家庭、学校、工作、咖啡馆、朋友的记录,mall values>0对此延迟回复表示歉意。通过在查询中做一些小的更改,您可以得到您想要的输出。例如(‘home’、‘school’…)我有可能只在运行时知道的位置查找。有什么样的sql模板吗?
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