# Write your MySQL query statement below
178. 分数排名
表: Scores
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | score | decimal | +-------------+---------+ Id是该表的主键。 该表的每一行都包含了一场比赛的分数。Score是一个有两位小数点的浮点值。
编写 SQL 查询对分数进行排序。排名按以下规则计算:
按 score
降序返回结果表。
查询结果格式如下所示。
示例 1:
输入: Scores 表: +----+-------+ | id | score | +----+-------+ | 1 | 3.50 | | 2 | 3.65 | | 3 | 4.00 | | 4 | 3.85 | | 5 | 4.00 | | 6 | 3.65 | +----+-------+ 输出: +-------+------+ | score | rank | +-------+------+ | 4.00 | 1 | | 4.00 | 1 | | 3.85 | 2 | | 3.65 | 3 | | 3.65 | 3 | | 3.50 | 4 | +-------+------+
原站题解
pythondata 解法, 执行用时: 252 ms, 内存消耗: 61.4 MB, 提交时间: 2023-08-09 17:39:21
import pandas as pd def order_scores(scores: pd.DataFrame) -> pd.DataFrame: scores['rank'] = scores['score'].rank(method='dense', ascending=False) scores.sort_values('score', ascending=False, inplace=True) # 将第二列转换为整数类型 scores['rank'] = scores['rank'].astype(int) return scores[['score', 'rank']]
mysql 解法, 执行用时: 905 ms, 内存消耗: N/A, 提交时间: 2018-08-22 14:49:50
# Write your MySQL query statement below select Score, (select count(distinct Score) from Scores where Score >= s.Score) Rank from Scores s order by Score desc;