当前位置:网站首页>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不同的记录有多少条
边栏推荐
- Factory mode
- 2020-09-18 referer认证 url转义
- 数据仓库:金融/银行业的分层架构篇
- 2020-09-23左右值 右值引用 std::move()
- Slider validation code
- CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
- [technology development] development and design of alcohol tester solution
- 长安链GO语言智能合约编写与编译
- LeetCode刷题——泰波那契数列
- The former security director of Uber faced fraud allegations and had concealed data leakage incidents
猜你喜欢

# 《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)

Simplicity Studio无法识别新买的JLink v9解决方法

Student addition / deletion gaih

GD32F4xx 以太网芯片(enc28j60)驱动移植

ORA-01950 对表空间无权限

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

UE4 blueprint modify get a copy in array to reference

Making of simple addition calculator based on pyqt5 and QT Designer

Segmentation of Head and Neck Tumours Using Modified U-net

1424. diagonal traversal II
随机推荐
Do you know what BFD is? This article explains the principle and usage scenarios of BFD protocol in detail
IDEA自动补全
Cloud management platform: openstack architecture design and detailed interpretation
数据治理:数据治理在数据中台下的解决方案
linux下centos7中mysql5.7安装教程
数据治理:元数据管理(第二篇)
A 2.5D Cancer Segmentation for MRI Images Based on U-Net
Matlab tips (21) matrix analysis -- partial least squares regression
Idea debugging fails, reporting jdwp no transports initialized, jvmtierror=agent_ ERROR_ TRANSPORT_ LOAD(196)
微信小程序实现数据侦听器watch,包含销毁watch和子属性的watch
In the future of Business Intelligence BI, how do you view the ai+bi model?
LSM6DSL之SPI驱动
Reading notes on how to connect the network - Web server request and response (V)
股票炒股账号开户安全吗?是靠谱的吗?
Western Polytechnic University, one of the "seven national defense schools", was attacked by overseas networks
Wechat applet rewrites the page function to realize global logging
数据治理:数据标准管理(第三篇)
我想要股票开户优惠,怎么得到?还有,在线开户安全么?
装饰器模式的应用,包装ServletRequest,增加addParameter方法
zabbix4.4配置监控服务器指标,以及图形页乱码解决