2
2
# Space: O(n)
3
3
4
4
# 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 (
12
6
SELECT user_id, post_date, COUNT (* ) OVER w AS sevenday_posts
13
7
FROM Posts
14
8
WINDOW w AS (PARTITION BY user_id
@@ -20,6 +14,12 @@ WITH avg_weekly_posts_cte AS (
20
14
FROM posts_in_7_days_cte
21
15
GROUP BY 1
22
16
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
23
23
)
24
24
25
25
SELECT user_id, max_7day_posts, avg_weekly_posts
@@ -32,13 +32,7 @@ ORDER BY 1;
32
32
# Time: O(nlogn)
33
33
# Space: O(n)
34
34
# 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 (
42
36
SELECT user_id, post_date, COUNT (* ) OVER w AS sevenday_posts
43
37
FROM Posts
44
38
WINDOW w AS (PARTITION BY user_id
@@ -50,6 +44,12 @@ WITH avg_weekly_posts_cte AS (
50
44
FROM posts_in_7_days_cte
51
45
GROUP BY 1
52
46
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
53
53
)
54
54
55
55
SELECT user_id, max_7day_posts, avg_weekly_posts
0 commit comments