Chapter 7 · CTEs
31. Complete Patient Journey Timeline
Hard40 XPHealthcare
Build a comprehensive patient journey report using CTEs. For each patient, show: total visits, total prescriptions, total lab tests, total billed amount, and days between their first and last visit (patient tenure). Return patient full name, total_visits, total_prescriptions, total_lab_tests, total_billed, and tenure_days. Sort by tenure_days descending.
Tables
- PKidINT
- first_nameTEXT
- last_nameTEXT
- date_of_birthTEXT
- genderTEXT
- emailTEXT
- phoneTEXT
- blood_typeTEXT
- cityTEXT
- stateTEXT
- FKinsurance_id→ insurance.idINT
- created_atTIMESTAMP
- PKidINT
- FKpatient_id→ patients.idINT
- FKdoctor_id→ doctors.idINT
- visit_dateTEXT
- diagnosisTEXT
- notesTEXT
- follow_up_dateTEXT
- statusTEXT
- PKidINT
- FKvisit_id→ visits.idINT
- medicationTEXT
- dosageTEXT
- frequencyTEXT
- start_dateTEXT
- end_dateTEXT
- refillsINT
- PKidINT
- FKvisit_id→ visits.idINT
- test_nameTEXT
- result_valueTEXT
- unitTEXT
- reference_rangeTEXT
- is_abnormalINT
- tested_atTEXT
- PKidINT
- FKvisit_id→ visits.idINT
- amountREAL
- insurance_coveredREAL
- patient_responsibilityREAL
- statusTEXT
- billed_atTEXT
- paid_atTEXT
Need help?
Ask Sol
AI tutor — looks at your current query and nudges you, without spoiling the answer.
Solution
Try the hints first — solving it yourself is what makes it stick.