Rolling Average Steps - Problem
Calculate 3-Day Rolling Average Steps for Fitness Tracking

Imagine you're building a fitness tracking application that helps users analyze their daily step patterns. Given a database table containing user step counts over multiple days, your task is to calculate the 3-day rolling average for each user.

The Steps table contains:
user_id (int): Unique identifier for each user
steps_count (int): Number of steps taken on a specific date
steps_date (date): The date when steps were recorded

Rolling Average Calculation:
For each day, calculate the average of 3 consecutive days of step counts ending on that day. If fewer than 3 days of data are available ending on that day, the rolling average is not defined.

Output Requirements:
• Return user_id, steps_date, and rolling_average
• Round rolling average to two decimal places
• Order results by user_id and steps_date in ascending order
• Only include dates where a 3-day rolling average can be calculated

Input & Output

example_1.sql — Basic Rolling Average
$ Input: Steps table: | user_id | steps_count | steps_date | |---------|-------------|------------| | 1 | 1000 | 2023-01-01 | | 1 | 1200 | 2023-01-02 | | 1 | 1100 | 2023-01-03 | | 1 | 900 | 2023-01-04 | | 2 | 2000 | 2023-01-01 | | 2 | 1800 | 2023-01-02 |
Output: | user_id | steps_date | rolling_average | |---------|------------|----------------| | 1 | 2023-01-03 | 1100.00 | | 1 | 2023-01-04 | 1066.67 |
💡 Note: For user 1 on 2023-01-03: avg(1000, 1200, 1100) = 1100.00. For user 1 on 2023-01-04: avg(1200, 1100, 900) = 1066.67. User 2 only has 2 days of data, so no 3-day rolling average can be calculated.
example_2.sql — Multiple Users
$ Input: Steps table: | user_id | steps_count | steps_date | |---------|-------------|------------| | 1 | 1000 | 2023-01-01 | | 1 | 1500 | 2023-01-02 | | 1 | 2000 | 2023-01-03 | | 2 | 3000 | 2023-01-01 | | 2 | 3200 | 2023-01-02 | | 2 | 2800 | 2023-01-03 |
Output: | user_id | steps_date | rolling_average | |---------|------------|----------------| | 1 | 2023-01-03 | 1500.00 | | 2 | 2023-01-03 | 3000.00 |
💡 Note: User 1: avg(1000, 1500, 2000) = 1500.00. User 2: avg(3000, 3200, 2800) = 3000.00. Both users have exactly 3 days of consecutive data.
example_3.sql — Insufficient Data
$ Input: Steps table: | user_id | steps_count | steps_date | |---------|-------------|------------| | 1 | 1000 | 2023-01-01 | | 1 | 1200 | 2023-01-02 | | 2 | 2000 | 2023-01-05 |
Output: Empty result set
💡 Note: No user has 3 consecutive days of data. User 1 has only 2 days, User 2 has only 1 day. Rolling average requires exactly 3 days of data.

Constraints

  • 1 ≤ user_id ≤ 104
  • 1 ≤ steps_count ≤ 106
  • steps_date is a valid date in 'YYYY-MM-DD' format
  • Each (user_id, steps_date) combination is unique
  • Rolling average must be calculated for exactly 3 consecutive days

Visualization

Tap to expand
📱 Fitness Tracker: 3-Day Rolling AverageDaily Steps TimelineUser 1 - January 2023Day 11000Day 21200Day 31100Day 4900Day 513003-Day Window: [1000, 1200, 1100]Average: 1100.00Day 11000Day 21200Day 31100Day 4900Day 51300Next Window: [1200, 1100, 900]Average: 1066.67Window slides →🎯 Key Insight: Window functions automatically manage the sliding window,calculating rolling averages efficiently in a single database pass!
Understanding the Visualization
1
Window Formation
Create a 3-day sliding window ending on current date
2
Average Calculation
Calculate mean of steps in the window
3
Window Slide
Move window forward by one day, drop oldest, add newest
4
Result Filtering
Only output results where full 3-day window exists
Key Takeaway
🎯 Key Insight: SQL window functions with ROWS frame specification provide the most efficient solution for rolling average calculations, automatically managing the sliding window while maintaining optimal performance.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 25
28.4K Views
High Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen