gpt4 book ai didi

sql-server - SQL 行到列 sql

转载 作者:行者123 更新时间:2023-12-03 02:58:13 30 4
gpt4 key购买 nike

CallType                Jan   Feb   Mar
vend call Goal >45 0 57 0
OCS call Goal >25 0 13 0
Modified Vend Target 45 38 45
Modified OCS Target 25 25 25

我有这样的表

但是结果我需要像这样显示请帮忙...

CallType  vend call Goal>45 OCS call Goal>25    ModiVTart    ModifOCSTarget
Jan 0 0 45 25
Feb 57 13 38 25
Mar 0 0 45 25

最佳答案

测试数据

DECLARE @TABLE TABLE 
(CallType VARCHAR(1000), Jan INT,Feb INT,Mar INT)
INSERT INTO @TABLE VALUES
('vend call Goal >45' , 0 , 57 , 0),
('OCS call Goal >25' , 0 , 13 , 0),
('Modified Vend Target', 45 , 38 , 45),
('Modified OCS Target' , 25 , 25 , 25)

查询

SELECT N AS CallType
,[vend call Goal >45]
,[OCS call Goal >25]
,[Modified Vend Target]
,[Modified OCS Target]
FROM @TABLE t
UNPIVOT (Vals FOR N IN (Jan,Feb,Mar))up
PIVOT (SUM(Vals)
FOR CallType
IN ([vend call Goal >45],[OCS call Goal >25]
,[Modified Vend Target],[Modified OCS Target])
)p

结果集

╔══════════╦════════════════════╦═══════════════════╦══════════════════════╦═════════════════════╗
║ CallType ║ vend call Goal >45 ║ OCS call Goal >25 ║ Modified Vend Target ║ Modified OCS Target ║
╠══════════╬════════════════════╬═══════════════════╬══════════════════════╬═════════════════════╣
║ Feb ║ 57 ║ 13 ║ 38 ║ 25 ║
║ Jan ║ 0 ║ 0 ║ 45 ║ 25 ║
║ Mar ║ 0 ║ 0 ║ 45 ║ 25 ║
╚══════════╩════════════════════╩═══════════════════╩══════════════════════╩═════════════════════╝

关于sql-server - SQL 行到列 sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29720848/

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