LeetCode——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 |
+-------+------+
此题,其本质就是赋值行号(需要注意分数相同的情景).
在实践过程中,初版答案如下所示:
# 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;
此处,使用select count来统计行号,注意使用distinct来区分相同分数.
但是,此解题方案的效率较差,sql运行肯定是越快越好.
因此,在sql中引入变量来赋值行号,以替代耗时的select count操作.
# Write your MySQL query statement below
SELECT
Score,
@row := @row + (@pre <> (@pre := Score)) AS Rank
FROM Scores, (SELECT @row := 0, @pre := -1) t
ORDER BY Score DESC;
此处,查询是在Scores与临时表之间进行cross join.
此外,使用临时变量(@row,@pre)记录行号.
Tips:
- 通过
@pre与当前Score的比较,确定是否+1,需要注意mysql中的true/false为0/1); - 在
sql中,set/update语句中,=表示赋值;在set/update/select中,:=表示赋值,因此使用:=.
通过以上改进,mysql的运行效率得到了较大的提高.
PS:
如果您觉得我的文章对您有帮助,请关注我的微信公众号,谢谢!
LeetCode——Rank Scores的更多相关文章
- [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
做到这题时卡了不少时间,参考了别人的解法,觉得挺不错的,还挺巧妙. SELECT s2.Score,s1.Rank From ( SELECT S1.Score, COUNT(*) as Rank F ...
- 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 ...
- 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 ...
- [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]LeetCode178. 分数排名 | 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 ...
- sql -leetcode 178. Rank Scores
Score 很好得到: select Score from Scores order by Score desc; 要得到rank, 可以通过比较比当前Score 大的Score 的个数得到: sel ...
随机推荐
- Lnmp架构部署动态网站环境.2019-7-3-1.3
Php安装 一.安装准备 1.Php依赖包 [root@Lnmp tools]# yum install -y zlib libxml libjpeg freetype libpng gd curl ...
- 【hdu3311】Dig The Wells(斯坦纳树+dp)
传送门 题意: 给出\(n\)个重要点,还有其余\(m\)个点,\(p\)条边. 现在要在这\(n+m\)个点中挖几口水井,每个地方的费用为\(w_i\).连接边也有费用. 问使得这\(n\)个地点都 ...
- 初学JavaScript正则表达式(九)
分组:可以用 ( ) 来进行分组 一.Byron重复三次 Byron{3} --------- Byronnn 只是将紧挨着量词的字符重复 (Byron) ...
- nm U -l库的
nm U -l库的
- template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [code/leading], template m ...
- C# WF 第12节 Timer控件
本节内容: 1:Timer控件的简介 2:实例1 : 不停的弹出,恶意exe 3:实例2: :流水灯 4:实例3:给流水灯加上计时器和在规定的时间进行播放音乐 1:Timer控件的简介 2:实例1 ...
- Misc-不简单的压缩包
题目下载地址 https://ctf.bugku.com/files/e5a937a3985f5264a723bcbd0e062b0f/zip 友情连接同时也是网上看到的第一份关于这题的writeup ...
- 《滴滴自研分布式 NoSQL 数据库 Fusion 的演进之路》
SSD:采用闪存: 读的速度很快:写入数据时,因为需要通过加压的方式对存储单元进行电子填充,所以速度略慢:擦除速度最慢,擦除块的时间在ms级.在使用SSD的时,需要考虑到SSD的读写不平衡的特性. 滴 ...
- zz自动驾驶多传感器感知的探索1
Pony.ai 在多传感器感知上积累了很多的经验,尤其是今年年初在卡车上开始了新的尝试.我们有不同的传感器配置,以及不同的场景,对多传感器融合的一些新的挑战,有了更深刻的认识,今天把这些经验,总结一下 ...
- Python进阶-XVI 继承 单继承 多继承
一.初识继承 1.引入继承 class A(object): pass # 父类,基类,超类 class B: pass # 父类,基类,超类 class A_son(A, B): pass # 子类 ...
