Field note · August 4, 2023
The overall average hides 3 different numbers
Real group means for one column across 3 segments, and what the pooled average conceals.
payment_type splits ride-hail-trips into 3 groups. Here is what distance_km looks like inside each.
card— mean 4.87, median 3.19 (4,983 rows)wallet— mean 4.78, median 3.27 (2,125 rows)cash— mean 4.74, median 3.16 (892 rows)
Top to bottom that is 4.87 against 4.74, a spread of 2.9%. The pooled average is 4.83.
select payment_type,
count(*) as rows,
round(avg(distance_km)::numeric, 2) as mean,
percentile_cont(0.5) within group (order by distance_km) as median
from ride_hail_trips
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 card, 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.