How would you design a schema to track patient billing cycles over time at Candid Health?
Represent patients, billing cycles, and cycle events, then return a chronological cycle history that shows prior-cycle context and the latest recorded event for each cycle.
Output
- One row per billing cycle, excluding patients without cycles
- Columns: patient identity, cycle dates and status, billed amount, prior cycle status, days between cycles, latest event type, and latest event timestamp
- Sort by patient ID, cycle start date, and cycle ID
Output details
- Return columns in this order:
patient_id, patient_name, cycle_id, cycle_start_date, cycle_end_date, cycle_status, billed_amount, prior_cycle_status, days_between_cycles, latest_event_type, latest_event_at.
- Sort every listed key in ascending order:
patient_id, cycle_start_date, then cycle_id.
- For equal patient and start-date values, use ascending
cycle_id to determine prior-cycle context.
- Select the latest event by descending
event_at, breaking ties with descending event_id.
- Preserve
NULL for missing events, the first prior-cycle values, and days_between_cycles when no prior end date exists.