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 ...
随机推荐
- nginx在centos下的安装
第一步:打开浏览器下载,再上传到centOS系统中 http://nginx.org/download/ 或者在 centOS系统输入: wget http://nginx.org/download/ ...
- pdfium ppm demo
#include "fpdfview.h" #include <iostream> #include <string> #include <strin ...
- read_sql_query, def read_sql_table
read_sql_query, read_sql_table def read_sql_query(sql, con, index_col=None, coerce_float=True, param ...
- jQuery中的工具(十)
1. jQuery.each(object, [callback]), 通用遍历方法,可用于遍历对象和数组 不同于遍历 jQuery 对象的 $().each() 方法,此方法可用于遍历任何对象.回调 ...
- linux-部署2
gunicorn+supervisor 1.gunicorn 安装: pip3 install gunicorn 配置: 两种方式:命令和文件,因为配置项比较多,所以放在文件里,启动时指明配置文件即可 ...
- 安装Rtools
1.好多工具需要安装Rtools install.packages("installr") install.packages("stringr") ###依赖包 ...
- Paper | Learning convolutional networks for content-weighted image compression
目录 摘要 故事要点 模型训练 发表在2018年CVPR. 以下对于一些专业术语的翻译可能有些问题. 摘要 有损压缩是一个优化问题,其优化目标是率失真,优化对象是编码器.量化器和解码器(同时优化). ...
- Spring Cloud Alibaba Sentinel对RestTemplate的支持
Spring Cloud Alibaba Sentinel 支持对 RestTemplate 的服务调用使用 Sentinel 进行保护,在构造 RestTemplate bean的时候需要加上 @S ...
- mysql-新增数据库
一.新增数据库 1.检查mysql 新增数据库之前,先检查是否安装了数据库,本次我们使用的是mysql数据库,检查是否安装mysql直接使用 mysql --version即可: 显示了mysql的版 ...
- Jsp自学1
jsp学习之初,我是用记事本(EditPlus)来进行编辑的,那么写好的jsp文件如何执行看到效果呢?不像html文件可以直接用浏览器打开,jsp文件需要先进行编译器的编译才能执行,而Tomcat就可 ...
