题目是:按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
分别有以下表格(ID代表学生学号)
答案是:
select a.ID,
max(case when b.course = '语文' then a.score else null end) as '语文成绩',
max(case when b.course = '数学' then a.score else null end) as '数学成绩',
max(case when b.course = '英语' then a.score else null end) as '英语成绩', avg(a.score) as 平均分
from score as a left join course as b
on a.courseID = b.courseID
group by a.ID
输出结果是:
问题是:为什么要加【max】? 如果不加则输出结果只能取每个人的第一项成绩,如图所示: