在 PostgreSQL 上创建统计分类变量的列的最简单方法是什么?也许某种旋转?
What's the easiest way of creating a column counting a categorical variable on PostgreSQL? Maybe some sort of pivoting?
假设我有以下 table:
Id color
A00 blue
A00 blue
A99 red
A99 blue
A95 yellow
A97 green
我想要这样的东西:
Id blue red yellow green
A00 2 0 0 0
A99 1 1 0 0
A95 0 0 1 0
A97 0 0 0 1
最简单的方法是什么?
我想过这个:
select Id,
sum(case when color='blue' then 1 else 0 end) as blue,
sum(case when color='red' then 1 else 0 end) as red,
.
.
.
from table
问题是我有太多的颜色,这样做会很累。有没有更简单的方法?
有很多方法可以实现:
使用过滤器
select
id,
count(*) filter (where color='blue') as "Blue",
count(*) filter (where color='red') as "Red",
count(*) filter (where color='yellow') as "Yellow",
count(*) filter (where color='green') as "Green"
from samp
group by id
你的方法
select
id,
sum(case when color='blue' then 1 else 0 end) as "Blue",
sum(case when color='red' then 1 else 0 end) as "Red",
sum(case when color='yellow' then 1 else 0 end) as "Yellow",
sum(case when color='green' then 1 else 0 end) as "Green"
from samp
group by id
使用交叉表
select * from crosstab(
'select id, color,count(*) from samp group by id,color order by id,color',
'select distinct color from samp order by color'
)
as ct("ID" varchar, "blue" int,"green" int,"red" int,"yellow" int);
注意:您必须使用以下查询为交叉表创建扩展
CREATE EXTENSION IF NOT EXISTS tablefunc;
假设我有以下 table:
Id color
A00 blue
A00 blue
A99 red
A99 blue
A95 yellow
A97 green
我想要这样的东西:
Id blue red yellow green
A00 2 0 0 0
A99 1 1 0 0
A95 0 0 1 0
A97 0 0 0 1
最简单的方法是什么?
我想过这个:
select Id,
sum(case when color='blue' then 1 else 0 end) as blue,
sum(case when color='red' then 1 else 0 end) as red,
.
.
.
from table
问题是我有太多的颜色,这样做会很累。有没有更简单的方法?
有很多方法可以实现:
使用过滤器
select
id,
count(*) filter (where color='blue') as "Blue",
count(*) filter (where color='red') as "Red",
count(*) filter (where color='yellow') as "Yellow",
count(*) filter (where color='green') as "Green"
from samp
group by id
你的方法
select
id,
sum(case when color='blue' then 1 else 0 end) as "Blue",
sum(case when color='red' then 1 else 0 end) as "Red",
sum(case when color='yellow' then 1 else 0 end) as "Yellow",
sum(case when color='green' then 1 else 0 end) as "Green"
from samp
group by id
使用交叉表
select * from crosstab(
'select id, color,count(*) from samp group by id,color order by id,color',
'select distinct color from samp order by color'
)
as ct("ID" varchar, "blue" int,"green" int,"red" int,"yellow" int);
注意:您必须使用以下查询为交叉表创建扩展
CREATE EXTENSION IF NOT EXISTS tablefunc;