一、刷题内容
原题链接
https://leetcode-cn.com/problems/rank-scores/
内容描述
SQL架构
编写一个 SQL 查询来实现分数排名。
如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。
+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
重要提示:对于 MySQL 解决方案,如果要转义用作列名的保留字,可以在关键字之前和之后使用撇号。例如 Rank
二、解题方法
1.方法一
降序排列的分数
select a.Score as Score
from Scores a
order by a.Score DESC
排名
select count(distinct b.Score) from Scores b where b.Score >= X as 'Rank';
结果
# Write your MySQL query statement below
select a.Score as Score,
(select count(distinct b.Score) from Scores b where b.Score >= a.Score) as 'Rank'
from Scores a
order by a.Score DESC;
该博客介绍了如何使用SQL查询实现分数排名,当分数相同时,排名相同且无间隔。提供了具体的SQL查询语句示例,展示了解决此问题的方法。

被折叠的 条评论
为什么被折叠?



