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.

这题的难度属于hard级别,但我觉得最多也就是一般难度。初看起来这题好像有点不知所措,但仔细分析却能发现其实条件很简单,只要满足以下任意三个条件即可:

1.id in (x,x+1,x+2) 的记录的people >= 100;

2.1.id in (x,x+1,x-1) 的记录的people >= 100;

3.1.id in (x,x-1,x-2) 的记录的people >= 100;

代码如下:

select * 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)
)
);

最新文章

  1. ArcGIS 10.5新功能预览
  2. 编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。
  3. (十七)迭代器模式详解(foreach的精髓)
  4. hdu-acm steps 命运
  5. AngularJS 模态对话框
  6. JavaScript hasOwnProperty() 函数详解
  7. Mui - 全局css
  8. 查看SQL执行计划
  9. iOS开发工具Xcode:Interface Builder
  10. js中点击事件方法三种方式的区别
  11. 表格td、th强制换行
  12. discuz! X3.2 自定义后台门户模块模板里的标签
  13. GDKOI2015 Day2
  14. poj 2299 Ultra-QuickSort 逆序对模版题
  15. C语言的指针使用
  16. java多线程基本概述(七)——join()方法
  17. CDQ分治 陌上花开(三维偏序)
  18. continue和break的特殊用法。
  19. Python 反射机制之hasattr()、getattr()、setattr() 、delattr()函数
  20. 斯坦福大学公开课机器学习:监督学习在行人检测的应用(supervised learning for pedestrian detection)

热门文章

  1. 不错的点击li标签删除的例子
  2. windows编程 使用C++实现多线程类
  3. HDU 1176 免费馅饼 (动态规划、另类数塔)
  4. HDU 1074 Doing Homework (状压DP)
  5. MySQL数据库常用引擎
  6. laravel5.5学习2-路由系统
  7. Nginx教程(一)-全面认知
  8. url简单加密
  9. 使用Idea部署SSM项目后,访问路径为url:8080/项目名_war_exploded的解决方案
  10. Elasticsearch入门教程(三):Elasticsearch索引&映射