Problems / Pivot Average Order Size / Editorial
AVG(CASE WHEN quarter = X THEN amount END)
AVG
The ELSE 0 habit from SUM/COUNT pivots does not transfer to AVG — zeros are invisible to SUM but are *data* to AVG (they change the denominator). The general rule: in conditional aggregation, the ELSE branch must be the aggregate's identity element — 0 for SUM, but *NULL (absence)* for AVG, MIN, and MAX. Getting this wrong doesn't error; it just quietly reports averages diluted by phantom zeros, which is why this bug ships to production so often.
ELSE 0
Solve Pivot Average Order Size yourself →