A facilities platform tracks completed repair jobs and the operating zone assigned to each facility. Finance wants a zone-level view of repair spending for a fixed 365-day reporting period.
Table: facilities
+-------------+--------------+
| Column Name | Type |
+-------------+--------------+
| facility_id | integer |
| zone | varchar(255) |
+-------------+--------------+
+-------------+--------------+
| Column Name | Type |
+-------------+--------------+
| facility_id | integer |
| zone | varchar(255) |
+-------------+--------------+
facility_id is the primary key. zone may be NULL.
Table: repair_jobs
+----------------+---------------+
| Column Name | Type |
+----------------+---------------+
| repair_id | integer |
| facility_id | integer |
| completed_on | date |
| repair_cost | decimal(12,2) |
+----------------+---------------+
+----------------+---------------+
| Column Name | Type |
+----------------+---------------+
| repair_id | integer |
| facility_id | integer |
| completed_on | date |
| repair_cost | decimal(12,2) |
+----------------+---------------+
repair_id is the primary key. facility_id references facilities.facility_id. Each row represents one completed repair job.
Consider repair jobs completed during the 365-day window beginning on 2025-10-01 and ending immediately before 2026-10-01. In other words, include 2025-10-01 through 2026-09-30, inclusive.
Join each qualifying repair job to its facility and calculate the total repair_cost for every zone. Replace a NULL zone with Unknown. A real zone value equal to Unknown belongs to the same output group as the replaced NULL values.
Return zone and total_repair_cost. Order the results by total_repair_cost descending, then by zone ascending when totals are tied. Do not return zones without a qualifying repair job.
Example
Input:
facilities table:
+-------------+-------+
| facility_id | zone |
+-------------+-------+
| 1 | West |
| 2 | East |
| 3 | North |
+-------------+-------+
facilities table:
+-------------+-------+
| facility_id | zone |
+-------------+-------+
| 1 | West |
| 2 | East |
| 3 | North |
+-------------+-------+