2

我的表中有以下记录

ID   StudentID   Semester   Grade     
1    1           First      100
2    1           Second     90
3    2           First      90
4    1           Third      85

我想将学生 ID 1 的所有记录合并到一个记录中

StudentID   First   Second   Third
1           100     90       85 
2           90      null     null

有任何想法吗?

4

1 回答 1

5

由于您没有提及您使用的是什么 RDBMS,请尝试一下。

SELECT  StudentID,
        MAX(CASE WHEN Semester = 'First' THEN Grade ELSE NULL END) AS First,
        MAX(CASE WHEN Semester = 'Second' THEN Grade ELSE NULL END) AS Second,
        MAX(CASE WHEN Semester = 'Third' THEN Grade ELSE NULL END) AS Third
FROM    tableName
GROUP BY StudentID
于 2012-12-08T08:49:17.930 回答