Skip to main content

Command Palette

Search for a command to run...

Using WHERE inside aggregate functions

Updated
1 min readView as Markdown
Using WHERE inside aggregate functions
C

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified

I've previously shared how aggregate functions in BigQuery SQL can pick their rows with HAVING MAX / HAVING MIN.

WHERE is now supported in preview (yes, I'm the tenth person posting it). It filters the input rows, so any aggregate can run on any subset of the data. The difference: HAVING MAX picks the extreme rows of the group; WHERE picks rows by conditions you know beforehand.

Sure, you could solve most of this before with SUM(CASE WHEN ... END), a classic SQL interview question. To me, a WHERE like this is more intuitive and pleasing to the eye. Very BigQuery!