LeetCode--SQL 查询:体育馆的人流量

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 的增加而增加。

sql:

SELECT a.* FROM stadium a, stadium b, stadium c
WHERE a.people>=100 and b.people>=100 and c.people>=100
and (
    (a.id-b.id=1 and b.id-c.id=1 and a.id-c.id=2) or  
    (b.id-a.id=1 and a.id-c.id=1 and b.id-c.id=2) or
    (c.id-b.id=1 and b.id-a.id=1 and c.id-a.id=2)
)
group by a.id

慢慢来才是最快的
原文地址:https://www.cnblogs.com/jongty/p/12746432.html