001Level 1

Chapter 7 · CTEs

24. Billing Aging Report with Multiple CTEs

Hard40 XPHealthcare

Build a billing aging report using CTEs. First CTE computes the age in days of each unpaid bill (status != 'paid') as of '2025-01-01'. Second CTE categorizes: 0-30 days = 'Current', 31-60 = '31-60 Days', 61-90 = '61-90 Days', 91+ = '90+ Days'. Return the aging_bucket and total_amount (sum of amount, rounded to 2). Order by the bucket in the order listed.

Tables

  • 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.