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

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. VS2003.NET在文件中查找卡死

    不知怎么的,安装vs2003后,一点查找就卡死. 修复方法:修改devenv.exe的兼容性配置,勾选“禁用视觉主题”! 说实话,还真不知道这两者有什么关系?

  2. php非递归无限级分类.

    项目需要.递归无限级分类效率实在太低.理了半天思路写的. 分类越多效率越高. /** * 单次循环返回无限极分类嵌套 * @param array $data 操作的数组 * @param strin ...

  3. 获取mssqlserver与access数据库插入的当前行的id

    //mssqlserver public static int GetInsertId(string sql) { try { SqlCommand cmd = new SqlCommand(); u ...

  4. webStorm 列编辑

    webStorm可以像Sublime一样使用列编辑,只是区别在于webStorm只可以编辑连续列表. 按住alt键鼠标选择一列,然后输入文字就会编辑多行,这个功能很赞,比较实用

  5. python运维开发(十一)----线程、进程、协程

    内容目录: 线程 基本使用 线程锁 自定义线程池 进程 基本使用 进程锁 进程数据共享 进程池 协程 线程 线程使用的两种方式,一种为我们直接调用thread模块上的方法,另一种我们自定义方式 方式一 ...

  6. ImportError: cannot import name webdriver问题解决

    安装完selenium之后,发现根本无法使用,一运行代码,就报ImportError: cannot import name webdriver错误 于是各种FQ查找解决方法,查到方法如下: 在当前目 ...

  7. SQL Server 影响dbcc checkdb的 8 种因素

    第一种: 数据库的大小. 第二种: IO系统的速度. 第三种: 当前CPU的负荷. 第四种: 当前数据库的并发修改量.因为并发修改量越大维护数据库快照的成本就越高,dbcc 的过程中要创建快照,所以. ...

  8. 修改IE8搜索框为指定搜索引擎,如CSDN、百度知道等

    1.运行regedit打开注册表编辑器2.找到\HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\SearchScopes\3.添加新搜索项 ...

  9. MFC窗口的父子关系和层级关系

    一直对窗口之间的关系有些混乱,遇到需要指定父窗口的函数时常常要考虑很久,究竟父窗口是哪个窗口,遂上网查资料,略有所悟,简记如下: 对话框中的所有控件(比如Button等)都是其子窗口.        ...

  10. GPS功能:百度路书自定义【轨迹回放】

    如题所述:百度的编辑界面很直观,修改后就可以运行,地址:http://developer.baidu.com/map/jsdemo.htm#c2_8: 因为同事研究了一下午结果都没搞出来,他copy百 ...