我有以下数据:
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
然而,这是错误的。我如何解决它?