Your question is 30-Day Moving Average of Bookings. Start with the requirements and the two tables on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
You’re on the analytics engineering team at a two-sided logistics marketplace (think “Uber for warehouse labor”) that processes ~2M task bookings per month across multiple metro areas. Operations leaders track booking volume to forecast staffing needs and to detect demand shocks (weather events, promotions, large enterprise contracts). Daily booking counts are noisy, so the team uses a 30-day moving average as the primary trend metric in dashboards.
Bookings are stored at the event level (one row per booking). However, the dashboard needs a daily time series that includes days with zero bookings (e.g., holidays or system outages), because missing dates can make the moving average misleading.
Write a SQL query to compute the daily booking count and the 30-day moving average of daily bookings.
2024-01-01 and 2024-01-07 (inclusive) using the dim_dates table.booked_at (treat booked_at as UTC).ma_30d_bookings as the average of daily_bookings over the current day and the prior 29 days (a 30-day trailing window).dt, daily_bookings, ma_30d_bookings.dt ascending.| Column | Type | Description |
|---|---|---|
| dtPK | DATE | Calendar date (one row per day) |
| Column | Type | Description |
|---|---|---|
| booking_idPK | BIGINT | Unique booking identifier |
| task_id | BIGINT | Task being booked |
| booked_at | TIMESTAMP | Timestamp when the booking was created (UTC) |
| status | VARCHAR(20) | Booking state (e.g., confirmed, cancelled) |