Write a SQL query to compare planned dispatches versus actual completed loads by day and region, and flag regions where completion rate fell below target.
Use the provided tables and include days with planned dispatches but no completed loads, as well as completed loads without plans.
Output
- One row per day and region with
activity_date, region_name, planned_dispatches, completed_loads, completion_rate, and below_target
- Include all activity dates, calculate a zero completion rate when no dispatches were planned, and flag rates below the region target
- Order by
activity_date, then region_name
Output details
- Sort
activity_date ascending, then region_name ascending.
- Round
completion_rate to exactly 4 decimal places.
- Treat zero planned dispatches as
completion_rate 0 and below_target TRUE.
- Compare the unrounded completion ratio with the region target.
- Exclude completed loads with a
NULL region_id.
- Omit activity rows whose region identifier has no matching region record.