Samsara's fleet operations team wants to monitor incident trends for the West Operations team. Write a PostgreSQL query that calculates the daily incident count and its rolling 7-day average from January 1 through January 10, 2025.
Requirements
- Include every calendar date in the requested range, including dates with zero incidents.
- Count only incidents associated with active vehicles assigned to the West Operations team.
- Use a window function to calculate the average of the current day and previous six calendar days.
- Return the team name, report date, daily incident count, and rolling average, ordered chronologically.
Output details
- Return columns named
team_name, report_date, daily_incident_count, and rolling_7_day_average, in that order.
- Round
rolling_7_day_average to exactly two decimal places.
- Order results by
team_name ascending, then report_date ascending.
- Treat each matching incident report row as one incident, without deduplication.
- Use incidents with
reported_at on or after each date and before the following calendar date.