-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1709.sql
More file actions
38 lines (37 loc) · 1007 Bytes
/
LeetCode1709.sql
File metadata and controls
38 lines (37 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-- 先分组排序,再自连接获取成对的日期,再分组求最大值
with
cte as (
select user_id, visit_date, ROW_NUMBER() over (
partition by
user_id
order by visit_date
) ranking
from UserVisits
)
select t1.user_id, max(
DATEDIFF(
ifnull(t2.visit_date, '2021-1-1'), t1.visit_date
)
) as "biggest_window"
from cte t1
left join cte t2 on t1.user_id = t2.user_id
and t1.ranking + 1 = t2.ranking
group by
t1.user_id
order by user_id;
-- 通过LEAD直接找到成对的日期,分组求最大值
SELECT user_id, MAX(
DATEDIFF(next_day, visit_date)
) AS biggest_window
FROM (
SELECT
user_id, visit_date, LEAD(visit_date, 1, '2021-1-1') OVER (
PARTITION BY
user_id
ORDER BY visit_date
) AS next_day
FROM UserVisits
) tmp
GROUP BY
user_id
ORDER BY user_id;