SQL 旋转 (n)varchar 数据时旋转 returns 空值

SQL Pivot returns null values when pivoting (n)varchar data

我正在努力在 Microsoft SQL Server 2012 中转换数据。当我尝试使用 varchar 值执行转换时,脚本 returns 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 最重要答案的建议修改了我的脚本。最终结果是我的脚本仍然 returns 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

这里有一些代码详细说明了我的 table 结构和一些要使用的示例值:

    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 等语言中,这是一个非常简单的过程)

提前谢谢你。

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

这只是 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