Skip to content

Commit 4cdf577

Browse files
authored
Create biggest-window-between-visits.sql
1 parent 0482e8f commit 4cdf577

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
SELECT
5+
user_id,
6+
MAX(DATEDIFF(visit_date_after, visit_date)) AS biggest_window
7+
FROM (
8+
SELECT
9+
user_id,
10+
visit_date,
11+
IFNULL(
12+
LEAD(visit_date, 1) OVER (
13+
PARTITION BY user_id
14+
ORDER BY visit_date
15+
),
16+
'2021-1-1'
17+
) AS visit_date_after
18+
FROM UserVisits
19+
) tmp
20+
GROUP BY 1
21+
ORDER BY NULL;

0 commit comments

Comments
 (0)