Field note · April 25, 2024
The overall average hides 6 different numbers
Real group means for one column across 6 segments, and what the pooled average conceals.
category splits retail-orders into 6 groups. Here is what discount_pct looks like inside each.
home— mean 0.10, median 0.06 (1,279 rows)electronics— mean 0.10, median 0.00 (960 rows)grocery— mean 0.10, median 0.00 (1,188 rows)apparel— mean 0.09, median 0.00 (1,696 rows)sports— mean 0.09, median 0.00 (789 rows)beauty— mean 0.09, median 0.00 (1,088 rows)
Top to bottom that is 0.10 against 0.09, a spread of 16.1%. The pooled average is 0.10.
select category,
count(*) as rows,
round(avg(discount_pct)::numeric, 2) as mean,
percentile_cont(0.5) within group (order by discount_pct) as median
from retail_orders
group by 1
order by mean desc;The groups are close enough that the pooled average is a fair summary. That is worth confirming rather than assuming: the check costs one query and the failure mode is invisible.
Notice the mean and median columns disagree most in home, where the mean sits well above the median. Always compute both in the group-by. The comparison between them per segment is free and tells you whether you are looking at a level difference or a tail difference.
This is the setup for Simpson's paradox — the case where every segment moves one way and the total moves the other.