Post Engagement Trends
Beginner Mode
Objective
Write a SQL query to calculate the 3-day rolling average of posts for each user.
Table Schema:
posts
| Column | Type | Description |
|---|---|---|
| post_id | INTEGER | Unique identifier for each post |
| user_id | INTEGER | ID of the user who created the post |
| post_date | DATE | Date when the post was created |
| post_count | INTEGER | Number of posts created on that date by the user |
Task Requirements:
- Calculate a 3-day rolling average of posts for each user
- The rolling average should include the current day and the 2 preceding days
- Round the rolling average to 2 decimal places
- Order results by user_id and post_date
Output columns: user_id, post_date, rolling_avg_3d
Examples
Example 1:
Output:
Input:
| posts | |||
|---|---|---|---|
| post_count | post_date | post_id | user_id |
| 5 | 2024-01-01 | 1 | 101 |
| 3 | 2024-01-02 | 2 | 101 |
| 7 | 2024-01-03 | 3 | 101 |
| 2 | 2024-01-04 | 4 | 101 |
| post_date | rolling_avg_3d | user_id |
|---|---|---|
| 2024-01-01 | 5 | 101 |
| 2024-01-02 | 4 | 101 |
| 2024-01-03 | 5 | 101 |
| 2024-01-04 | 4 | 101 |
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Twitter