站点怎么建网页,商丘家具网站建设,百度域名ip是多少,谷歌没收录网站主页 301重定向文章目录1. 题目2. 解题1. 题目
X 市建了一个新的体育馆#xff0c;每日人流量信息被记录在这三列信息中#xff1a;序号 (id)、日期 (visit_date)、 人流量 (people)。
请编写一个查询语句#xff0c;找出人流量的高峰期。高峰期时#xff0c;至少连续三行记录中的人流量…
文章目录1. 题目2. 解题1. 题目
X 市建了一个新的体育馆每日人流量信息被记录在这三列信息中序号 (id)、日期 (visit_date)、 人流量 (people)。
请编写一个查询语句找出人流量的高峰期。高峰期时至少连续三行记录中的人流量不少于100。
例如表 stadium
-----------------------------
| id | visit_date | people |
-----------------------------
| 1 | 2017-01-01 | 10 |
| 2 | 2017-01-02 | 109 |
| 3 | 2017-01-03 | 150 |
| 4 | 2017-01-04 | 99 |
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-08 | 188 |
-----------------------------对于上面的示例数据输出为
-----------------------------
| id | visit_date | people |
-----------------------------
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-08 | 188 |
-----------------------------提示 每天只有一行记录日期随着 id 的增加而增加。 来源力扣LeetCode 链接https://leetcode-cn.com/problems/human-traffic-of-stadium 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 2. 解题
使用 id 跟排序行号做差连续的做差是一样的
select stadium.*, id - cast(row_number() over(partition by people 100) as signed) rnk
from stadium
where people 100{headers: [id, visit_date, people, rnk],
values: [
[2, 2017-01-02, 109, 1],
[3, 2017-01-03, 150, 1],
[5, 2017-01-05, 145, 2],
[6, 2017-01-06, 1455, 2],
[7, 2017-01-07, 199, 2],
[8, 2017-01-08, 188, 2]]}再套一层算出 rnk 一样的有多少个
select id, visit_date, people,count(*) over(partition by rnk) cntfrom
(select stadium.*, id - cast(row_number() over(partition by people 100) as signed) rnkfrom stadiumwhere people 100
) t{headers: [id, visit_date, people, cnt],
values: [
[2, 2017-01-02, 109, 2],
[3, 2017-01-03, 150, 2],
[5, 2017-01-05, 145, 4],
[6, 2017-01-06, 1455, 4],
[7, 2017-01-07, 199, 4],
[8, 2017-01-08, 188, 4]]}最后筛选 cnt 3 的
# Write your MySQL query statement below
select id, visit_date, people
from
(select id, visit_date, people,count(*) over(partition by rnk) cntfrom(select stadium.*, id - cast(row_number() over(partition by people 100) as signed) rnkfrom stadiumwhere people 100) t
) t
where cnt 3或者 3表连接
# Write your MySQL query statement below
select distinct a.*
from stadium a, stadium b, stadium c
where ((b.id a.id1 and c.id b.id1) or(c.id b.id1 and a.id c.id1) or(a.id c.id1 and b.id a.id1))and a.people100 and b.people100 and c.people100
order by a.id我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号Michael阿明一起加油、一起学习进步