Write a SQL query to calculate patient adherence metrics over a specific treatment timeline.
Use the treatment plan dates as the timeline. Count only dose events marked taken whose dates fall within the plan, and exclude events with other or NULL statuses from the taken count.
Output
- One row per treatment plan, including plans with no matching dose events.
- Columns:
patient_id, patient_name, plan_id, start_date, end_date, expected_doses, taken_doses, missed_doses, and adherence_percentage.
- Order by
patient_id, then plan_id ascending.