本题出题的题意其实主要是考察sum() over (order by ) 开窗函数,sum(a) over (order by b) 的含义是:
例如
a b
1 2
3 4
5 6
按照b列排序,将a依次相加,得到结果,如下:
a b sum(a) over (order by b):
1 2 1
3 4 1+3
5 6 1+3+5
此题就是将b换成了grade,即使b列乱序也没关系,因为有order by b:
number grade sum(number) over(order by grade):
2 A 2
2 B 2+2
2 C 2+2+2
1 D 2+2+2+1
所以我们代码可以如下写:
select grade, sum(number) over(order by grade) t_rank from class_grade order by grade;