LeetCode:Rank Scores
做到这题时卡了不少时间,参考了别人的解法,觉得挺不错的,还挺巧妙。
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的更多相关文章
- [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
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- 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 ...
随机推荐
- css 兼容小三角
<!DOCTYPE><html ><head><meta http-equiv="Content-Type" content=" ...
- python虚拟环境Virtualenvwrapper无法升降级虚拟环境软件解决方法
virtualenv用于创建独立的Python环境,多个Python相互独立,互不影响,它能够在没有权限的情况下安装新套件,不同应用可以使用不同的套件版本,套件升级不影响其他应用. Virtaulen ...
- JS面向对象编程之:封装、继承、多态
最近在实习公司写代码,被隔壁的哥们吐槽说,代码写的没有一点艺术.为了让我的代码多点艺术,我就重新温故了<javascript高级程序设计>(其中几章),然后又看了<javascrip ...
- SQL Server 内存管理在64位时代的改变
64位机上 地址空间比以前大了去了.它引起的改变多了去了 1.MemToLeave这个词不存在了.因为SQL Server以不再做这种预留空间的事了,也就是说multiple page 想用多少就用 ...
- MYSQL 表分区的 3 方法
背景知识: 表分区是把逻辑上同一范围的数据保存到同一个文件中,就和超市一样,把同类商品放在同一个区域,把不同的商品放在不同的地方.不同的是超市中 是根据用途分类的,表分区是根据它的取值区间来分的. 分 ...
- VirtualBox 上安装CentOS 6.5
目标:1.在VirtualBox中安装CentOS2.配置虚拟机网络,实现: a.主机联网后,宿机能够通过主机上网 b.不管主机联网与否,主机都能SSH登录宿机,并且主宿机能互相传送文件 ...
- shell基础——创建临时文件
(1)当前时间命名临时文件: 按照一定的格式获取当前时间:date +%y-%m-%d_%H:%M:%S #generate the file name tmpfile=`date +%y-%m-%d ...
- create custom launcher icon 细节介绍
create custom launcher icon 是创建你的Android app的图标 点击下一步的时候,出现的界面就是创建你的Android的图标 Foreground: ” Foregro ...
- Jquery基础之动画操作
Jquery的动画效果能够轻松的为网页添加动画效果,为网页带来了新的活力. 一.show()方法和hide()方法 show()方法和hide()方法是jquery中最基本的动画方法.使用show() ...
- Can't create a new thread (errno 11); if you are not out of available memory, you can consult the manual for a possible OS-dependent bug
解决方案: http://www.javatang.com/archives/2013/06/19/2701909.html