CREATE TABLE Scores (
StudentName VARCHAR(50),
Subject VARCHAR(50),
Score INT
);
INSERT INTO Scores VALUES ('张三', '语文', 90);
INSERT INTO Scores VALUES ('张三', '数学', 80);
INSERT INTO Scores VALUES ('张三', '英语', 70);
INSERT INTO Scores VALUES ('李四', '语文', 85);
INSERT INTO Scores VALUES ('李四', '数学', 75);
INSERT INTO Scores VALUES ('李四', '英语', 65);
SELECT *
FROM (
SELECT StudentName, Subject, Score
FROM Scores
) AS SourceTable
PIVOT (
--字段
AVG(Score)
FOR Subject IN ([语文], [数学], [英语])
) AS PivotTable;