SQL257. 刷题通过的题目排名
描述
id | number |
1 2 3 4 5 6 | 4 3 3 2 5 4 |
.....
第6行表示id为6的用户通过了4个题目;
请你根据上表,输出通过的题目的排名,通过题目个数相同的,排名相同,此时按照id升序排列,数据如下:
id | number | t_rank |
5 1 6 2 3 4 | 5 4 4 3 3 2 | 1 2 2 3 3 4 |
id为5的用户通过了5个排名第1,
id为1和id为6的都通过了4个,并列第2
示例1
输入:
drop table if exists passing_number; CREATE TABLE `passing_number` ( `id` int(4) NOT NULL, `number` int(4) NOT NULL, PRIMARY KEY (`id`)); INSERT INTO passing_number VALUES (1,4), (2,3), (3,3), (4,2), (6,4), (5,5);
输出:
5|5|1 1|4|2 6|4|2 2|3|3 3|3|3 4|2|4
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3456KB, 提交时间: 2021-12-06
select * from (select *, dense_rank()over(order by number desc) t_rank from passing_number) order by t_rank ,id
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3472KB, 提交时间: 2021-08-02
SELECT p1.id, p1.number, COUNT(DISTINCT p2.number) rank FROM passing_number p1, passing_number p2 WHERE p1.number <= p2.number GROUP BY p1.id ORDER BY rank, p1.id;
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3496KB, 提交时间: 2021-09-15
SELECT *, DENSE_RANK()OVER(ORDER BY number DESC) t_rank FROM passing_number ORDER BY t_rank,id
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3496KB, 提交时间: 2021-09-11
select *, case when 1=1 then dense_rank()over(order by number desc) end t_rank from passing_number order by number desc, id asc
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3500KB, 提交时间: 2021-12-11
SELECT id,number, DENSE_RANK() OVER(ORDER BY number DESC) AS t_rank FROM passing_number ORDER BY number DESC, id ASC;