baby123

编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同

+----+-------+
| Id | Score |
+----+-------+
| 1  | 3.50  |
| 2  | 3.65  |
| 3  | 4.00  |
| 4  | 3.85  |
| 5  | 4.00  |
| 6  | 3.65  |
+----+-------+

例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):

+-------+------+
| Score | Rank |
+-------+------+
| 4.00  | 1    |
| 4.00  | 1    |
| 3.85  | 2    |
| 3.65  | 3    |
| 3.65  | 3    |
| 3.50  | 4    |
+-------+------+
select s1.Score,(select count(distinct score) from scores where score >=s1.score) as Rank from Scores s1 order by Score desc

 

分类:

技术点:

相关文章:

  • 2021-11-18
  • 2021-12-14
  • 2021-06-05
  • 2022-01-04
  • 2021-10-30
  • 2021-12-17
  • 2021-09-07
猜你喜欢
  • 2021-06-19
  • 2021-11-30
  • 2021-09-13
  • 2021-07-09
  • 2021-12-01
  • 2021-11-04
相关资源
相似解决方案