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

【leetcode database】Human Traffic of Stadium的更多相关文章

  1. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  2. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  3. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  4. 【LeetCode题解】136_只出现一次的数字

    目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...

  5. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

  6. 【LeetCode题解】350_两个数组的交集Ⅱ

    目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...

  7. 【LeetCode题解】349_两个数组的交集

    目录 [LeetCode题解]349_两个数组的交集 描述 方法一:两个哈希表 Java 实现 类似的 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 ...

  8. 【LeetCode题解】94_二叉树的中序遍历

    目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...

  9. 【LeetCode题解】144_二叉树的前序遍历

    目录 [LeetCode题解]144_二叉树的前序遍历 描述 方法一:递归 Java 代码 Python 代码 方法二:非递归(使用栈) Java 代码 Python 代码 [LeetCode题解]1 ...

随机推荐

  1. Java中volatile关键字的最全总结

    转载至:https://blog.csdn.net/u012723673/article/details/80682208 关于volatile很重要的一点: 它保证了可见性,即其他线程对volati ...

  2. 【VS开发】设置文档标题

    [注意]我们注意到我们的文档的创建的时候我们会发现系统会去调用Doc类中的OnNewDocument函数,这里我们知道他是一个虚函数,我们可以在这里设置我们的文档的标题. 代码如下: BOOL CGr ...

  3. Angular5 父组件获取子组件实例( ViewChildren、ViewChild用法)

    原文链接 Understanding ViewChildren, ContentChildren, and QueryList in Angular 使用场景 有时候,我们想要在父组件中访问它的子组件 ...

  4. mysql——单表查询——其它整理示例01

    create database see; use database see; drop database sww; ========================================== ...

  5. C语言I博客作业12-学期总结

    一.我学到的内容 二.我的收获 1. https://www.cnblogs.com/1076022899-lj/p/11576442.html 收获:第一次接触到C语言和PTA,第一次学习了博客园和 ...

  6. 教你用 Netty 实现一个简单的 RPC!

    众所周知,dubbo 底层使用了 Netty 作为网络通讯框架,而 Netty 的高性能我们之前也分析过源码,对他也算还是比较了解了. 今天我们就自己用 Netty 实现一个简单的 RPC 框架. 1 ...

  7. Ubuntu下Win10虚拟机卡顿问题解决

    问题描述 Ubuntu下用VMware创建了一个Windows10的虚拟机,初始给的配置如下: Memory: 4GB Graphics Memory: 1GB Hard Disk: 60GB 用的时 ...

  8. RocketMQ吐血总结

    RocketMQ吐血总结 架构 概念模型 最基本的概念模型与扩展后段概念模型 存储模型 RocketMQ吐血总结 User Guide RocketMQ是一款分布式消息中间件,最初是由阿里巴巴消息中间 ...

  9. 真香系列之 Golang 升级

    Golang 以前的依赖管理一直饱受诟病,社区的方案也层出不穷,比如 vendor, glide, godep 等.之前的依赖管理一直是依靠 GOPATH 或者将依赖代码下载到本地,这种方式都有劣势. ...

  10. 国际化:node导语言包

    项目做国际化,是离不开语言包的en.json.zh-CN.json等等.难道要一个一个json文件的写吗???不!!!只要我们写在excel里,然后用nodejs导出我们需要的语言包就可以了!这样大大 ...