Pick the Grain Before You Aggregate
Define query grain and align SELECT, GROUP BY, and aggregate expressions to it.
Aggregation is not just math; it is math at a declared grain. The GROUP BY list is your contract with the reader. Name the output row Before writing SUM or COUNT, say what one output row represents. "One row per month" needs GROUP BY month. "One row per customer per month" needs GROUP BY customer and month. Protect entity counts Joins can multiply rows. Counting users after joining to events counts user-event rows unless you use COUNT(DISTINCT users.id) or pre-aggregate events to one row per user. Align SELECT with GROUP BY A selected non-aggregate column must either be part of…
Sign up free — one personalized lesson every day, matched to your role and goals.
Already have an account? Sign in