gpt4 book ai didi

postgresql - 在 PostgreSQL 上创建统计分类变量的列的最简单方法是什么?也许某种旋转?

转载 作者:行者123 更新时间:2023-12-05 03:51:55 26 4
gpt4 key购买 nike

假设我有下表:

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

Fiddle

你的方法

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

Fiddle

使用交叉表

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;

关于postgresql - 在 PostgreSQL 上创建统计分类变量的列的最简单方法是什么?也许某种旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62541157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com