Field note · November 26, 2023
The overall average hides 4 different numbers
Real group means for one column across 4 segments, and what the pooled average conceals.
channel splits retail-orders into 4 groups. Here is what unit_price_usd looks like inside each.
marketplace— mean 75.55, median 50.94 (1,151 rows)ios— mean 74.60, median 50.81 (1,729 rows)android— mean 71.15, median 48.11 (1,386 rows)web— mean 70.99, median 47.75 (2,734 rows)
Top to bottom that is 75.55 against 70.99, a spread of 6.4%. The pooled average is 72.66.
select channel,
count(*) as rows,
round(avg(unit_price_usd)::numeric, 2) as mean,
percentile_cont(0.5) within group (order by unit_price_usd) 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 marketplace, 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.