gpt4 book ai didi

sql - postgresql 中的数据透视表

转载 作者:行者123 更新时间:2023-11-29 14:31:30 27 4
gpt4 key购买 nike

我有什么

customerid   status

Ax 1
Bx 3
Cx 5
Dx 4
Ex 2

我希望在 table 上方旋转。

我需要什么

customerid   status_1 status_2 status_3 status_4 status_5

Ax 1 0 0 0 0
Bx 0 0 1 0 0
Cx 0 0 0 0 1
Dx 0 0 0 1 0
Ex 0 1 0 0 0

最佳答案

select customerid, 
case when status = 1 then 1 else 0 end as status_1,
case when status = 2 then 1 else 0 end as status_2,
case when status = 3 then 1 else 0 end as status_3,
case when status = 4 then 1 else 0 end as status_4,
case when status = 5 then 1 else 0 end as status_5
from your_table
order by customerid;

关于sql - postgresql 中的数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51090675/

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