面试题:分数排名
難度:中等
寫一段SQL語句查詢分數排名情況。如果兩個分數相等,則排名相同。
+----+-------+ | 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 a.Score, (select count(distinct Score) from Scores where Score>=a.Score) as Rank from Scores a order by a.Score desc?
轉載于:https://www.cnblogs.com/wan1976/p/4762843.html
總結
- 上一篇: bnu 34986 Football o
- 下一篇: 异步读写数据流