设为首页 加入收藏

TOP

LeetCodeRankScores
2015-11-21 01:45:39 来源: 作者: 【 】 浏览:0
Tags:LeetCodeRankScores

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    |
+-------+------+
select a.score as Score, 
count(b.score) 
from (
    select distinct score 
    from Scores
)b, Scores a 
where b.score >= a.score 
group by a.score, a.id 
order by a.score desc;
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇数据库中的悲观锁和乐观锁详解 下一篇Pregel系统介绍

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: