[LeetCode#178]Rank Scores
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks. +----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
For example, given the above Scores table, your query should generate the following report (order by highest score): +-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
这道题目让我们对分数进行排序。如果两个分数之间相同则存在相同的排名,需要注意的是,如果有相同排名后,下一个排名数字应该是一个连续的整数值:
在这里我们先拓展一下,先使用Excel函数来对此项题目求解:
在Excel中,可以使用SUMPRODUCT()函数来求解:

接着我们使用SQL语句来实现该需求:
解法一:
SELECT Score, (SELECT COUNT(DISTINCT Score) FROM Scores WHERE Score >= s.Score) Rank FROM Scores s ORDER BY Score DESC;
此题的解法是把成绩按照倒序排序,再把去重后每一门成绩做比较大小来统计数;
咱们再做一次扩展:就是成绩依然排名,不过要求是当要重复排名的时候,之后的排名数会跳过重复数的排名
我们知道在Excel中使用Rank函数就可以实现:

那么在SQL中怎么实现这个排名呢:
select score, RANK() OVER(order by Score DESC) rank from scores ORDER BY score DESC;
在这里可以使用排名函数就可以实现这个需求。
解法二:
SELECT Score,
(SELECT COUNT(*) FROM (SELECT DISTINCT Score s FROM Scores ) t WHERE s >= Score) Rank
FROM Scores ORDER BY Score DESC;
解法二与解法一的解题思路是一致的,只不过写法上略有不同。
解法三:
SELECT s.Score, COUNT(DISTINCT t.Score) Rank
FROM Scores s JOIN Scores t ON s.Score <= t.Score
GROUP BY s.Id ORDER BY s.Score DESC;
本方法使用了内交,join是inner join 的简写形式,自己和自己内交,条件是右表的分数大于等于左表,然后群组起来根据分数的降序排列。
解法四:
SELECT Score,
@rank := @rank + (@pre <> (@pre := Score)) Ranking
FROM Scores, (SELECT @rank := 0, @pre := -1) INIT
ORDER BY Score DESC;
这里用了两个变量,变量使用时其前面需要加@,这里的:= 是赋值的意思,如果前面有Set关键字,则可以直接用=号来赋值,如果没有,则必须要使用:=来赋值,两个变量rank和pre,其中rank表示当前的排名,pre表示之前的分数,下面代码中的<>表示不等于,如果左右两边不相等,则返回true或1,若相等,则返回false或0。初始化rank为0,pre为-1,然后按降序排列分数,对于分数4来说,pre赋为4,和之前的pre值-1不同,所以rank要加1,那么分数4的rank就为1,下面一个分数还是4,那么pre赋值为4和之前的4相同,所以rank要加0,所以这个分数4的rank也是1,以此类推就可以计算出所有分数的rank了。
[LeetCode#178]Rank Scores的更多相关文章
- MySQL中变量的用法——LeetCode 178. Rank Scores
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- sql -leetcode 178. Rank Scores
Score 很好得到: select Score from Scores order by Score desc; 要得到rank, 可以通过比较比当前Score 大的Score 的个数得到: sel ...
- LeetCode Database: Rank Scores
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- 【SQL】178. Rank Scores
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- 178. Rank Scores - database - 178. Rank Scores (Oracle)
题目链接 https://leetcode.com/problems/rank-scores/description/ 题意:对所有的分数按照降序进行排序,查询出分数和排名,排名相同的输出相同名 ...
- 178. Rank Scores
问题描述 解决方案 select sc.Score, (select count(*) from (select distinct Score from Scores ) ds where ds.Sc ...
- [LeetCode] Rank Scores 分数排行
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- [LeetCode] Rank Scores -- 数据库知识(mysql)
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- LeetCode——Rank Scores
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
随机推荐
- MongoDB for OPS 03:分片 shard 集群
写在前面的话 上一节的复制集也就是主从能够解决我们高可用和数据安全性问题,但是无法解决我们的性能瓶颈问题.所以针对性能瓶颈,我们需要采用分布式架构,也就是分片集群,sharding cluster! ...
- SpringBoot2.0.4部署在tomcat容器中
1. 修改启动类继承自SpringBootServletInitializer. 2. 重写config方法: @Overrideprotected SpringApplicationBuilder ...
- Python【day 15-3】函数部分
'''''' ''' 一.函数 1.函数定义 对功能或者动作的封装 在类中定义,就是方法 在类之外定义,就是函数 2.函数写法 1.定义或者申明函数 def 函数名(形参列表): 函数体(return ...
- C++ 手把手教你实现可变长的数组
01 实现自定义的可变长数组类型 假设我们要实现一个会自动扩展的数组,要实现什么函数呢?先从下面的main函数给出的实现,看看有什么函数是需要我们实现的. int main() { MyArray a ...
- 网站的favicon图标
网站的favicon图标 favicon.ico一般用于作为缩略的网站标志,它显示在浏览器的地址栏或者标签上. 制作favicon图标 把图片转换为png图片 把png图片转换为ico图标,这需要借助 ...
- JavaScript之找LHS查询和RHS查询
LHS和RHS,当变量出现在赋值操作的左侧时进行LHS 查询,出现在右侧时进行RHS 查询. LHS 查询是试图找到变量的容器本身,从而可以对其赋值. RHS 理解成retrieve his sour ...
- opencv::DNN介绍
DNN模块介绍: Tiny-dnn模块 支持深度学习框架 - Caffe - TensorFlow - Torch/PyTorch DNN运用 图像分类 对象检测 实时对象检测 图像分割 预测 视频对 ...
- 工具类Base64Util
在和服务器交互的过程中,有时候我们需要把图片编码成base64字符串传输,记录一下工具类 import android.graphics.Bitmap; import android.graphics ...
- 仓库管理移动应用解决方案——C#开发的移动应用开源解决方案
产品简介 SmoWMS是一款仓库管理移动解决方案,通过Smobiler平台开发,包含了仓库管理中基础的入库.出库.订单管理.调拨.盘点.报表等功能.支持扫码条码扫描.RFID扫描等仓库中常见的场景. ...
- 外置 tomcat 服务器设置
外置 Tomcat 没这么太用, 今天在 windows 搭 xwiki 服务器, 比预期多花了点时间, 主要是 tomcat 环境变量没配对, tomcat 启动后闪退, 还没有日志. 最后定位 ...