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. BayaiM__Oracle ASM操作管理

    BayaiM__Oracle ASM操作管理   BayaiM__Oracle ASM操作管理                                                      ...

  2. 无法打开“Visual Studio Code”,因为Apple无法检查其是否包含恶意软件。”的问题解决

    解决方法: 1.系统偏好设置==> 安全性与隐私 ===> 在下方允许就可以了. 2.一劳永逸 但是注意安全性 打开terminal 命令行工具输入命令:sudo spctl --mast ...

  3. 解压 Android 系统中的 system.img

    本篇文章讲解 system.img 是什么东西,以及它的打包和解包方式 system.img 是什么 system.img 是 Android 系统中用来存放系统文件的镜像 (image) ,文件格式 ...

  4. 初学JavaScript正则表达式(五)

    范围类 [ ] 例: 'a1b1c1s1v1d1f1'.replace(/[a-z]/g,'q') //q1q1q1q1q1q1q1 将a-z范围内的字符替换为q 'a1b1c1s1v1d1f1AKS ...

  5. 第50 课C++对象模型分析——成员变量(上)

    C++对象模型,其实就是C++中的对象在内存中是如何排布的.C++中的对象包含了成员变量和成员函数,其实就是研究C++中的类对象它的成员变量和成员函数在内存中是如何排布的. 回归本质class 是一种 ...

  6. JWT签名算法中HS256和RS256有什么区别 转载

    JWT签名算法中,一般有两个选择,一个采用HS256,另外一个就是采用RS256. 签名实际上是一个加密的过程,生成一段标识(也是JWT的一部分)作为接收方验证信息是否被篡改的依据. RS256 (采 ...

  7. python3.5.3rc1学习八:文件打包

    from cx_Freeze import setup, Executable setup(name='test to exe', version = '0.1', description='test ...

  8. 为什么MySQL数据库要用B+树存储索引?

    问题:MySQL中存储索引用到的数据结构是B+树,B+树的查询时间跟树的高度有关,是log(n),如果用hash存储,那么查询时间是O(1).既然hash比B+树更快,为什么mysql用B+树来存储索 ...

  9. ubuntu 查看版本

    cat /etc/proc 可以查看是16.04还是18.04

  10. 使用JaCoCo统计单元测试代码覆盖率

    1 JaCoCo介绍 JaCoCo是EclEmma团队基于多年覆盖率库使用经验总结而研发的一个开源的Java代码覆盖率库. 2 JaCoCo覆盖率计数器 JaCoCo 包含了多种尺度的覆盖率计数器(C ...