【发布时间】:2011-01-28 14:36:52
【问题描述】:
从my last question 得到如此好的反馈后。也许有人也可以帮助我解决这个问题。
我必须从 SQL Server 轮换给定的表,但正常的数据透视不起作用(据我尝试)。那么有人知道如何将表格旋转成所需的格式吗?
只是为了让问题更复杂,给定标签的列表可能会有所不同,并且可能会在任何给定时间出现新的标签名称。
给定数据
ID | Label | Numerator | Denominator | Ratio
---+-----------------+-------------+---------------+--------
1 | LabelNameOne | 41 | 10 | 4,1
1 | LabelNameTwo | 0 | 0 | 0
1 | LabelNameThree | 21 | 10 | 2,1
1 | LabelNameFour | 15 | 10 | 1,5
2 | LabelNameOne | 19 | 19 | 1
2 | LabelNameTwo | 0 | 0 | 0
2 | LabelNameThree | 15 | 16 | 0,9375
2 | LabelNameFive | 19 | 19 | 1
2 | LabelNameSix | 17 | 17 | 1
3 | LabelNameOne | 12 | 12 | 1
3 | LabelNameTwo | 0 | 0 | 0
3 | LabelNameThree | 11 | 12 | 0,9167
3 | LabelNameFour | 12 | 12 | 1
3 | LabelNameSix | 0 | 1 | 0
想要的结果
ID | LabelNameOneNumerator | LabelNameOneDenominator | LabelNameOneRatio | LabelNameTwoNumerator | LabelNameTwoDenominator | LabelNameTwoRatio | LabelNameThreeNumerator | LabelNameThreeDenominator | LabelNameThreeRatio | ...
---+-----------------------+-------------------------+-------------------+-----------------------+-------------------------+-------------------+-------------------------+---------------------------+---------------------+-----
1 | 41 | 10 | 4,1 | 0 | 0 | 0 | 21 | 10 | 2,1 | ...
2 | 19 | 19 | 1 | 0 | 0 | 0 | 15 | 16 | 0,9375 | ...
3 | 12 | 12 | 1 | 0 | 0 | 0 | 11 | 12 | 0,9167 | ...
我知道,在我之前的问题得到如此好的答案后,我应该能够自己解决这个问题,但我就是无法理解这个枢轴,非枢轴部分。
另外,如果您需要更多 SQL 方式的示例数据,您可以试试这个:
DECLARE @src AS TABLE
(
ID int NOT NULL
,Label varchar(14) NOT NULL
,Numerator int NOT NULL
,Denominator int NOT NULL
,Ratio decimal(10, 4) NOT NULL
) ;
INSERT INTO @src
VALUES (1, 'LabelNameOne', 41, 10, 4.1) ;
INSERT INTO @src
VALUES (1, 'LabelNameTwo', 0, 0, 0) ;
INSERT INTO @src
VALUES (1, 'LabelNameThree', 21, 10, 2.1) ;
INSERT INTO @src
VALUES (1, 'LabelNameFour', 15, 10, 1.5) ;
INSERT INTO @src
VALUES (2, 'LabelNameOne', 19, 19, 1) ;
INSERT INTO @src
VALUES (2, 'LabelNameTwo', 0, 0, 0) ;
INSERT INTO @src
VALUES (2, 'LabelNameThree', 15, 16, 0.9375) ;
INSERT INTO @src
VALUES (2, 'LabelNameFive', 19, 19, 1) ;
INSERT INTO @src
VALUES (2, 'LabelNameSix', 17, 17, 1) ;
INSERT INTO @src
VALUES (3, 'LabelNameOne', 12, 12, 1) ;
INSERT INTO @src
VALUES (3, 'LabelNameTwo', 0, 0, 0) ;
INSERT INTO @src
VALUES (3, 'LabelNameThree', 11, 12, 0.9167) ;
INSERT INTO @src
VALUES (3, 'LabelNameFour', 12, 12, 1) ;
INSERT INTO @src
VALUES (3, 'LabelNameSix', 0, 1, 0) ;
【问题讨论】:
-
与我在第一个问题上提供的答案类似,做你想做的事情的唯一方法是动态 SQL,这通常不推荐。 T-SQL 不是为产生这种输出而设计的。相反,您应该简单地将原始数据返回到中间层或报告工具,并在那里“透视”数据。
-
我不同意@Thomas。虽然动态 SQL 在这里是合适的解决方案,但请记住它不是好/坏,推荐/不推荐。它只是一个工具。查看sommarskog.se/dynamic_sql.html 了解有关动态 T-SQL 的更多信息。
-
@Jeremiah - IMO 我们应该区分什么是可能的和推荐的。是的,可能在 T-SQL 中使用动态 SQL 生成任何您可以想象的输出。然而,正如螺丝刀本身既不是好也不是坏的工具,可以用来敲钉子,它也不是最好的工具。 T-SQL 不是为报告而设计的。
标签: sql sql-server sql-server-2005 tsql pivot