gpt4 book ai didi

sql - 在 SQL Server 中将行转换为列不起作用?

转载 作者:行者123 更新时间:2023-12-01 15:06:19 25 4
gpt4 key购买 nike

period  providerid  type    volume     subscribers
--------------------------------------------------
Aug-2016 7 1 4027917 117172
Aug-2016 7 2 5325430 232293
Aug-2016 7 3 8722165 236472
Jul-2016 7 1 2981655 97409
Jul-2016 7 2 6449570 147315
Jul-2016 7 3 7702484 206140

我想要这种格式的结果。

period      providerid  SMS     Data    minutes
Aug-2016 7 432142 42342 5454
Jul-2016 7 5454 5454 545

我试过这个查询,但它不起作用。

select   
period, providerid, 1 as SMS, 2 as Data, 3 as minutes
from
#P
pivot
(sum(volume) for [type] in ([1],[2],[3])) as P

请在 SQL Server 中帮助我

最佳答案

去掉 subscribers 列:

SELECT  [period],
providerid,
[1] as SMS,
[2] as [Data],
[3] as [minutes]
FROM (
SELECT [period],providerid, [type], volume
FROM YourTable
) as t
PIVOT (
MAX(volume) FOR [type] in ([1], [2], [3])
) as P

输出:

period      providerid  SMS     Data    minutes
Aug-2016 7 4027917 5325430 8722165
Jul-2016 7 2981655 6449570 7702484

关于sql - 在 SQL Server 中将行转换为列不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39207722/

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