Provider Analytics Path · Mission 22 of 25Expert

Patient satisfaction trend with moving average

Use CTEs, window frames (ROWS BETWEEN), LAG, and CASE flags

Back to Provider Analytics

The Brief

Rita VasquezDirector of Patient Experiencepatient-experience

Show monthly average patient satisfaction scores with a 3-month moving average alongside. Filter out satisfaction scores of 0 or negative — those are clearly data errors. Also flag any month where satisfaction dropped more than 10% from the prior month. I'm presenting to the patient advisory council next week.

You'll practice

CTEROWS BETWEENLAG

Tables & columns available

fact_quality_metricsfact7 columns
ColumnTypeKey
metric_idINTPK
encounter_idINTFK → fact_encounters
patient_idINTFK → dim_patients
metric_typeTEXT
metric_valueREAL
measurement_dateTEXT
reported_byTEXT

Hints (3)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

CTE for monthly averages with the bad values filtered out. The outer query adds a windowed moving average and a drop flag

Hint 2

A moving average needs a window frame that includes the current row and the prior two. The drop flag compares current to the lagged value

Hint 3

`AVG(avg_score) OVER (ORDER BY year, month ROWS BETWEEN ___ PRECEDING AND ___ ROW) AS moving_avg_3m` — flag via `CASE WHEN (avg_score - LAG(avg_score) OVER(...)) / LAG(avg_score) OVER(...) < ___ THEN 'DROP' END`