X市建了一个新的体育馆,每日人流信息记录在三列的信息中,序号(id)/日期(visit_date),人流量(people),写一个查询语句,找出人流量的高峰期。高峰期时,至少连续三行记录中的人流量不少于100.例如表是stadium:
id | visit_date | people |
---|---|---|
1 | 2019-01-01 | 10 |
2 | 2019-01-02 | 109 |
3 | 2019-01-03 | 150 |
4 | 2019-01-04 | 99 |
5 | 2019-01-05 | 145 |
6 | 2019-01-06 | 1455 |
7 | 2019-01-07 | 199 |
8 | 2019-01-08 | 177 |
对于上面的数据应该得到
SELECT DISTINCT s1.*
FROM studium s1 join studium s2 on s1.id=s2.id-1
join studium s3 on s1.id=s3.id-2
where s1.people>=100
and s2.people>=100
and s3.people>=100
UNION
select DISTINCT s1.*
FROM studium s1 join studium s2 on s1.id=s2.id+1
join studium s3 on s1.id=s3.id+2
where s1.people>=100
and s2.people>=100
and s3.people>=100