Write a SQL query to analyze production efficiency by plant, shift, and week, joining production output and downtime tables to identify the top bottlenecks.
Calculate production totals, planned units, downtime minutes, efficiency percentage, and prior-week downtime. Include unmatched production or downtime groups, and identify the two highest-downtime groups per plant.
plant_name, shift_name, week_start, total_units_produced, planned_units, total_downtime_minutes, efficiency_pct, prior_week_downtime_minutes, and bottleneck_rank.| Column | Type | Description |
|---|---|---|
| output_idPK | INT | Production output record identifier |
| plant_name | VARCHAR(100) | Manufacturing plant name |
| shift_name | VARCHAR(30) | Production shift |
| production_date | DATE | Date of production |
| units_produced | INT | Units produced during the record |
| planned_units | INT | Planned units for the production record |
| Column | Type | Description |
|---|---|---|
| downtime_idPK | INT | Downtime event identifier |
| plant_name | VARCHAR(100) | Manufacturing plant name |
| shift_name | VARCHAR(30) | Shift during the downtime event |
| downtime_date | DATE | Date of downtime event |
| downtime_minutes | INT | Downtime duration in minutes |
| reason | VARCHAR(100) | Downtime reason |