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.

FormulasReporting Setup time: 5 minutes

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

Filter and sort: orders over $100, biggest firstFormula
=QUERY(Sales!A:E, "select A, B, E where E > 100 order by E desc", 1)
Revenue by customerFormula
=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)
Top 5 products by quantityFormula
=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)
Revenue by month (month() is 0-based, hence +1)Formula
=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)
Between two dates typed in G1 and G2Formula
=QUERY(Sales!A:E, "select * where A >= date '"&TEXT(G1, "yyyy-mm-dd")&"' and A <= date '"&TEXT(G2, "yyyy-mm-dd")&"'", 1)
Search box: product contains whatever is typed in G3Formula
=QUERY(Sales!A:E, "select * where lower(C) contains '"&LOWER(G3)&"'", 1)
Pivot: customers down, products acrossFormula
=QUERY(Sales!A:E, "select B, sum(E) where B is not null group by B pivot C", 1)

Good to know

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.