Write a SQL query to calculate weekly production output, defect rate, and on-time delivery by plant from operational data.
Use the plants, production_batches, and delivery_records tables. Treat missing defect counts as zero, include production batches without a delivery record, and calculate each week from the production date.
Output
- One row per plant and production week.
- Columns:
plant_name, week_start, total_units_produced, defect_rate_pct, and on_time_delivery_rate_pct.
- Include weeks with production but no delivery, return
NULL when a rate has no valid denominator, and sort by plant name and week start.