T-SQL帮助多个pivot / unpivot

时间:2010-12-09 17:52:22

标签: tsql ssrs-2008 pivot reporting-services ssrs-grouping

我不确定什么是最好的approch枢轴或unpivot。我在一个表中有以下列。


group1 | group2 | group3 | amtA1 | amtA2 | amtB1 | amtB2 | amtC1 | amtC1

我需要像

这样的产品表
group1
X | amtA1  | amtB1  | amtC1
Y | amtA2  | amtB2  | amtC2

group2
X | amtA1  | amtB1  | amtC1
Y | amtA2  | amtB2  | amtC2

group3
X | amtA1  | amtB1  | amtC1
Y | amtA2  | amtB2  | amtC2

我尽我所能但却无法理解。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试

SELECT group1 As Group, "X" as ColType, amtA1 as ColA,  amtB1 as ColB, amtC1 as ColC
UNION
SELECT group1, "Y", amtA2,  amtB2, amtC2
UNION 
SELECT group2, "X", amtA1,  amtB1, amtC1
UNION 
SELECT group2, "Y", amtA2,  amtB2, amtC2
UNION 
SELECT group3, "X", amtA1,  amtB1, amtC1
UNION 
SELECT group3, "Y", amtA2,  amtB2, amtC2

您现在可以根据“组”列轻松分组。

虽然我会与您组织中的某人进行关于表规范化的“讨论”。