leetcode601. 体育馆的人流量(SQL)
X 市建了一個新的體育館,每日人流量信息被記錄在這三列信息中:序號 (id)、日期 (visit_date)、?人流量 (people)。
請編寫一個查詢語句,找出人流量的高峰期。高峰期時,至少連續(xù)三行記錄中的人流量不少于100。
例如,表 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 ? ? ? |
+------+------------+-----------+
對于上面的示例數(shù)據(jù),輸出為:
+------+------------+-----------+
| id ? | visit_date | people ? ?|
+------+------------+-----------+
| 5 ? ?| 2017-01-05 | 145 ? ? ? |
| 6 ? ?| 2017-01-06 | 1455 ? ? ?|
| 7 ? ?| 2017-01-07 | 199 ? ? ? |
| 8 ? ?| 2017-01-08 | 188 ? ? ? |
+------+------------+-----------+
?
提示:
每天只有一行記錄,日期隨著 id 的增加而增加。
思路:三個自連接,把三種情況寫一下,死亡大SQL
select distinct t1.* from stadium t1, stadium t2, stadium t3 where t1.people >= 100 and t2.people >= 100 and t3.people >= 100 and ((t1.id - t2.id = 1 and t2.id - t3.id =1)or(t1.id - t3.id =1 and t2.id - t1.id = 1)or(t3.id - t2.id = 1 and t2.id - t1.id =1) ) order by t1.id;?
總結(jié)
以上是生活随笔為你收集整理的leetcode601. 体育馆的人流量(SQL)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国单晶硅龙头上市公司
- 下一篇: 2021年吉祥文化纪念币 2021吉祥文