当前位置:网站首页>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 .
边栏推荐
- Report redirect after authorized login on wechat official account_ The problem of wrong URI parameters
- The mystery of ID number
- leetcode day4 部门工资最高的员工
- Huawei shares in Nanjing core vision, laying out the solid-state laser radar chip field
- 你知道雨的类型有几种?
- In order to develop high-end photoresist, Jingrui Co., Ltd. invested 75million yuan to purchase SK Hynix ASML lithography machine
- C language cycle sentence strengthening exercises
- BLDC 6-step commutation simulink
- 【经验之谈】关于维修电子设备的几点建议和经验
- The United States will provide $25billion in subsidies to encourage chip manufacturers such as Intel to move back to production lines
猜你喜欢

11. Learn MySQL union operator

在矩池云快速安装torch-sparse、torch-geometric等包

远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作

Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation

Smart contract security - overflow vulnerability

Rust 入门指南(modules 和工程结构)

Business visualization - let your flowchart "run" (4. Actual business scenario test)

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

Using Baidu easydl to realize chef hat recognition of bright kitchen and stove

基于MATLAB的函数拟合
随机推荐
克服“看牙恐惧”,我们用技术改变行业
npm安装和卸载全局包
leetcode day5 删除重复的电子邮箱
[notes] Networking: Internet product managers change the world
Verification of chip power consumption performance: from dilemma to transcendence
Android-第十三节03xUtils-数据库框架(增删改查)详解
【笔记】《结网:互联网产品经理改变世界》
Rust 入门指南(crate 管理)
并发程序设计,你真的懂吗?
China's first chip stamp released: built-in 120um ultra-thin NFC chip
【笔记】《启示录》:产品经理的实践经验与反省清单
MySQL8 Encrypting InnoDB Tablespaces
企业级分布式爬虫框架入门
Cvpr19 - adjust reference dry goods bag of tricks for image classification with revolutionary neural network
初步学习函数(第3篇博客)
在矩池云快速安装torch-sparse、torch-geometric等包
英文翻译意大利语-批量英文翻译意大利语工具免费
投资35.45亿元!格力集团参与小米产业基金
亚马逊推出Amazon One手掌支付系统,非接触式掌静脉识别市场有望爆发
诺基亚扩大与英国电信的5G协议,将成其最大无线接入设备供应商