601.(Hard)体育馆的人流量
表:Stadium
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| visit_date | date |
| people | int |
+---------------+---------+
visit_date 是表的主键
每日人流量信息被记录在这三列信息中:序号 (id)、日期 (visit_date)、 人流量 (people)
每天只有一行记录,日期随着 id 的增加而增加
编写一个 SQL 查询以找出每行的人数大于或等于 100 且 id 连续的三行或更多行记录。
返回按 visit_date 升序排列的结果表。
查询结果格式如下所示。
Stadium table:
+------+------------+-----------+
| 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-09 | 188 |
+------+------------+-----------+
Result table:
+------+------------+-----------+
| id | visit_date | people |
+------+------------+-----------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+-----------+
id 为 5、6、7、8 的四行 id 连续,并且每行都有 >= 100 的人数记录。
请注意,即使第 7 行和第 8 行的 visit_date 不是连续的,输出也应当包含第 8 行,因为我们只需要考虑 id 连续的记录。
不输出 id 为 2 和 3 的行,因为至少需要三条 id 连续的记录。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/human-traffic-of-stadium
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Solution
1. 变量法
想复杂了,不过就当练习了。
with t1 as (
SELECT *, (people>=100) busy
FROM stadium
),t2 as (
SELECT id, visit_date, people, busy,
case
when @prev = busy then @slice := @slice
when (@prev := busy) is not null then @slice := @slice+1
end flag
from t1, (SELECT @prev:=null, @slice:=0) tmp
),t3 as (
select *, count(1) over (partition by flag) cnt from t2
)
SELECT id, visit_date, people from t3
where busy =1 and cnt>=3
order by 2
t1 计算 busy
t2 计算 flag(变量)
t3 计算 cnt(窗口函数)
id visit_date people busy flag cnt 1 2017-01-01 10 0 1 1 2 2017-01-02 109 1 2 2 3 2017-01-03 150 1 2 2 4 2017-01-04 99 0 3 1 5 2017-01-05 145 1 4 4 6 2017-01-06 1455 1 4 4 7 2017-01-07 199 1 4 4 8 2017-01-09 188 1 4 4
事实上flag列的计算可以直接用
(id - row_number() OVER(ORDER BY id))
结合WHERE people >= 100
一步到位,如👇🏻窗口函数法
2.窗口函数法
with t1 as (
SELECT *
,(id - row_number() OVER(ORDER BY id)) flag
FROM stadium
WHERE people >= 100
), t2 as (
SELECT *,count(1) over (partition by flag) cnt from t1
)
SELECT id, visit_date, people from t2
where cnt>=3 order by 2
Table Schema
Create table If Not Exists stadium (id int, visit_date DATE NULL, people int);
Truncate table stadium;
insert into stadium (id, visit_date, people) values ('1', '2017-01-01', '10');
insert into stadium (id, visit_date, people) values ('2', '2017-01-02', '109');
insert into stadium (id, visit_date, people) values ('3', '2017-01-03', '150');
insert into stadium (id, visit_date, people) values ('4', '2017-01-04', '99');
insert into stadium (id, visit_date, people) values ('5', '2017-01-05', '145');
insert into stadium (id, visit_date, people) values ('6', '2017-01-06', '1455');
insert into stadium (id, visit_date, people) values ('7', '2017-01-07', '199');
insert into stadium (id, visit_date, people) values ('8', '2017-01-09', '188');
-- case
Truncate table stadium;
insert into stadium (id, visit_date, people) values ('1', "2017-01-01", '10');
insert into stadium (id, visit_date, people) values ('2', "2017-01-02", '109');
insert into stadium (id, visit_date, people) values ('3', "2017-01-03", '150');
insert into stadium (id, visit_date, people) values ('4', "2017-01-04", '100');
Last updated
Was this helpful?