gpt4 book ai didi

sql - 如何在 SQL Server 中透视文本列?

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

我的数据库中有一个这样的表(SQL Server 2008)

ID      Type            Desc
--------------------------------
C-0 Assets No damage
C-0 Environment No impact
C-0 People No injury or health effect
C-0 Reputation No impact
C-1 Assets Slight damage
C-1 Environment Slight environmental damage
C-1 People First Aid Case (FAC)
C-1 Reputation Slight impact; Compaints from local community

我必须将 Assets 、人员、环境和声誉显示为列,并将匹配的 Desc 显示为值。但是当我运行数据透视查询时,我的所有值都为空。

有人可以查看我的查询并告诉我我做错了什么吗?
Select severity_id,pt.[1] As People, [2] as Assets , [3] as Env, [4] as Rep
FROM
(
select * from COMM.Consequence
) As Temp
PIVOT
(
max([DESCRIPTION])
FOR [TYPE] In([1], [2], [3], [4])
) As pt

这是我的输出
ID  People  Assets   Env     Rep
-----------------------------------
C-0 NULL NULL NULL NULL
C-1 NULL NULL NULL NULL
C-2 NULL NULL NULL NULL
C-3 NULL NULL NULL NULL
C-4 NULL NULL NULL NULL
C-5 NULL NULL NULL NULL

最佳答案

Select severity_id, pt.People, Assets, Environment, Reputation
FROM
(
select * from COMM.Consequence
) As Temp
PIVOT
(
max([DESCRIPTION])
FOR [TYPE] In([People], [Assets], [Environment], [Reputation])
) As pt

关于sql - 如何在 SQL Server 中透视文本列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025934/

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