无法使用我的查询透视表?

时间:2013-11-27 21:24:07

标签: sql sql-server sql-server-2005 pivot

我有一张看起来像这样的表 -

Id  AttributeName   AttributeValue
A1  Atr1            A1V1
A1  Atr2            A1V2
A1  Atr3            A1V3
A2  Atr1            A2V1
A2  Atr2            A2V2
A2  Atr3            A3V3

此表中的每个ID都具有完全相同的属性,即ATR1,ATR2,ATR3。这些属性的值是唯一的。

我想转动此表并获得以下输出 -

Id  Atr1 Atr2 Atr3
A1  A1V1 A1V2 A1V3
A2  A2V1 A2V2 A2V3

我该怎么做?

我尝试了一个查询,但它失败并显示错误 - 消息156,级别15,状态1,行21 关键字“FOR”附近的语法不正确。

-- Create a temporary table
DECLARE @MyTable TABLE
(Id varchar(25),
 AttributeName varchar(30),
 AttributeValue varchar(30))
-- Load Sample Data
INSERT INTO @MyTable VALUES ('A1', 'Atr1', 'A1V1')
INSERT INTO @MyTable VALUES ('A1', 'Atr2', 'A1V2')
INSERT INTO @MyTable VALUES ('A1', 'Atr3', 'A1V3')
INSERT INTO @MyTable VALUES ('A2', 'Atr1', 'A2V1')
INSERT INTO @MyTable VALUES ('A2', 'Atr2', 'A2V2')
INSERT INTO @MyTable VALUES ('A2', 'Atr3', 'A3V3')
SELECT Id, [Atr1], [Atr2],[Atr3]
FROM
( 
SELECT ID, AttributeName, AttributeValue
FROM @MyTable) AS SourceTable 
PIVOT 
(
    AttributeValue
    FOR AttributeName IN ([ATR1], [ATR2], [ATR3])
) AS pvt

3 个答案:

答案 0 :(得分:5)

为了扩展其他答案,PIVOT功能需要某种类型的聚合。由于您要从行转换为列的值是字符串,因此您只能使用max()min()聚合函数。

AttributeName / AttributeValue对有ID / max对时,@Muhammed Ali's答案会有效,如果每个min有多对,那么您只会返回INSERT INTO @MyTable VALUES ('A1', 'Atr1', 'A1V1'); INSERT INTO @MyTable VALUES ('A1', 'Atr1', 'A1V4'); INSERT INTO @MyTable VALUES ('A1', 'Atr2', 'A1V2'); INSERT INTO @MyTable VALUES ('A1', 'Atr3', 'A1V3'); INSERT INTO @MyTable VALUES ('A2', 'Atr1', 'A2V1'); INSERT INTO @MyTable VALUES ('A2', 'Atr2', 'A2V2'); INSERT INTO @MyTable VALUES ('A2', 'Atr3', 'A3V3'); A1值。

例如,如果您的样本数据是:

Atr1

即使您有max(attributevalue)| ID | ATR1 | ATR2 | ATR3 | |----|------|------|------| | A1 | A1V4 | A1V2 | A1V3 | | A2 | A2V1 | A2V2 | A3V3 | 组合的多行,其他查询也只返回row_number()

row_number()

我猜你真的想要归还所有的组合。我建议扩展您的查询以在查询中包含窗口函数SELECT Id, [Atr1], [Atr2],[Atr3] FROM ( SELECT ID, AttributeName, AttributeValue, row_number() over(partition by id, attributename order by attributevalue) seq FROM @MyTable ) AS SourceTable PIVOT ( max(AttributeValue) FOR AttributeName IN ([ATR1], [ATR2], [ATR3]) ) AS pvt order by id; 。此查询生成一个唯一值,该值将包含在PIVOT的分组方面,并允许您为每个ID返回多行。

通过添加| ID | ATR1 | ATR2 | ATR3 | |----|------|--------|--------| | A1 | A1V1 | A1V2 | A1V3 | | A1 | A1V4 | (null) | (null) | | A2 | A2V1 | A2V2 | A3V3 | ,查询将类似于以下内容:

SELECT Id, 
  max(case when attributename = 'Atr1' then attributevalue end) Atr1,
  max(case when attributename = 'Atr2' then attributevalue end) Atr2,
  max(case when attributename = 'Atr3' then attributevalue end) Atr3
FROM
( 
  SELECT ID, AttributeName, AttributeValue,
    row_number() over(partition by id, attributename
                      order by attributevalue) seq
  FROM @MyTable
) AS SourceTable 
group by id, seq

SQL Fiddle with Demo。您将获得一个返回所有行的结果:

{{1}}

如果您在掌握PIVOT的概念时遇到困难,那么我建议使用聚合函数和CASE表达式的组合来获得结果。然后,您可以看到序列/ id的分组:

{{1}}

请参阅SQL Fiddle with Demo

答案 1 :(得分:1)

DECLARE @MyTable TABLE
(Id varchar(25),
 AttributeName varchar(30),
 AttributeValue varchar(30))
-- Load Sample Data
INSERT INTO @MyTable VALUES ('A1', 'Atr1', 'A1V1')
INSERT INTO @MyTable VALUES ('A1', 'Atr2', 'A1V2')
INSERT INTO @MyTable VALUES ('A1', 'Atr3', 'A1V3')
INSERT INTO @MyTable VALUES ('A2', 'Atr1', 'A2V1')
INSERT INTO @MyTable VALUES ('A2', 'Atr2', 'A2V2')
INSERT INTO @MyTable VALUES ('A2', 'Atr3', 'A3V3')
SELECT Id, [Atr1], [Atr2],[Atr3]
FROM
( 
SELECT ID, AttributeName, AttributeValue
FROM @MyTable) AS SourceTable 
PIVOT 
(
    MAX(AttributeValue)
    FOR AttributeName IN ([ATR1], [ATR2], [ATR3])
) AS pvt

您缺少数据透视表语法

中的功能

结果集

Id  Atr1    Atr2    Atr3
A1  A1V1    A1V2    A1V3
A2  A2V1    A2V2    A3V3

答案 2 :(得分:0)

试试这个:

select id, 
       max(case AttributeName when 'Atr1' then AttributeName else '' end) as atr1,
       max(case AttributeName when 'Atr2' then AttributeName else '' end) as atr2,
       max(case AttributeName when 'Atr3' then AttributeName else '' end) as atr3,
       AttributeValue
  from YourTable
group by id, AttributeValue
order by id