日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

LeetCode MySQL 601. 体育馆的人流量(row_number+over+cast)

發布時間:2024/7/5 数据库 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 跟排序行號做差,連續的做差是一樣的
select stadium.*, id - cast(row_number() over(partition by people >= 100) as signed) rnk from stadium where people >= 100 {"headers": ["id", "visit_date", "people", "rnk"], "values": [ [2, "2017-01-02", 109, 1], [3, "2017-01-03", 150, 1], [5, "2017-01-05", 145, 2], [6, "2017-01-06", 1455, 2], [7, "2017-01-07", 199, 2], [8, "2017-01-08", 188, 2]]}
  • 再套一層,算出 rnk 一樣的有多少個
select id, visit_date, people,count(*) over(partition by rnk) cntfrom (select stadium.*, id - cast(row_number() over(partition by people >= 100) as signed) rnkfrom stadiumwhere people >= 100 ) t {"headers": ["id", "visit_date", "people", "cnt"], "values": [ [2, "2017-01-02", 109, 2], [3, "2017-01-03", 150, 2], [5, "2017-01-05", 145, 4], [6, "2017-01-06", 1455, 4], [7, "2017-01-07", 199, 4], [8, "2017-01-08", 188, 4]]}
  • 最后篩選 cnt >= 3 的
# Write your MySQL query statement below select id, visit_date, people from (select id, visit_date, people,count(*) over(partition by rnk) cntfrom(select stadium.*, id - cast(row_number() over(partition by people >= 100) as signed) rnkfrom stadiumwhere people >= 100) t ) t where 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。