很明显使用case when 语句

select grade,
CASE
when grade ='A' then (select sum(number) from class_grade where grade <='A' )
when grade ='B' then (select sum(number) from class_grade where grade <='B' )
when grade ='C' then (select sum(number) from class_grade where grade <='C' )
when grade ='D' then (select sum(number) from class_grade where grade <='D' )
else (select sum(number) from class_grade)
end t_rank
from class_grade
order by grade

或者使用窗口函数:

-- 看上面的答案,千篇一律, order by grade 已经排过序了,因此就不用再排序了
select grade,
sum(number) over(order by grade) t_rank
from class_grade;