Chapter 8 · Window Functions
12. Compare Each Bill to the Previous One
Medium20 XPHealthcare
Finance wants to see how each billing amount compares to the previous one chronologically. Return id, billed_at, amount, prev_amount (using LAG), and the difference as amount_change. Order by billed_at, id.
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.