178. Rank Scores

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    |
+-------+------+



Solution:

SELECT s.Score, (SELECT COUNT(DISTINCT Score) FROM Scores WHERE Score >= s.Score)Rank
FROM Scores s
ORDER BY Score DESC
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,419评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,793评论 0 23
  • 文/小漂亮v 昨天你放学归来,一进门,我就发现你的脸色不对。当时就想问问你是不是有什么事情发生。一转念,还是等你告...
    小漂亮v阅读 898评论 0 3
  • 你有过一次经历吗?在别人掉了东西后,拼命追悼上别人,只为把东西交到别人手中,让东西物归原主。不为别的,只为图一份安...
    展翅翱翔_3c21阅读 1,453评论 0 0
  • 学习结束,今天计划去湖北美术学院转一转,感受一下美院的学习环境,在路上出现了一个小意外,耽误了时间,结果让我目睹了...
    影子倒了阅读 319评论 3 8