Write a SQL query to find the top 3 regions by network downtime, partitioned by month.
Use completed downtime events only. Calculate each event's duration in minutes and assign it to the month in which it started. Return the three highest-downtime regions for each month, with deterministic ordering for ties.
Output
- One row per selected region and month
- Columns:
month, region_name, total_downtime_minutes, and region_rank
- Include only ranks 1 through 3, ordered by month ascending, rank ascending, and region name ascending
Output details
- Treat an event as completed only when
ended_at is non-NULL and strictly later than started_at.
- Format
month as YYYY-MM.
- Round
total_downtime_minutes to exactly 2 decimal places.
- Break equal downtime totals by
region_name ascending before assigning region_rank.