为有牺牲多壮志,敢教日月换新天。

[SQL]LeetCode601. 体育馆的人流量 | Human Traffic of Stadium

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10451553.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

SQL架构

 1 Create table If Not Exists stadium (id int, visit_date DATE NULL, people int)
 2 Truncate table stadium
 3 insert into stadium (id, visit_date, people) values ('1', '2017-01-01', '10')
 4 insert into stadium (id, visit_date, people) values ('2', '2017-01-02', '109')
 5 insert into stadium (id, visit_date, people) values ('3', '2017-01-03', '150')
 6 insert into stadium (id, visit_date, people) values ('4', '2017-01-04', '99')
 7 insert into stadium (id, visit_date, people) values ('5', '2017-01-05', '145')
 8 insert into stadium (id, visit_date, people) values ('6', '2017-01-06', '1455')
 9 insert into stadium (id, visit_date, people) values ('7', '2017-01-07', '199')
10 insert into stadium (id, visit_date, people) values ('8', '2017-01-08', '188')

X city built a new stadium, each day many people visit it and the stats are saved as these columns: id, visit_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   | 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       |
+------+------------+-----------+

For the sample data above, the output is:

+------+------------+-----------+
| id   | visit_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.


X 市建了一个新的体育馆,每日人流量信息被记录在这三列信息中:序号 (id)、日期 (date)、 人流量 (people)。

请编写一个查询语句,找出高峰期时段,要求连续三天及以上,并且每天人流量均不少于100。

例如,表 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       |
+------+------------+-----------+

对于上面的示例数据,输出为:

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

Note:
每天只有一行记录,日期随着 id 的增加而增加。


260ms

 1 # Write your MySQL query statement below
 2 select distinct t1.*
 3 from stadium t1, stadium t2, stadium t3
 4 where t1.people>=100 and t2.people>=100 and t3.people>=100
 5 and
 6 ((t1.id-t2.id=1 and t1.id-t3.id=2 and t2.id-t3.id=1)
 7 or
 8 (t2.id-t1.id=1 and t2.id-t3.id=2 and t1.id-t3.id=1)
 9 or
10 (t3.id-t2.id=1 and t2.id-t1.id=1 and t3.id-t1.id=2))
11 order by t1.id

266ms

 1 # Write your MySQL query statement below
 2 
 3 select distinct t.* from 
 4 stadium t ,
 5 (
 6 select 
 7 t1.Id, t2.Id as t2Id, t3.Id as t3Id
 8 from (
 9 select * from stadium 
10 where people>=100)  as t1
11 left join
12 (
13 select * from stadium 
14 where people>=100 ) as t2
15 ON  t1.Id+1=t2.Id
16 left join 
17 (
18 select * from stadium 
19 where people>=100 ) as t3
20 ON  t1.Id+2=t3.Id
21 where (t2.Id is not null and t3.Id is not null)
22 ) as b 
23 where (t.Id=b.Id or t.Id=b.t2Id or t.Id=b.t3Id);

268ms

 1 # Write your MySQL query statement below
 2 SELECT s1.id AS id
 3       ,s1.date AS date
 4       ,s1.people AS people
 5 FROM stadium s1, stadium s2, stadium s3
 6 WHERE(
 7     (s1.id = s2.id - 1 AND s1.id = s3.id - 2)
 8     OR (s1.id = s2.id + 1 AND s1.id = s3.id - 1)
 9     OR (s1.id = s2.id + 1 AND s1.id = s3.id +2)
10     )
11 AND s1.people >= 100
12 AND s2.people >= 100
13 AND s3.people >= 100
14 GROUP BY s1.id;

 

posted @ 2019-02-28 19:09  为敢技术  阅读(496)  评论(0编辑  收藏  举报