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/false0/1);
  • sql中,set/update语句中,=表示赋值;在set/update/select中,:=表示赋值,因此使用:=.

通过以上改进,mysql的运行效率得到了较大的提高.

PS:

如果您觉得我的文章对您有帮助,请关注我的微信公众号,谢谢!

LeetCode——Rank Scores的更多相关文章

  1. [LeetCode] Rank Scores 分数排行

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  2. [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 ...

  3. LeetCode:Rank Scores

    做到这题时卡了不少时间,参考了别人的解法,觉得挺不错的,还挺巧妙. SELECT s2.Score,s1.Rank From ( SELECT S1.Score, COUNT(*) as Rank F ...

  4. 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 ...

  5. 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 ...

  6. [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 ...

  7. [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 ...

  8. 【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 ...

  9. sql -leetcode 178. Rank Scores

    Score 很好得到: select Score from Scores order by Score desc; 要得到rank, 可以通过比较比当前Score 大的Score 的个数得到: sel ...

随机推荐

  1. Go切片去掉重复元素

    1.Go切片去掉重复元素 如果传入的是string类型: //slice去重 func removeRepByMap(slc []string) []string { result := []stri ...

  2. Python—编码与解码(encode()和decode())

    编码与解码 decode英文意思是解码,encode英文原意是编码. Python 里面的编码和解码也就是 unicode 和 str 这两种形式的相互转化.编码是 unicode -> str ...

  3. .net core使用百度webupload上传图片

    后端代码: /// <summary> /// 图片上传,上传路径: "/Uploads/Images/" + folder + "/" + sav ...

  4. Falling back to java on path. This behavior is deprecated

    windows启动elasticsearch报错:warning: Falling back to java on path. This behavior is deprecated. Specify ...

  5. Shell命令-搜索文件或目录之which、find

    文件及内容处理 - which.find 1. which:查找二进制命令,按环境变量PATH路径查找 which命令的功能说明 which 命令用于查找文件.which 指令会在环境变量 $PATH ...

  6. SpringCloud学习笔记(九、SpringCloud Stream)

    目录: 什么是SpringCloud Stream 如何使用SpringCloud Stream 消息分流 什么是SpringCloud Stream: SpringCloud Stream是一个用于 ...

  7. mysql 导入sql文件的几种形式

    1.没有登陆mysql的时候以文件的形式导入mysql数据 在students.sql文件中加入这些语句 create table t_student( id int primary key auto ...

  8. MySQL学习笔记6——备份与恢复

    备份与恢复 备份与恢复 数据库-->sql:备份 sql-->数据库:恢复 1.数据库导出SQL脚本 >mysqldump -u用户名 -p密码 数据库名>生成的脚本文件路径 ...

  9. java中的转义字符(遇到再进一步总结)

    一.常见的转义字符转移字符对应的英文是escape character , 转义字符串(Escape Sequence)字母前面加上捺斜线""来表示常见的那些不能显示的ASCII字 ...

  10. thinkphp的运行

    打开cmd切换到www目录下运行think E:\wamp64\www>php think run ThinkPHP Development server is started On <h ...