SQL 查询排名,相同数据排名相同

Table_1:

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

答:

select a.Score as Score,
(select count(distint b.Score) from Table_1 b where b.Score >= a.Score) as Rank
from Tabel_1 a
order by a.Score DESC;

内容解释:

  • 先通过 where b.Score >= a.Score 把符合条件的筛选出来,也就是把当前分数和所有分数做对比,只把前面(大于)的部分拿出来,去重后就是排名

函数解释:

  • DESC: 降序排列
  • distint :去重

猜你喜欢

转载自blog.csdn.net/weixin_44355591/article/details/105809849