当前位置:网站首页>leetcode MYSQL数据库题目178
leetcode MYSQL数据库题目178
2022-06-29 09:10:00 【水似冰】
178. Rank Scores
1、题目与答案
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no “holes” between ranks.
编写SQL查询对分数进行排序。如果两个分数相等,则两者应具有相同的排名。请注意,在相等之后,下一个排名数应该是下一个连续的整数值。换句话说,相等的分数并列。
Table:Scores
+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+For example, given the above
Scorestable, your query should generate the following report (order by highest score):
例如,给定上面的“Scores”表,您的查询应生成以下报告(按最高分数排序)
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+答案: select
Score,
(select count(distinct b.Score) from scores b where b.Score >= s.Score) as Rank
from scores s
order by Score desc
2、 知识点总结
验证步骤:
创建并插入表scorescreate table scores(
Id int NULL,
Score double NULL
);
insert into scores
values(1,3.5),(2,3.65),(3,4.00),(4,3.85),(5,4.00),(6,3.65);
执行答案可得:
+-------+------+
| Score | Rank |
+-------+------+
| 4 | 1 |
| 4 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.5 | 4 |
+-------+------+
6 rows in set (0.00 sec)知识点:
- 子查询与where筛选
select count( distinct id ) from table_name计算talbebname表中id不同的记录有多少条
边栏推荐
- General part: cognition, design and best practice of prototype design
- JS获取手机型号和系统版本
- Mac mysql数据库基本操作
- Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear
- The principle of session and cookie
- zabbix4.4配置监控服务器指标,以及图形页乱码解决
- Wechat applet realizes store function
- programing language
- Data governance: Metadata Management (Part 2)
- Western Polytechnic University, one of the "seven national defense schools", was attacked by overseas networks
猜你喜欢

Reading notes on how to connect the network - Web server request and response (IV)

Segmentation of Head and Neck Tumours Using Modified U-net

Gd32f4xx Ethernet Chip (ENC28J60) Drive transplantation

Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks

The principle of session and cookie

Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images

1424. diagonal traversal II

Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear

Do you know what BFD is? This article explains the principle and usage scenarios of BFD protocol in detail

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
随机推荐
Mysql配置主从数据库
A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
Uber 前安全主管面临欺诈指控,曾隐瞒数据泄露事件
Data governance: data standard management (Part III)
长安链GO语言智能合约环境搭建及使用
Data governance: Metadata Management (Part 2)
Idea auto completion
Set up lamp environment under cenos7
Wechat applet realizes store function
How to do unit test well
Surveiller l'utilisation du pool de connexion des sources de données
2020-09-23左右值 右值引用 std::move()
数据治理:数据治理在数据中台下的解决方案
LSM6DSL之SPI驱动
爱快安装或重置后,PC或手机端获取不到ip
长安链数据存储介绍及Mysql存储环境搭建
Data governance: the solution of data governance in the data Arena
General part: cognition, design and best practice of prototype design
Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages
Visual assist plug-in settings for UE4 vs