【leetcode database】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.
这题的难度属于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的更多相关文章
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode题解】136_只出现一次的数字
目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
- 【LeetCode题解】350_两个数组的交集Ⅱ
目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...
- 【LeetCode题解】349_两个数组的交集
目录 [LeetCode题解]349_两个数组的交集 描述 方法一:两个哈希表 Java 实现 类似的 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 ...
- 【LeetCode题解】94_二叉树的中序遍历
目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...
- 【LeetCode题解】144_二叉树的前序遍历
目录 [LeetCode题解]144_二叉树的前序遍历 描述 方法一:递归 Java 代码 Python 代码 方法二:非递归(使用栈) Java 代码 Python 代码 [LeetCode题解]1 ...
随机推荐
- 在Linux命令行模式安装VMware Tools
在Linux命令行模式安装VMware Tools 方法/步骤1: 首先启动CentOS 7,在VMware中点击上方“VM”,点击“Install VMware Tools...”(如已安装则显示“ ...
- epoll 性能分析(解决占用CPU 过高问题)2
针对服务器框架Engine,在工作线程中发现该线程占用CPU过高,分析之后发现问题出在死循环那里 void cServerBase::OnProcess() { printf("cServe ...
- Python学习之数据库
9.6 表的查询 [结构]select distinct 字段1,字段2 from 表名 where 条件 group by 字段 having 筛选 order by 字段 limit 限制条数 [ ...
- python常用语句
流程控制if...else.... name = '疯子' res = input('你叫什么名字?') if res == name: print('帅哥') else: print('丑男') 如 ...
- C++ 结构体重载运算符
听说这个东西有很多种写法什么的,来不及了(要退役了),先整一个之前用到的,可能用到的频率比较高的东西上来. struct node{ ll x,y; }; bool operator < (co ...
- [Vuejs] 在vue各个组件中应用全局scss变量
需要安装一个插件:sass-resources-loader 1.执行安装命令: npm i sass-resources-loader --save-dev 2.修改vue-cli环境下build文 ...
- 20191128 Spring Boot官方文档学习(10)
10.附录 附录A:通用应用程序属性 附录B:配置元数据 附录C:自动配置类 附录D:测试的自动配置注释 附录E:可执行的Jar格式 附录F:依赖版本
- mysql生成全局id(转)
由于数据量以及IO效率的因素,很多项目对数据支持的数据库会采取分库分表的方式.使用了分库分表之后需要解决的一个问题就是主键的生成.多个表之间的主键就不能用数据库本身的自增主键来支持,因为不同表之间生成 ...
- Java集合简单解析
一. Collection 1. List a. ArrayList b. Vector c. LinkedList 首先要对List的三种实现进行一个简单的异同比较: 同: *ArrayList和V ...
- js and java 中正则表达式的使用
首先介绍一下js当中的几个关键的正则表达式: 1.js中的正则表达式校验 a: RegExp(如果这里有转义字符的话,需要使用“\\”) var patt1=new RegExp("e&qu ...