题目描述

(通过次数166,157 | 提交次数273,273,通过率60.80%)

表:Scores
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| score | decimal |
+-------------+---------+
Id是该表的主键。
该表的每一行都包含了一场比赛的分数。Score是一个有两位小数点的浮点值。
编写 SQL 查询对分数进行排序。排名按以下规则计算:
分数应按从高到低排列。
如果两个分数相等,那么两个分数的排名应该相同。
在排名相同的分数后,排名数应该是下一个连续的整数。换句话说,排名之间不应该有空缺的数字。
按score降序返回结果表。
查询结果格式如下所示。
示例 1:
输入:
Scores 表:
+----+-------+
| id | score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
输出:
+-------+------+
| score | rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
通过次数166,157提交次数273,273
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rank-scores
表:Scores +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | score | decimal | +-------------+---------+ Id是该表的主键。 该表的每一行都包含了一场比赛的分数。Score是一个有两位小数点的浮点值。 编写 SQL 查询对分数进行排序。排名按以下规则计算: 分数应按从高到低排列。 如果两个分数相等,那么两个分数的排名应该相同。 在排名相同的分数后,排名数应该是下一个连续的整数。换句话说,排名之间不应该有空缺的数字。 按score降序返回结果表。 查询结果格式如下所示。 示例 1: 输入: Scores 表: +----+-------+ | id | score | +----+-------+ | 1 | 3.50 | | 2 | 3.65 | | 3 | 4.00 | | 4 | 3.85 | | 5 | 4.00 | | 6 | 3.65 | +----+-------+ 输出: +-------+------+ | score | rank | +-------+------+ | 4.00 | 1 | | 4.00 | 1 | | 3.85 | 2 | | 3.65 | 3 | | 3.65 | 3 | | 3.50 | 4 | +-------+------+ 通过次数166,157提交次数273,273 来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/rank-scores
表:Scores
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| score       | decimal |
+-------------+---------+
Id是该表的主键。
该表的每一行都包含了一场比赛的分数。Score是一个有两位小数点的浮点值。

编写 SQL 查询对分数进行排序。排名按以下规则计算:

分数应按从高到低排列。
如果两个分数相等,那么两个分数的排名应该相同。
在排名相同的分数后,排名数应该是下一个连续的整数。换句话说,排名之间不应该有空缺的数字。
按score降序返回结果表。

查询结果格式如下所示。

示例 1:
输入: 
Scores 表:
+----+-------+
| id | score |
+----+-------+
| 1  | 3.50  |
| 2  | 3.65  |
| 3  | 4.00  |
| 4  | 3.85  |
| 5  | 4.00  |
| 6  | 3.65  |
+----+-------+
输出: 
+-------+------+
| score | rank |
+-------+------+
| 4.00  | 1    |
| 4.00  | 1    |
| 3.85  | 2    |
| 3.65  | 3    |
| 3.65  | 3    |
| 3.50  | 4    |
+-------+------+
通过次数166,157提交次数273,273

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rank-scores
//测试数据
Create table If Not Exists Scores (id int, score DECIMAL(3,2));
insert into Scores (id, score) values ('1', '3.5');
insert into Scores (id, score) values ('2', '3.65');
insert into Scores (id, score) values ('3', '4.0');
insert into Scores (id, score) values ('4', '3.85');
insert into Scores (id, score) values ('5', '4.0');
insert into Scores (id, score) values ('6', '3.65');
//测试数据 Create table If Not Exists Scores (id int, score DECIMAL(3,2)); insert into Scores (id, score) values ('1', '3.5'); insert into Scores (id, score) values ('2', '3.65'); insert into Scores (id, score) values ('3', '4.0'); insert into Scores (id, score) values ('4', '3.85'); insert into Scores (id, score) values ('5', '4.0'); insert into Scores (id, score) values ('6', '3.65');
//测试数据
Create table If Not Exists Scores (id int, score DECIMAL(3,2));

insert into Scores (id, score) values ('1', '3.5');
insert into Scores (id, score) values ('2', '3.65');
insert into Scores (id, score) values ('3', '4.0');
insert into Scores (id, score) values ('4', '3.85');
insert into Scores (id, score) values ('5', '4.0');
insert into Scores (id, score) values ('6', '3.65');

解题思路

这仍然是一道分组排序题,题目本身也不复杂。因为要求分组内排序的序号连续且相同的值有相同的序号,使用dense_rank分析函数即可。

但为什么只有60.80%的通过率呢?

坦白说,强哥也折在这里了。第二次提交才通过的。

第一次提交失败,检查了好几遍,没发现哪里写的有问题。

最后看到要求返回的序号的字段名rank,才想起来这是一个关键字。

对于关键字作为字段名使用,倒也不是完全不可以,但还是建议尽量不要使用。像这道题,就报出了莫名其妙的错误。

当然,如果非得要用关键字作为字段名返回,需要在字段名两边加上反单引号。

比如,\`rank\`。

参考SQL

未特别说明的情况下,参考SQL为基于MySQL8.0实现。
select
score,
dense_rank() over(order by score desc) as `rank`
from Scores a
order by score desc;
select score, dense_rank() over(order by score desc) as `rank` from Scores a order by score desc;
select
    score,
    dense_rank() over(order by score desc) as `rank`
from Scores a
order by score desc;
picture loss