Spreadsheet & Formula Studio

Google Sheets QUERY: Group By and Sum a Column

How to build a Google Sheets QUERY that groups by a column and sums another, with a date filter, labels, sorting and a 20-row preview to check the totals.

PerkTonic Team4 min read
Cover of the guide "Google Sheets QUERY: Group By and Sum a Column"

A revenue-per-region summary in Google Sheets is a QUERY with sum() and a group by clause. The syntax is the hard part, because the query is a text string with its own rules for dates, labels and sorting. The Google Sheets QUERY Builder builds the formula from your choices, so you do not type the string by hand. This guide shows the result and explains each part.

The formula, built step by step

For sales data in Sales!A1:F, with the order date in column A, the region in column B and the revenue in column F, the revenue per region since February is:

=QUERY(Sales!A1:F, "select B, sum(F), count(A) where A >= date '2026-02-01' group by B order by sum(F) desc label sum(F) 'Revenue', count(A) 'Orders'", 1)

Read it clause by clause:

  • select B, sum(F), count(A) returns the region, the total of column F and the number of orders.
  • where A >= date '2026-02-01' keeps only the rows from 1 February 2026.
  • group by B makes one row per region. Every column in select that is not aggregated must be listed here.
  • order by sum(F) desc puts the largest total first.
  • label sum(F) 'Revenue', count(A) 'Orders' sets the column headers.
  • The final 1 tells QUERY that the data has one header row.
Google Sheets QUERY Builder showing a QUERY formula with group by, sum and a date filter
The builder writes the QUERY formula for revenue per region since February.

In the builder, choose the range, the columns and the aggregate for each, the filter, the grouping and the order. The formula is written for you, and you can copy it into the sheet.

Dates in QUERY: date 'yyyy-mm-dd'

A date inside a QUERY string must be written as the word date followed by the date in the form yyyy-mm-dd, in single quotes: date '2026-02-01'. A date in the style of your local format, or a cell reference placed directly in the string, does not work and returns an error or no rows.

To take the date from a cell, build the string with text:

"select B, sum(F) where A >= date '"&TEXT(H1,"yyyy-mm-dd")&"' group by B"

This puts the date of cell H1 in the right form. The same rule applies to the upper bound of a period. For a month, use a lower bound with >= and an upper bound with <, as explained for Excel in SUMIFS Between Two Dates: Sum One Month in Excel.

Labels and sorting

Without label, QUERY names the aggregate columns itself, for example sum F, which is hard to read in a report. Label gives each aggregate a header in single quotes: label sum(F) 'Revenue', count(A) 'Orders'. Label comes after order by in the clause order, and each pair is separated from the next with a comma.

Order by accepts a column or an aggregate. Add desc for the largest first, or leave it out for ascending order. If a clause is in the wrong position, QUERY returns a parse error, because the clauses must follow the order select, where, group by, pivot, order by, limit, offset, label, format and options.

The builder has no controls for the format and options clauses. If you add them by hand with Edit the query text by hand, they stay in the formula, and the preview says that it did not apply them.

Aggregate menu set to sum for the revenue column in the Google Sheets QUERY Builder
The revenue column uses the aggregate sum, and the order column uses count.

Each column has an Aggregate menu with sum, count and the other functions of the query language. Choose sum for the revenue column and count for the order column.

Check the 20-row preview

Before you copy the formula, look at the preview. It shows the first 20 rows of the result and is calculated in the browser, so you can see the grouping and the totals without opening your sheet. With the example sales data, the preview shows each region with its revenue and number of orders, and for the West region the total is 11826.

Google Sheets QUERY Builder preview with the West region total of 11826 marked
The preview shows the grouped result, and the West total is marked.

Compare one region with a manual check, for example a filter on the region in your sheet and the sum in the status bar. If the totals differ, check the date filter first, then the column letters.

Limits of the preview

The preview works with up to 5 MB of pasted data, 50 MB for a CSV file and 200 columns. It shows only the first 20 rows of the result. Real data in Google Sheets can be larger, and the sheet calculates the whole result.

The preview does not apply format and options clauses that you add by hand. If you use them, check the effect in the sheet itself.

If a filter on one cell is what you need, the next guide is Google Sheets QUERY: Filter by a Cell Value. For the SQL version of the same grouping, read Convert an Excel SUMIFS Formula to SQL GROUP BY. In Excel, the same result for one region is a SUMIFS, which the Excel Formula Generator can write.

Google documents the function on its help page QUERY function, and the language of the query string is described in the Google Visualization API Query Language reference.

Do it with your own data

Open the Google Sheets QUERY Builder, enter your range and pick the region as the group and the revenue as the sum. Check the 20-row preview against one region in your sheet, then copy the formula.

Was this guide helpful?
Share

Comments