-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3089-FindBurstyBehavior.sql
More file actions
232 lines (222 loc) · 6.71 KB
/
3089-FindBurstyBehavior.sql
File metadata and controls
232 lines (222 loc) · 6.71 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
-- 3089. Find Bursty Behavior
-- Table: Posts
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | post_id | int |
-- | user_id | int |
-- | post_date | date |
-- +-------------+---------+
-- post_id is the primary key (column with unique values) for this table.
-- Each row of this table contains post_id, user_id, and post_date.
-- Write a solution to find users who demonstrate bursty behavior in their posting patterns during February 2024. Bursty behavior is defined as any period of 7 consecutive days where a user's posting frequency is at least twice to their average weekly posting frequency for February 2024.
-- Note: Only include the dates from February 1 to February 28 in your analysis, which means you should count February as having exactly 4 weeks.
-- Return the result table orderd by user_id in ascending order.
-- The result format is in the following example.
-- Example:
-- Input:
-- Posts table:
-- +---------+---------+------------+
-- | post_id | user_id | post_date |
-- +---------+---------+------------+
-- | 1 | 1 | 2024-02-27 |
-- | 2 | 5 | 2024-02-06 |
-- | 3 | 3 | 2024-02-25 |
-- | 4 | 3 | 2024-02-14 |
-- | 5 | 3 | 2024-02-06 |
-- | 6 | 2 | 2024-02-25 |
-- +---------+---------+------------+
-- Output:
-- +---------+----------------+------------------+
-- | user_id | max_7day_posts | avg_weekly_posts |
-- +---------+----------------+------------------+
-- | 1 | 1 | 0.2500 |
-- | 2 | 1 | 0.2500 |
-- | 5 | 1 | 0.2500 |
-- +---------+----------------+------------------+
-- Explanation:
-- User 1: Made only 1 post in February, resulting in an average of 0.25 posts per week and a max of 1 post in any 7-day period.
-- User 2: Also made just 1 post, with the same average and max 7-day posting frequency as User 1.
-- User 5: Like Users 1 and 2, User 5 made only 1 post throughout February, leading to the same average and max 7-day posting metrics.
-- User 3: Although User 3 made more posts than the others (3 posts), they did not reach twice the average weekly posts in their consecutive 7-day window, so they are not listed in the output.
-- Note: Output table is ordered by user_id in ascending order.
-- Create table If Not Exists Posts (post_id int, user_id int, post_date date)
-- Truncate table Posts
-- insert into Posts (post_id, user_id, post_date) values ('1', '1', '2024-02-27')
-- insert into Posts (post_id, user_id, post_date) values ('2', '5', '2024-02-06')
-- insert into Posts (post_id, user_id, post_date) values ('3', '3', '2024-02-25')
-- insert into Posts (post_id, user_id, post_date) values ('4', '3', '2024-02-14')
-- insert into Posts (post_id, user_id, post_date) values ('5', '3', '2024-02-06')
-- insert into Posts (post_id, user_id, post_date) values ('6', '2', '2024-02-25')
-- SELECT
-- user_id,
-- MAX(cnt) AS max_7day_posts,
-- SUM(cnt) / 4 AS avg_weekly_posts
-- FROM
-- (
-- (
-- SELECT
-- COUNT(*) AS cnt,
-- user_id
-- FROM
-- Posts
-- WHERE
-- post_date BETWEEN '2024-02-01' AND '2024-02-06'
-- GROUP BY
-- user_id
-- )
-- UNION ALL
-- (
-- SELECT
-- COUNT(*) AS cnt,
-- user_id
-- FROM
-- Posts
-- WHERE
-- post_date BETWEEN '2024-02-07' AND '2024-02-13'
-- GROUP BY
-- user_id
-- )
-- UNION ALL
-- (
-- SELECT
-- COUNT(*) AS cnt,
-- user_id
-- FROM
-- Posts
-- WHERE
-- post_date BETWEEN '2024-02-14' AND '2024-02-20'
-- GROUP BY
-- user_id
-- )
-- UNION ALL
-- (
-- SELECT
-- COUNT(*) AS cnt,
-- user_id
-- FROM
-- Posts
-- WHERE
-- post_date BETWEEN '2024-02-21' AND '2024-02-28'
-- GROUP BY
-- user_id
-- )
-- ) AS t
-- GROUP BY
-- user_id
-- HAVING
-- COUNT(*) <= 2
-- ORDER BY
-- user_id
-- SELECT
-- t.*,
-- rank() OVER(ORDER BY week) AS "rk"
-- FROM
-- (
-- (
-- SELECT
-- '1' AS week,
-- COUNT(*) AS cnt,
-- user_id
-- FROM
-- Posts
-- WHERE
-- post_date BETWEEN '2024-02-01' AND '2024-02-06'
-- GROUP BY
-- user_id
-- )
-- UNION ALL
-- (
-- SELECT
-- '2' AS week,
-- COUNT(*) AS cnt,
-- user_id
-- FROM
-- Posts
-- WHERE
-- post_date BETWEEN '2024-02-07' AND '2024-02-13'
-- GROUP BY
-- user_id
-- )
-- UNION ALL
-- (
-- SELECT
-- '3' AS week,
-- COUNT(*) AS cnt,
-- user_id
-- FROM
-- Posts
-- WHERE
-- post_date BETWEEN '2024-02-14' AND '2024-02-20'
-- GROUP BY
-- user_id
-- )
-- UNION ALL
-- (
-- SELECT
-- '4' AS week,
-- COUNT(*) AS cnt,
-- user_id
-- FROM
-- Posts
-- WHERE
-- post_date BETWEEN '2024-02-21' AND '2024-02-28'
-- GROUP BY
-- user_id
-- )
-- ) AS t
WITH
P AS ( -- 用户任何 连续 7 天 的时段发帐数
SELECT
p1.user_id AS user_id,
COUNT(1) AS cnt
FROM
Posts AS p1
JOIN
Posts AS p2
ON
p1.user_id = p2.user_id AND
-- 任何 连续 7 天 的时段
p2.post_date BETWEEN p1.post_date AND DATE_ADD(p1.post_date, INTERVAL 6 DAY)
GROUP BY
p1.user_id, p1.post_id
),
T AS ( -- 2月平均每周发贴数
SELECT
user_id,
COUNT(1) / 4 AS avg_weekly_posts
FROM
Posts
WHERE
post_date BETWEEN '2024-02-01' AND '2024-02-28'
GROUP BY
user_id
)
SELECT
user_id,
MAX(cnt) AS max_7day_posts,
avg_weekly_posts
FROM
P
JOIN T USING (user_id)
GROUP BY
user_id
HAVING
max_7day_posts >= avg_weekly_posts * 2 -- 任何 连续 7 天 的时段中发帖频率是其 平均 每周发帖频率的 至少两倍。
ORDER BY
user_id;
-- WITH
-- ),
-- T AS (
-- SELECT user_id, COUNT(1) / 4 AS avg_weekly_posts
-- FROM Posts
-- WHERE post_date BETWEEN '2024-02-01' AND '2024-02-28'
-- GROUP BY user_id
-- )
-- SELECT user_id, MAX(cnt) AS max_7day_posts, avg_weekly_posts
-- FROM
-- P
-- JOIN T USING (user_id)
-- GROUP BY user_id
-- HAVING max_7day_posts >= avg_weekly_posts * 2
-- ORDER BY user_id;