LeetCode MySQL 601. 体育馆的人流量(row_number+over+cast)
生活随笔
收集整理的這篇文章主要介紹了
LeetCode MySQL 601. 体育馆的人流量(row_number+over+cast)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
X 市建了一個新的體育館,每日人流量信息被記錄在這三列信息中:序號 (id)、日期 (visit_date)、 人流量 (people)。
請編寫一個查詢語句,找出人流量的高峰期。高峰期時,至少連續三行記錄中的人流量不少于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 | +------+------------+-----------+對于上面的示例數據,輸出為:
+------+------------+-----------+ | 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 的增加而增加。
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/human-traffic-of-stadium
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
2. 解題
- 使用 id 跟排序行號做差,連續的做差是一樣的
- 再套一層,算出 rnk 一樣的有多少個
- 最后篩選 cnt >= 3 的
或者 3表連接
# Write your MySQL query statement below select distinct a.* from stadium a, stadium b, stadium c where ((b.id = a.id+1 and c.id = b.id+1) or(c.id = b.id+1 and a.id = c.id+1) or(a.id = c.id+1 and b.id = a.id+1))and a.people>=100 and b.people>=100 and c.people>=100 order by a.id我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的LeetCode MySQL 601. 体育馆的人流量(row_number+over+cast)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 1754. 构造字典序
- 下一篇: LeetCode MySQL 1479.