FormulaCraft

How to use QUERY to group by multiple columns in Excel and Google Sheets

Topic:QUERY (Google Sheets SQL)

Heads up: Excel and Google Sheets do this differently.

Excel
=SUMIFS(D2:D10,B2:B10,F2,C2:C10,G2)
Google Sheets
=QUERY(A1:D10,"SELECT B, C, SUM(D) WHERE A IS NOT NULL GROUP BY B, C ORDER BY B, C LABEL SUM(D) 'Revenue'",1)

Try it with your data

Edit the grid or formula, then run it through a real spreadsheet engine — no signup.

Sample data — click any cell to edit

Runs server-side · free · no signup

Step by step

  1. 1Prepare your table with at least three grouping-relevant columns — for example: Date (A), Region (B), Product (C), Revenue (D).
  2. 2Click an empty cell, such as F1, to anchor the output.
  3. 3Enter the formula: =QUERY(A1:D10,"SELECT B, C, SUM(D) WHERE A IS NOT NULL GROUP BY B, C ORDER BY B, C LABEL SUM(D) 'Revenue'",1).
  4. 4The output will have one row per unique (Region, Product) combination with a summed Revenue column.
  5. 5To add a count of transactions per group, change SUM(D) to SUM(D), COUNT(D) and add COUNT(D) 'Txns' to the LABEL clause.

Tips

Need it for your exact data?

Describe your columns in plain English and get the precise formula for your sheet, with the right Excel or Sheets syntax.

Frequently asked

Is there a limit to how many columns I can GROUP BY?

Practically no hard limit, but each additional GROUP BY column multiplies the number of output rows. Beyond three or four grouping columns the result usually becomes harder to read than a proper pivot table.

Can I group by a calculated column in QUERY?

QUERY does not support arbitrary expressions in GROUP BY directly. You can pre-compute a helper column in your sheet, or use the scalar functions QUERY provides (year(), month(), day(), hour()).

How do I replicate multi-column GROUP BY in Excel without a pivot table?

Use SUMIFS with multiple criteria ranges: =SUMIFS(D:D, B:B, region_cell, C:C, product_cell). To generate the unique group list automatically, use the UNIQUE function (Excel 365) or a helper column.

More on QUERY (Google Sheets SQL)

See all →

Formulas used

Written and reviewed by FormulaCraft Team. Each formula on this page is run through our verification engine before publishing.

Last reviewed: