当透视(n)varchar数据时,SQL Pivot返回空值

时间:2015-01-29 17:36:53

标签: sql sql-server pivot

我正在努力在Microsoft SQL Server 2012中转移数据。当我尝试使用varchar值执行数据透视时,脚本会返回所有记录的 NULL 值。我的代码如下所示:

select [Identifier], [Geography Description], [Temperature Description], [Humidity Description], [Pressure Description]
from 
    (
        select [Identifier], [AttributeID], [AttributeValue]
            from [dbo].[StringAttributeValues]
    ) as A

PIVOT

(
    max(AttributeID)
    for [AttributeValue] in ([Geography Description], [Temperature Description], [Humidity Description], [Pressure Description])
) as B

然后我按照这个thread的最高答案的建议并修改了我的脚本。最终结果是我的脚本仍然为所有列的每条记录返回 NULL 值。

select [Identifier], [1] as [Geography Description], [2] as [Temperature Description], [3] as [Humidity Description], [4] as [Pressure Description]
from 
    (
        select [Identifier], [AttributeID], [AttributeValue]
            from [dbo].[StringAttributeValues]
    ) as A

PIVOT

(
    max(AttributeID)
    for [AttributeValue] in ([1], [2], [3], [4])
) as B

以下是一些详细说明我的表结构和一些要使用的示例值的代码:

    create table [dbo].[StringAttributeValues]
(
    [Identifier] int
    ,[AttributeID] int
    ,[AttributeValue] nvarchar(15)
)

insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (1, 1, 'Mountains')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (2, 1, 'Coast')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (3, 1, 'Desert')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (4, 1, 'Jungle')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (5, 1, 'Plains')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (1, 2, 'Cold')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (2, 2, 'Warm')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (3, 2, 'Hot')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (4, 2, 'Hot')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (5, 2, 'Hot')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (1, 3, 'Dry')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (2, 3, 'Humid')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (3, 3, 'Dry')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (4, 3, 'Humid')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (5, 3, 'Dry')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (1, 4, 'High')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (2, 4, 'Low')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (3, 4, 'High')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (4, 4, 'Low')
insert into dbo.StringAttributeValues ([Identifier], [AttributeID], [AttributeValue]) values (5, 4, 'Low')

任何人都可以请我提供一些见解,了解为什么会发生这种情况以及我如何解决这个问题?

(这是一个非常简单的程序,适用于MATLAB,C#,Python等语言)

提前谢谢。

2 个答案:

答案 0 :(得分:0)

它只是PIVOT子句中列的错位。 试试这个:

SELECT [Identifier], [1] as [Geography], [2] as [Temperature], [3] as [Humidity], [4] as [Pressure]
FROM
    (
        SELECT [Identifier], [AttributeID], [AttributeValue]
        FROM dbo.StringAttributeValues
    ) as A

PIVOT
(
    max(AttributeValue)
    for AttributeID in ([1], [2], [3], [4])
) as B

答案 1 :(得分:-1)

select [Identifier], [1] as [Geography Description], [2] as [Temperature Description], [3] as [Humidity Description], [4] as [Pressure Description]
from 
    (
        select [Identifier], [AttributeID], [AttributeValue]

            from [dbo].[StringAttributeValues]
    ) as x

PIVOT

(
    max([AttributeValue])
    for [AttributeID] in ([1], [2], [3], [4])
) as piv

FIDDLE