Approach
Relational aggregation
For Find Bursty Behavior, the query transforms and combines relational rows, then filters or aggregates them into the requested result.
- Identify the source rows and join keys.
- Apply filters before aggregation when possible.
- Group, rank, or project the final columns required by the result.
Code notes
- 30 lines of SQL from the credited upstream file 3089.sql.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Review join cardinality, grouping keys, and available indexes when estimating query cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1WITH2 SevenDayPostCounts AS (3 SELECT4 Post1.user_id,5 COUNT(*) AS post_count6 FROM Posts AS Post17 INNER JOIN Posts AS Post28 USING (user_id)9 WHERE Post2.post_date BETWEEN Post1.post_date AND DATE_ADD(Post1.post_date, INTERVAL 6 DAY)10 GROUP BY Post1.user_id, Post1.post_id11 ),12 AverageWeeklyPosts AS (13 SELECT14 user_id,15 COUNT(*) / 4 AS avg_weekly_posts16 FROM Posts17 WHERE post_date BETWEEN '2024-02-01' AND '2024-02-28'18 GROUP BY 119 )20SELECT21 SevenDayPostCounts.user_id,22 MAX(SevenDayPostCounts.post_count) AS max_7day_posts,23 AverageWeeklyPosts.avg_weekly_posts24FROM SevenDayPostCounts25INNER JOIN AverageWeeklyPosts26 USING (user_id)27GROUP BY 128HAVING max_7day_posts >= avg_weekly_posts * 229ORDER BY 1;30