601. Human Traffic of Stadium

X city built a new stadium, each day many people visit it and the stats are saved as these columns: id, date, people

Please write a query to display the records which have 3 or more consecutive rows and the amount of people more than 100(inclusive).

For example, the table 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       |
+------+------------+-----------+

For the sample data above, the output is:

+------+------------+-----------+
| id   | date       | people    |
+------+------------+-----------+
| 5    | 2017-01-05 | 145       |
| 6    | 2017-01-06 | 1455      |
| 7    | 2017-01-07 | 199       |
| 8    | 2017-01-08 | 188       |
+------+------------+-----------+

Note:
Each day only have one row record, and the dates are increasing with id increasing.


Answer:

select distinct s1.id, s1.date, s1.people
from stadium s1 inner join stadium s2 inner join stadium s3
on ((s2.id=s1.id-1 and s3.id-2=s2.id) or ( s2.id-1=s1.id and s3.id-2=s1.id) or ( s2.id=s1.id-2 and s3.id-1=s2.id))
where s1.people>=100 and s2.people>=100 and s3.people>=100 
order by s1.id
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,974评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 11,668评论 0 23
  • 暴走随想 生活像一首歌那样轻快流畅时,笑颜常开乃易事;而在一切事都不妙时仍能微笑的...
    夏天sky阅读 312评论 4 2
  • 2017年10月9日 初三第50天 早上我去送她,张同学心情比较低落。 昨天预估分出来后,觉得自己考砸了,距离自己...
    天空有云阅读 217评论 0 0
  • 要随时提醒自己,干什么都要坚持,锻炼身体尤为重要,前段时间腰散了间断了半个月,再回去上,真的有点坚持不下来了,昨天...
    绪英阅读 283评论 0 0

友情链接更多精彩内容