编写一个 SQL 查询以找出每行的人数大于或等于 100 且 id 连续的三行或更多行记录。
返回按 visit_date 升序排列的结果表。
链接:https://leetcode-cn.com/problems/human-traffic-of-stadium
用lead lag大法可以选出!但是要注意选出的有三个部分,头,尾,中!头是指后两个都>=100,中是指前一个和后一个都>=100,尾是指前两个>=100
select id, visit_date, people
from (
select *, lag(people, 1, 0) over() as `p1`, lag(people, 2, 0) over() as `p2`, lead(people, 1, 0) over() as `l1`, lead(people, 2, 0) over() as `l2`
from stadium
) t1
where (p1 >= 100 and p2 >= 100 and people >= 100)
or (l1 >= 100 and l2 >= 100 and people >= 100)
or (l1 >= 100 and p1 >= 100 and people >= 100)