我的表格中有以下记录
ID StudentID Semester Grade
1 1 First 100
2 1 Second 90
3 2 First 90
4 1 Third 85
我想将学生证1的所有记录合并到一个记录中
StudentID First Second Third
1 100 90 85
2 90 null null
有什么想法吗?
答案 0 :(得分: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