Recipes / Recipe 7
QUERY function patterns: totals by customer, by month, top 5 and more
Build live summaries with one formula: filter, group, pivot and sort, without pivot tables.
You'll need: Google Sheets. Examples assume a Sales tab: A Date, B Customer, C Product, D Qty, E Amount, with headers in row 1.
The patterns
=QUERY(Sales!A:E, "select A, B, E where E > 100 order by E desc", 1)=QUERY(Sales!A:E, "select B, sum(E) where B is not null group by B order by sum(E) desc label sum(E) 'Revenue'", 1)=QUERY(Sales!A:E, "select C, sum(D) where C is not null group by C order by sum(D) desc limit 5 label sum(D) 'Units'", 1)=QUERY(Sales!A:E, "select year(A), month(A)+1, sum(E) where A is not null group by year(A), month(A)+1 label year(A) 'Year', month(A)+1 'Month', sum(E) 'Revenue'", 1)=QUERY(Sales!A:E, "select * where A >= date '"&TEXT(G1, "yyyy-mm-dd")&"' and A <= date '"&TEXT(G2, "yyyy-mm-dd")&"'", 1)=QUERY(Sales!A:E, "select * where lower(C) contains '"&LOWER(G3)&"'", 1)=QUERY(Sales!A:E, "select B, sum(E) where B is not null group by B pivot C", 1)Good to know
- The last argument (
1) is the number of header rows. Getting it wrong is the most common cause of odd results. - QUERY uses the majority data type in each column; minority values (e.g. a few text entries in a number column) are treated as empty. Keep columns consistent.
- Columns are letters when querying a range on a sheet, but
Col1, Col2…when querying an array such as{...}or IMPORTRANGE. - Dates in a query string must use the
date 'yyyy-mm-dd'format, which is why the TEXT() wrapper is there.
Do it yourself
Recipe Club: $12/month
Prefer to set things up yourself? Join the Recipe Club and get access to a private GitHub repository of ready-made, tested automations for Sheets, Apps Script, Python and Node. Each one comes with setup notes and sample data.
- A new recipe added every week
- Everything in the free library, plus the longer, more complete versions
- Use them in your own business as much as you like
- Cancel anytime by email (or the Stripe customer portal once available)
Recipe Club opens soon: join the waitlist
You'll need a free GitHub account. After checkout, reply with your GitHub username and we'll send the repo invite, usually within one business day.
Or buy a one-time recipe pack ($19–$29)
- Clean Data Pack $19
Dedupe, cleaning, merging and normalizing scripts and formulas for messy exports, with a sample sheet.
Coming soon - Forms & Inbox Pack $19
Form auto-replies, request routing, a lead log and follow-up nudges, ready to paste into Apps Script.
Coming soon - Money Ops Pack $29
Invoice reminders, a monthly revenue dashboard with QUERY, a receipts log and nightly backups.
Coming soon
Written with AI assistance. Script logic is unit-tested; formulas follow documented Google Sheets behavior. Always try new automations on a copy of your sheet first.