我有一个包含以下列的表
Create table TestTemp (id int, usernm varchar(15), subject varchar(25), score int)
Insert into TestTemp(id, usernm, subject, score) values (1, 'abc', 'english', 40)
Insert into TestTemp(id, usernm, subject, score) values (2, 'abc', 'maths', 60)
Insert into TestTemp(id, usernm, subject, score) values (3, 'abc', 'science', 55)
Insert into TestTemp(id, usernm, subject, score) values (4, 'efg', 'english', 46)
Insert into TestTemp(id, usernm, subject, score) values (5, 'efg', 'maths', 49)
Insert into TestTemp(id, usernm, subject, score) values (6, 'efg', 'science', 80)
我希望结果以这种格式显示
Usernm english maths science
abc 40 60 55
efg 46 49 80
答案 0 :(得分:-1)
这样做:
SELECT *
FROM
(
SELECT usernm, [score], [Subject]
FROM TestTemp
) DS
PIVOT
(
MAX([score]) FOR [Subject] IN ([english], [maths], [science])
) PVT
有关详细信息,请查看此link。