A factory maintenance platform stores every period when a machine is scheduled to be offline. Operations managers want to compare the overall downtime burden across machines.
Table: maintenance_windows
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| machine_id | integer |
| offline_from | date |
| online_on | date |
+--------------+---------+
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| machine_id | integer |
| offline_from | date |
| online_on | date |
+--------------+---------+
The combination of machine_id and offline_from is the primary key. Each row represents one scheduled maintenance window. Assume online_on is never earlier than offline_from.
For each machine, sum the number of calendar days across all of its maintenance windows. Include offline_from and exclude online_on, so a window from 2026-01-01 to 2026-01-10 contributes 9 days. A window whose two dates are equal contributes 0 days.
Calculate the average of these per-machine downtime totals across every machine present in the table. Round the result to 2 decimal places and return it as avg_downtime_days.