做到这题时卡了不少时间,参考了别人的解法,觉得挺不错的,还挺巧妙。

SELECT s2.Score,s1.Rank From
(
SELECT S1.Score, COUNT(*) as Rank
FROM
(SELECT DISTINCT Score from Scores) as S1,
(SELECT DISTINCT Score from Scores) as S2
Where S1.Score<=S2.Score
Group By S1.Score
) s1 ,Scores s2
WHERE s1.Score = s2.Score Order BY s2.Score desc

简单回顾一下题目,

+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
要利用上面的表,达到
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
这种效果,一般情况下用存储过程会比较方便地得到,但既然一句SQL可以解决,便学习一下罢。贴出的解法中的子查询起了关键作用
SELECT S1.Score, COUNT(*) as Rank
FROM
(SELECT DISTINCT Score from Scores) as S1,
(SELECT DISTINCT Score from Scores) as S2
Where S1.Score<=S2.Score
Group By S1.Score
)

(该子查询的外层查询仅仅是一个连接操作然后排序,比较简明,故不赘述)

这个子查询如果单独执行,可以得到下面的结果:

可以很清楚地看到,得到了一种“排序”效果,那么为什么会得到这种效果呢,我们可以进一步拆解:

SELECT S1.Score , Count(*) S2.Score as Rank
FROM
(SELECT DISTINCT Score from Scores) as S1,
(SELECT DISTINCT Score from Scores) as S2
Where S1.Score<=S2.Score

这个子查询我们对其稍作修改,以便更直观地观察其结果,如果单独执行,得到如下结果:

我们可以看到,S1.Score与S2.Score进行<=操作之后,得到了这样的结果,我们知道,俩字段的比较操作,会将左边表的字段依次与右边表的字段值逐个比较,这里我们可以看到,对于左表的3.5来说,右边表有四个值满足<=的比较条件,于是,左边表的3.5会出现4次,而右边的值则是满足比较条件的值。

于是,当所有的字段比较完成之后,我们可以总结出,因为每一个字段在右边去比较的时候,总会将比其小的值筛一个出去(因为是Distinct操作,不会存在重复值),这么一来,左边表的字段的值每迭代到一个更大的值,其在结果集中出现的次数一定比上一个次小值少一次,最后将其做Group By操作,这么一来,就得到了“排序”效果。

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

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

  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. 理解Java的GC日志

    分析如下GC日志:[GC [PSYoungGen: 9216K->1024K(9216K)] 1246196K->1246220K(1287040K), 0.2398360 secs] [ ...

  2. Sicily 1790. Single Round Match

    高进度求余 或者 将一个数奇位上的数字与偶位上的数字分别加起来,再求它们的差,如果这个差是11的倍数(包括0),那么,原来这个数就一定能被11整除. #include <iostream> ...

  3. 使用jquery插件uploadify上传文件的方法与疑问

    我是学生一枚,专业也不是计算机,但又要用到很多相关技术,所以在技术基础不牢靠的情况下,硬着头皮在做.最近在做一个小项目需要上传图片,而且是需要用ajax的方式.但是利用jquery的ajax方法总会有 ...

  4. java学习:AWT组件和事件处理的笔记(1)--Frame

    1.java的抽象窗口工具包(AWT)中包含了许多类来支持GUI设计2.AWT由java的java.awt包提供3.再进行GUI编程时,要理解:容器类(Container),组件(component) ...

  5. Qt4.7文档翻译:Qt样式单参考,Qt Style Sheets Reference(超长,超全)

    内容目录 Qt样式单参考 可进行样式设置的部件列表 属性列表 图标列表 属性类型列表 伪状态列表 子控件列表 Qt样式单参考 Qt样式单支持各种属性.伪状态和子控件,这样使得妳能够自行设计部件的外观. ...

  6. 转:Memcached常用命令及使用说明

    一.存储命令 存储命令的格式: 1 2 <command name> <key> <flags> <exptime> <bytes> < ...

  7. (三)Boost库之字符串处理

    (三)Boost库之字符串处理 字符串处理一直是c/c++的弱项,string_algo库很好的弥补了这一点. string_algo 库算法命名规则: 前缀i    : 有这个前缀表名算法的大小写不 ...

  8. 国内5款优秀的WEB前端框架

    1. JX(腾讯) 官网地址:http://alloyteam.github.io/JX/#home JX 是一个类似 Google Closure Library 的 Web 前端开发框架,服务于 ...

  9. 网易云课堂_C++程序设计入门(上)_第5单元:万类霜天竞自由 – 对象和类的更多内容_第5单元作业【4】 - 在线编程(难度:难)

    第5单元作业[4] - 在线编程(难度:难) 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提交答案,系 ...

  10. MySQL --概述--

    Mysql是最流行的关系型数据库管理,在Web应用方面MySQL是最好的RDBMS:关系数据库管理系统 什么是数据库? 数据库(Database)是按照数据结构来组织,存储和管理数据的仓库. 每个数据 ...