我想为学生计算出最高的课程:程序级别PhD> MA> CERT
Table A:
Student | Program
Student 1|A.PhD
Student 1|B.MA
Student 2|A.CERT
Student 2|B.MA
Output
Student | Highest Program
Student 1|A.PhD
Student 2|B.MA
我尝试将自定义等级分配给程序级别,然后采用等级的最小值,但是我在那里缺少一些东西。有人可以建议一种更好的方法来获得结果
一种方法是coalesce()
,有些聪明:
select student,
coalesce(max(case when program = 'A.PhD' then program end),
max(case when program = 'B.MA' then program end),
max(case when program = 'A.Cert' then program end)
) as highest_program
from a
group by student;