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. node-odata: 基于 NodeJS 的 REST 框架

    该开源项目目前已被 OData 官网 (odata.org)收录 关于 node-odata node-odata 可以让你轻松创建 REST API, 并能使用 OData 协议的格式进行数据的查询 ...

  2. PHP 增删改查 import!!

    主页面 <h1>主页面family</h1> <table width="100%" border="1px" cellpaddi ...

  3. 【分享】IT产业中的三大定理(二) —— 安迪&比尔定理 (Andy and Bill's Law)

    摩尔定理给所有的计算机消费者带来一个希望,如果我今天嫌计算机太贵买不起,那么我等十八个月就可以用一半的价钱来买.要真是这样简单的话,计算机的销售量就上不去了.需要买计算机的人会多等几个月,已经有计算机 ...

  4. OpenMp并行提升时间为什么不是线性的?

    最近在研究OpenMp,写了一段代码,如下: #include<time.h> #include<stdio.h> #include<stdlib.h> #incl ...

  5. 19.allegro过孔设置[原创]

    一.根据线宽设置过孔 在规则管理器下 --- --- ---- --- --- 二.设置原点 法1: -- -- 法二: 然后鼠标点选 ---option栏目在哪? --- 三:过孔问题1 当一个过孔 ...

  6. JSON格式化 JSON美化 输出到html

    {"promotion_details":{"promotion_detail":[{"discount_fee":"22.20& ...

  7. h-index

    https://leetcode.com/problems/h-index/ https://leetcode.com/mockinterview/session/result/xjcpjlh/ 看了 ...

  8. poj 1159 (DP LCS)

    滚动数组 + LCS // File Name: 1159.cpp // Author: Missa_Chen // Created Time: 2013年07月08日 星期一 10时07分13秒 # ...

  9. IOS学习-报错误 Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

    环境:XCODE:5.0.2  IOS7模拟器 界面:使用storyboard 拖拽 简单应用:一个CoreData的CRUD用例. 界面如下图(一个UITableViewController 列表 ...

  10. ecshop显示所有分类树栏目

    1.找到 category.php 和goods.php 两个文件修改: $smarty->assign('categories', get_categories_tree(0)); // 分类 ...