0

我有以下数据:

Id  names   namevalues
1   name1   first value1
1   name2   first value2
2   name1   second value1
2   name2   second value2

DDL 是:

Declare @t table(Id int, names varchar(50),namevalues varchar(100))
Insert Into @t select 1,'name1','first value1'
Insert Into @t select 1,'name2','first value2'
Insert Into @t select 1,'name1','second value1'
Insert Into @t select 1,'name2','second value2'

select * from @t

预期输出为:

Id  name1           name2
1   first value1    first value2
2   second value1   second value2

我的尝试:

SELECT *
FROM  @t AS src 
PIVOT ( 
    MAX(namevalues) FOR namevalues IN ([name1],[name2] ) 
) AS pvt 

然而,这是错误的。我如何解决它?

4

1 回答 1

1

您正在旋转names列,而不是 namevalues 列,所以

SELECT * 
FROM  @t AS src  
PIVOT (  
    MAX(namevalues) FOR names IN ([name1],[name2] )  
) AS pvt 
于 2012-10-22T12:50:42.267 回答