001Level 1

Chapter 7 · CTEs

29. Patient Visit Summary with Diagnosis Counts and Billing

Hard40 XPHealthcare

Using multiple CTEs, build a patient visit summary. For each patient, show: full name, total_visits, distinct_diagnoses (number of unique diagnoses across all visits), total_billed (rounded to 2 decimals), and avg_bill_per_visit (rounded to 2). Only include patients with at least one visit. Order by total_billed 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
  • 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.

Peek at the solution?

You can still earn the XP afterwards by submitting a working query — but you'll learn more if you give it one more try.