使用自定义排名和大小写的最高维度

问题描述 投票:0回答:1

我想为学生计算出最高的课程:程序级别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

我尝试将自定义等级分配给程序级别,然后采用等级的最小值,但是我在那里缺少一些东西。有人可以建议一种更好的方法来获得结果

sql sql-server
1个回答
0
投票

一种方法是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;
© www.soinside.com 2019 - 2024. All rights reserved.