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

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. linux学习笔记之shell

    本文参考:shell脚本学习指南 本文阅读前提为:知道shell指令,但不知道如何完成一个自动化的shell脚本. 因为编辑本文时,作者也是一个新手.所以,在一些理论上,可能存在错误.如果存在错误,希 ...

  2. linux学习笔记之硬盘分区

    引用:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/03/2997098.html PS:本文仅对知识点作总结.详情请参考原文. 首先 ...

  3. Intuit Quicken Home & Business 2016(Manage your business and personal finances)

    Quicken Home & Business 2016 - Manage your business and personal finances all in one place. Cate ...

  4. 可以供MFC调用的,QT实现的DLL(qtwinmigrate实现)

    MFC和QT的消息循环机制不同,所以,要让QT写的DLL可以供MFC调用,要做一点特殊的处理 #include <qmfcapp.h> #include <qwinwidget.h& ...

  5. linux上应用随机启动

    这是个go项目,其他的可以参考. 首先要有个脚本比如demo #!/bin/bash # # etcd This shell script takes care of starting and sto ...

  6. sysstat服务负载统计,如CPU占有率,网络使用率,磁盘速度

    sysstat服务负载统计,如CPU占有率,网络使用率,磁盘速度

  7. 【LeetCode练习题】Unique Paths

    Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...

  8. Z.Studio高级成衣定制(双井店)价格,地址(图)-北京-大众点评网

    Z.Studio高级成衣定制(双井店)价格,地址(图)-北京-大众点评网 Z.Studio高级成衣定制(双井店)

  9. #include <stack>

    1 pop(); 出栈 2 push(); 入栈 3 size(); 返回栈中元素个数 4 top(); 返回栈顶元素 使用栈,把十进制转换为二进制 #include <iostream> ...

  10. hdu 2571 命运(dp)

    Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了! 可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个 ...