Chapter 8 · Window Functions
62. Cumulative Abnormal Lab Results per Patient Over Time
Hard40 XPHealthcare
Track the accumulation of abnormal lab results for each patient over time. For each lab result, show the patient's full name, test_name, tested_at, is_abnormal, and a running count of abnormal results for that patient up to and including this test. Results with the same tested_at are counted in lab result id order. Sort by patient_name, then tested_at.
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
- test_nameTEXT
- result_valueTEXT
- unitTEXT
- reference_rangeTEXT
- is_abnormalINT
- tested_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.