X 市建了一个新的体育馆,每日人流量信息被记录在这三列信息中:序号 (id)、日期 (date)、 人流量 (people)。
请编写一个查询语句,找出高峰期时段,要求连续三天及以上,并且每天人流量均不少于100。
例如,表 stadium:
|------|------------|-----------|

id 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 date people
5 2017-01-05 145
6 2017-01-06 1455
7 2017-01-07 199
8 2017-01-08 188
------ ------------ -----------

Note:
每天只有一行记录,日期随着 id 的增加而增加。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--方法一:
SELECT A.id, to_char(A.visit_date, 'yyyy-MM-dd') AS visit_date, A.people
FROM stadium A
WHERE A.people >= 100
AND ((A.id - 1 IN ( SELECT id FROM stadium WHERE people >= 100 )
AND A.id - 2 IN ( SELECT id FROM stadium WHERE people >= 100 ))
OR (A.id - 1 IN ( SELECT id FROM stadium WHERE people >= 100 )
AND A.id | 1 IN ( SELECT id FROM stadium WHERE people >= 100 ))
OR (A.id | 1 IN ( SELECT id FROM stadium WHERE people >= 100 )
AND A.id | 2 IN ( SELECT id FROM stadium WHERE people >= 100 ))
)
ORDER BY A.id
--方法二:使用分析函数
WITH tmp AS (
SELECT id,last_id,next_id FROM (
select people,LAG(id,1,null)over(order by id) last_id,id,LEAD(id,1,null)over(order by id) next_id
from stadium WHERE people >=100
) A
WHERE (A.id=A.last_id | 1 AND A.id=A.next_id - 1)
)
SELECT id,SUBSTR(visit_date,1,10) visit_date,people FROM stadium
WHERE id =any(
SELECT id FROM tmp
UNION
SELECT last_id FROM tmp
UNION
SELECT next_id FROM tmp
)ORDER BY id