SQL281. 最差是第几名(一)
描述
grade | number |
A | 2 |
D | 1 |
C | 2 |
B | 2 |
grade | t_rank |
A | 2 |
B | 4 |
C | 6 |
D | 7 |
示例1
输入:
drop table if exists class_grade; CREATE TABLE class_grade ( grade varchar(32) NOT NULL, number int(4) NOT NULL ); INSERT INTO class_grade VALUES ('A',2), ('D',1), ('C',2), ('B',2);
输出:
A|2 B|4 C|6 D|7
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3372KB, 提交时间: 2021-12-03
select a.grade, SUM(b.number) AS t_rank FROM class_grade a JOIN class_grade b ON a.grade >= b.grade group by a.grade order by a.grade
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3456KB, 提交时间: 2021-11-30
select a.grade, sum(b.number) as t_rank from class_grade as a left join class_grade as b on a.grade>= b.grade group by a.grade;
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3480KB, 提交时间: 2021-09-10
select grade,sum(number)over (order by grade) from class_grade
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3480KB, 提交时间: 2021-09-01
select grade, sum(number) over(order by grade) as t_rank from class_grade
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3492KB, 提交时间: 2021-09-14
select grade, sum(number) over (order by grade) as t_rank from class_grade