当前位置:网站首页>LeetCode 178. Score ranking (MySQL)
LeetCode 178. Score ranking (MySQL)
2022-06-13 03:29:00 【TRX1024】
subject
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`
Their thinking :
External queries are arranged in descending order of scores , The sub query finds the number of scores greater than or equal to the current score , This number is the rank of the current score , If the same score and rank , use distinct duplicate removal .
Code :
select
Score,
(
select
count(distinct Score)
from
Scores
where
Score >= a.Score
) as `Rank`
from
Scores a
order by
Score desc
Expand :
mysql There are functions that provide automatic sorting in :dense_rank() over(),rank() over(),row_num() over().
Please move to the next step for specific usage and differences :mysql AutoSort function dense_rank() over()、rank() over()、row_num() over() Usage and difference
边栏推荐
- P1048 [noip2005 popularization group] Drug collection
- C method parameter: out
- Simulink代码生成: 查表模块及其代码
- Brew tool - "fatal: could not resolve head to a revision" error resolution
- Configuration and practice of shardingsphere JDBC sub database separation of read and write
- Azure SQL db/dw series (10) -- re understanding the query store (3) -- configuring the query store
- The use of curl in PHP
- C语言程序设计——从键盘任意输入一个字符串,计算其实际字符个数并打印输出,要求不能使用字符串处理函数strlen(),使用自定义子函数Mystrlen()实现计算字符个数的功能。
- Solution of Kitti data set unable to download
- Time processing class in PHP
猜你喜欢
Patrick Pichette, partner of inovia, former chief financial officer of Google and current chairman of twitter, joined the board of directors of neo4j
[JVM series 8] overview of JVM knowledge points
[JVM series 4] common JVM commands
[JVM Series 7] garbage collector
Yolov5 face+tensorrt: deployment based on win10+tensorrt8.2+vs2019
Video playback has repeatedly broken 1000w+, how to use the second dimension to create a popular model in Kwai
[azure data platform] ETL tool (6) -- re understanding azure data factory
Cross border M & a database: SDC cross border database, Thomson database, A-share listed company M & a database and other multi index data (4w+)
2021-08-30 distributed cluster
(9) Explain broadcasting mechanism in detail
随机推荐
Graph data modeling tool
Neil eifrem, CEO of neo4j, interprets the chart data platform and leads the development of database in the next decade
Spark Foundation
This article takes you to learn DDD, basic introduction
Loading process of [JVM series 3] classes
DDL operation table
ONNX+TensorRT+YoloV5:基于trt+onnx得yolov5部署1
Summary of the latest rail transit (Subway + bus) stops and routes in key cities in China (II)
Three ways to start WPF project
Understanding the ongdb open source map data foundation from the development of MariaDB
Use of compact, extract and list functions in PHP
MySQL group commit
Isolation level, unreal read, gap lock, next key lock
Pollution discharge fees of listed companies 2010-2020 & environmental disclosure level of heavy pollution industry - original data and calculation results
Several functions in YAF framework controller
MySQL learning summary 8: addition, deletion and modification of data processing
2-year experience summary to tell you how to do a good job in project management
Application scenarios of large arrows in Scala
Two Chinese vector map data with map review number
Yolov5 face+tensorrt: deployment based on win10+tensorrt8.2+vs2019