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 Score, Rank from
(
select Score,
@rank := @rank + if(@prevSc=Score, 0, 1) as Rank,
@prevSc := Score
from (select @prevSc := null) x, (select @rank := 0) y,
(select * from Scores order by Score desc) z
) a;

  

LeetCode Database: Rank Scores的更多相关文章

  1. 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 ...

  2. [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 ...

  3. sql -leetcode 178. Rank Scores

    Score 很好得到: select Score from Scores order by Score desc; 要得到rank, 可以通过比较比当前Score 大的Score 的个数得到: sel ...

  4. [LeetCode] Rank Scores 分数排行

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

  5. [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 ...

  6. LeetCode——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. LeetCode Database题解

    175. Combine Two Tables 使用外连接即可. # Write your MySQL query statement below select FirstName, LastName ...

  9. 【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 ...

随机推荐

  1. 重新格式化hdfs系统的方法

    重新格式化hdfs系统的方法: (1)查看hdfs-ste.xml <span style="font-size:18px;"><property> < ...

  2. Myeclipse2014配置JSF环境

    首先创建一个普通的webproject,然后看官网教程喽 https://www.genuitec.com/products/myeclipse/learning-center/web/myeclip ...

  3. linux dsp 播放音频文件

    #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/ioc ...

  4. Linux内核等待队列

    在Linux驱动程序设计中,可以使用等待队列来实现进程的阻塞,等待队列可看作保存进程的容器,在阻塞进程时,将进程放入等待队列,当唤醒进程时,从等待等列中取出进程. Linux 2.6内核提供了如下关于 ...

  5. 笔记三、apache搭建gitweb【转】

    参考文章:     http://www.latelee.org/using-gnu-linux/ubuntu-apache-gitweb.html     http://blog.csdn.net/ ...

  6. 如何从List<T>中筛选符合条件的数据的集合或个数

    方法一:Linq ChannelList就是一个List类型的数据,IsOpen 是其元素的属性 channelCount = (from channel in DevicesManager.Inst ...

  7. Photoshop图层混合模式计算公式大全(转)

    混合模式可以将两个图层的色彩值紧密结合在一起,从而创造出大量的效果.在这些效果的背后实际是一些简单的数学公式在起作用.下面我将介绍photoshop cs2中所有混合模式的数学计算公式.另外还介绍了不 ...

  8. Gson解析POJO类中的泛型参数

    在开发Android与API交互的时候,使用Json格式传输,遇到了这样一个情况,返回数据格式POJO类如下: public class ApiResult<T> { private in ...

  9. hdu3333(线段树)

    区间更新,单点查询. hdu3333 #include <iostream> #include <stdio.h> #include <string.h> #inc ...

  10. HTML5_智能表单

    1.HTML5中为了方便排版,可以使from中的表单标签脱离from的嵌套.方法:from指定ID,所有表单标签均添加from=id属性. <form action="http://l ...