当前位置:网站首页>Leetcode Day1 score ranking
Leetcode Day1 score ranking
2022-07-28 19:43:00 【wyqgg123】
Mysql
178. Score ranking
Medium difficulty
SQL framework
Write a SQL Query to achieve score ranking .
If two scores are the same , Then two scores rank (Rank) identical . Please note that , The next rank after bisection should be the next consecutive integer value . let me put it another way , There should not be between ranks “ interval ”.
+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
for example , According to the... Given above Scores surface , Your query should return ( Rank by score from high to low ):
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
** Important note :** about MySQL Solution , If you want to escape a reserved word used as a column name , You can use apostrophes before and after keywords . for example Rank
Answer key 1: Special window functions dense_rank
select `Score`,
dense_rank() over (Order by Score desc) as `Rank`
from Scores;
Running results :

dense_rank Lines with parallel ranks in functions will not occupy the position of the next name , That is to say 1、1、2、3、3、4. Rank like this .
Expand :
mysql Four sorting functions :
Example :
select `Score`,
dense_rank() over (Order by Score desc) as `Rank` ,
rank() over (Order by Score desc) as `Rank1`,
row_number() over (Order by Score desc) as `Rank2`
from Scores;
Running results :

According to the running results :
1、rank function : result 1、1、3、4、4、6 That is, if there is a row with parallel ranking , Will occupy the next place .
2、dense_rank function : result :1、1、2、3、3、4, That is, if there is a row with parallel ranking , Don't occupy the next place .
3、row_number function : result 1、2、3、4、5、6, That is, regardless of the parallel ranking .
The fourth sorting function is :
Example :
select `Score`,
ntile(1) over (Order by Score desc) as `Rank1`,
ntile(2) over (Order by Score desc) as `Rank2`,
ntile(3) over (Order by Score desc) as `Rank3`,
ntile(4) over (Order by Score desc) as `Rank4`,
ntile(5) over (Order by Score desc) as `Rank5`,
ntile(6) over (Order by Score desc) as `Rank6`,
ntile(7) over (Order by Score desc) as `Rank7`
from Scores;
result :

ntile(1) : result 1、1、1、1、1、1
ntile(2) : result 1、1、1、2、2、2
ntile(3) : result 1、1、2、2、3、3
ntile(4) : result 1、1、2、2、3、4
ntile(5) : result 1、1、2、3、4、5
ntile(6) : result 1、2、3、4、5、6
ntile(7) : result 1、2、3、4、5、6
According to the running results nitile It is sorted by groups , When the parameter is, it is sorted in groups .
Explanation 2 :
Example
select a.Score as Score,
(select count(distinct b.Score) from Scores as b where b.Score>=a.score)as `Rank`
from Scores as a
order by a.Score desc;
Reading :
The sub query will retrieve the total number of all scores greater than or equal to the current score , This is the rank that the score should have , Take this question as an example :
score : 4.00 — rank :1 Greater than or equal to 4 There are two of them 4 After the weight is removed, it is 1 .
score : 4.00 — rank: 1 Greater than or equal to 4 There are two of them 4 After the weight is removed, it is 1 .
score : 3.85 — rank: 2 Greater than or equal to 3.85 There are 3 individual 4、4、3.85 After the weight is removed, it is 2 .
score : 3.65 — rank : 3 Greater than or equal to 3.65 There are 5 individual 4、4、3.85、3.65、3.65 After the weight is removed, it is 3 .
score : 3.65 — rank : 3 Greater than or equal to 3.65 There are 5 individual 4、4、3.85、3.65、3.65 After the weight is removed, it is 3 .
score : 3.50 — rank : 4 Greater than or equal to 3.65 There are 5 individual 4、4、3.85、3.65、3.65、3.50 After the weight is removed, it is 4 .
边栏推荐
- How navicate modifies the database name
- Rust Getting Started Guide (modules and engineering structures)
- Redis master-slave architecture (how to calculate sizeof function)
- adb remount of the / superblock failed: Permission denied
- BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
- English article translation - English article translation software - free batch translation
- 云计算笔记part.1——系统管理
- WPF 实现带蒙版的 MessageBox 消息提示框
- leetcode day3 查找重复的电子邮箱
- Convertible bond concept table x notation gives you a convenient and fast experience!
猜你喜欢

Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)

Rust Getting Started Guide (rustup, cargo)

BLDC 6-step commutation simulink

App自动化测试是怎么实现H5测试的

彻底理解位运算——与(&)、非(~)、或(|)、异或(^)

11. Learn MySQL union operator

MATLAB实现的图像分割之边缘检测和连接

基于MATLAB的函数拟合

NetCoreAPI操作Excel表格

Cell review: single cell methods in human microbiome research
随机推荐
Investment of 3.545 billion yuan! Gree Group participates in Xiaomi industry fund
This customized keyboard turns me on~
2022年全国最新消防设施操作员(中级消防设施操作员)题库及答案
The opening price soared by 215%! Domestic signal chain chip enterprise Xinhai Technology landed on the scientific innovation board
Search problems and technologies
source insight项目导入和使用教程
Germany and Portugal have announced that they will not disable Huawei 5g equipment, but Germany will set strict restrictions!
Force buckle 1331. Array serial number conversion
使用SaltStack自动化部署Zabbix
Serial port receiving application ring buffer
这个客制化键盘,秀翻我了~
MATLAB实现的图像分割之边缘检测和连接
英文翻译西班牙语-批量英文翻译西班牙工具免费
NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!
Rust Getting Started Guide (rustup, cargo)
The United States will provide $25billion in subsidies to encourage chip manufacturers such as Intel to move back to production lines
NPM installing and uninstalling global packages
以数字化转型为契机,3C企业如何通过SRM供应商云协同平台实现高效协同?
Leetcode day2 连续出现的数字
Nokia expands its 5g agreement with BT and will become its largest wireless access device supplier