[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 ...
随机推荐
- Mysql 5.7:更改密码时出现ERROR 1054 (42S22): Unknown column 'password' in 'field list'
1.环境 在新服务器上重新安装了环境,原来是5.6的,就升级到了5.7版本. 2.问题 新安装的MySQL5.7,登录时提示密码错误,安装的时候并没有更改密码,后来通过免密码登录的方式更改密码. 输入 ...
- javascript实现base64编码、解码
我们知道,浏览器的window对象提供有window.atob()和window.btoa()方法可以对字符串进行Base64编码和解码. console.log(window.btoa(window ...
- 聊聊 Vue 中 title 的动态修改
由于之前的 Vue 项目打包成果物一直是嵌入集成平台中,所以一直没有关注过项目的 title.直到最近,突然有个需求,要求点击按钮在集成平台外新开一个页面,此时我才发现,原来我的项目的 title 一 ...
- Python 容器使用的 5 个技巧和 2 个误区
"容器"这两个字很少被 Python 技术文章提起.一看到"容器",大家想到的多是那头蓝色小鲸鱼:Docker,但这篇文章和它没有任何关系.本文里的容器,是 P ...
- Python分页爬取数据的分析
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 向右奔跑 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...
- flex布局开发
flex布局开发 布局原理 flex时flexible Box的缩写,意为"弹性布局",用来为盒子模型提供最大的灵活性,任何一个容器都可以定位flex布局 [注意] 当我们为父盒子 ...
- 【亲测有效】Ubuntu18.04 sudo apt update无法解析域名的解决方案
问题描述如下: 拿起了封尘已久的ThinkPad,输入 sudo apt update 的时候,发现这个命令变得不好使了,具体出现的问题如下图所示: #( 09/08/19@ 2:44下午 )( py ...
- Python 字符串用法总结
一.将某个对象转换为字符串,有str()和repr()两种方法 区别:repr() 转化为供解释器读取的形式str() 转化为适于人阅读的形式 a = 123456 print('repr输出:', ...
- 一文解读PV/UV/VV/IP (转)
什么是PV? PV即Page View,网站浏览量,指页面浏览的次数,用以衡量网站用户访问的网页数量.用户每次打开一个页面便记录1次PV,多次打开同一页面则浏览量累计.一般来说,PV与来访者的数量成正 ...
- [b0033] python 归纳 (十八)_队列Queue在多线程中使用(二)
# -*- coding: UTF-8 -*- """ 多线程同时读队列 使用 join(), task_done() 逻辑: 3个子线程并发 从有6个数据的队列中取数据 ...