Skip to content

Commit 5ef95ec

Browse files
authored
Update find-bursty-behavior.sql
1 parent 176be0f commit 5ef95ec

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

MySQL/find-bursty-behavior.sql

+14-14
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22
# Space: O(n)
33

44
# window function
5-
WITH avg_weekly_posts_cte AS (
6-
SELECT user_id, COUNT(*) / 4.0 AS avg_weekly_posts
7-
FROM Posts
8-
WHERE post_date BETWEEN '2024-02-01' AND '2024-02-28'
9-
GROUP BY 1
10-
ORDER BY NULL
11-
), posts_in_7_days_cte AS (
5+
WITH posts_in_7_days_cte AS (
126
SELECT user_id, post_date, COUNT(*) OVER w AS sevenday_posts
137
FROM Posts
148
WINDOW w AS (PARTITION BY user_id
@@ -20,6 +14,12 @@ WITH avg_weekly_posts_cte AS (
2014
FROM posts_in_7_days_cte
2115
GROUP BY 1
2216
ORDER BY NULL
17+
), avg_weekly_posts_cte AS (
18+
SELECT user_id, COUNT(*) / 4.0 AS avg_weekly_posts
19+
FROM Posts
20+
WHERE post_date BETWEEN '2024-02-01' AND '2024-02-28'
21+
GROUP BY 1
22+
ORDER BY NULL
2323
)
2424

2525
SELECT user_id, max_7day_posts, avg_weekly_posts
@@ -32,13 +32,7 @@ ORDER BY 1;
3232
# Time: O(nlogn)
3333
# Space: O(n)
3434
# window function
35-
WITH avg_weekly_posts_cte AS (
36-
SELECT user_id, COUNT(*) / 4.0 AS avg_weekly_posts
37-
FROM Posts
38-
WHERE post_date BETWEEN '2024-02-01' AND '2024-02-28'
39-
GROUP BY 1
40-
ORDER BY NULL
41-
), posts_in_7_days_cte AS (
35+
WITH posts_in_7_days_cte AS (
4236
SELECT user_id, post_date, COUNT(*) OVER w AS sevenday_posts
4337
FROM Posts
4438
WINDOW w AS (PARTITION BY user_id
@@ -50,6 +44,12 @@ WITH avg_weekly_posts_cte AS (
5044
FROM posts_in_7_days_cte
5145
GROUP BY 1
5246
ORDER BY NULL
47+
), avg_weekly_posts_cte AS (
48+
SELECT user_id, COUNT(*) / 4.0 AS avg_weekly_posts
49+
FROM Posts
50+
WHERE post_date BETWEEN '2024-02-01' AND '2024-02-28'
51+
GROUP BY 1
52+
ORDER BY NULL
5353
)
5454

5555
SELECT user_id, max_7day_posts, avg_weekly_posts

0 commit comments

Comments
 (0)