Yes, you can group by multiple columns in a Google Sheets QUERY by listing them after the GROUP BY clause, separated by commas. For example, =QUERY(A1:C100, "select A, B, sum(C) group by A, B", 1) groups by columns A and B and sums column C for each unique pair.
How the GROUP BY Clause Works in QUERY
The QUERY function uses a SQL-like language. The GROUP BY clause collapses rows that share the same values in the specified columns, and you must use an aggregate function (like sum(), count(), avg(), max(), min()) on the other columns. When you list multiple columns after GROUP BY, the grouping is based on the unique combination of those columns. For instance, if you group by Month and Product, each row in the result represents one month-product pair, not just one month or one product.
Important rule: every column in the SELECT clause must either be listed in GROUP BY or wrapped in an aggregate function. If you select a column that isn’t grouped or aggregated, the query returns an error like “Can’t group by without aggregate function”.
Real Example: Sales Data Grouped by Region and Product
Suppose your data is in A1:D20, with headers in row 1: A=Date, B=Region, C=Product, D=Sales. To get total sales per region and product, use:
=QUERY(A1:D20, "select B, C, sum(D) group by B, C", 1)
This returns three columns: Region, Product, and the sum of Sales for each unique pair. The 1 tells QUERY that row 1 contains headers, so it uses them in the result. If your data has no header row, use 0 instead.
You can also sort the grouped result by adding an ORDER BY clause. For example:
=QUERY(A1:D20, "select B, C, sum(D) group by B, C order by sum(D) desc", 1)
This lists the highest total sales first.
Using Aggregate Functions with Multiple Grouping Columns
You can use any aggregate function with multiple grouping columns. Common ones include:
count()– counts rows in each groupavg()– average of a numeric columnmax()/min()– highest/lowest valuesum()– total of a numeric column
You can also use multiple aggregates in the same query. For example:
=QUERY(A1:D20, "select B, C, sum(D), avg(D) group by B, C", 1)
This returns both the total and the average sales for each region-product pair.
Grouping by Date or Time Columns
If you group by a date column directly, each unique date becomes its own group. To group by month or year, you need to extract that part using the date functions in the QUERY string. For example, to group by month and product:
=QUERY(A1:D20, "select year(A), month(A), C, sum(D) group by year(A), month(A), C", 1)
Note that month() returns 0 for January, 1 for February, etc. If you want the month name, you can use format in the select clause, but that becomes more complex. A simpler approach is to add a helper column in your sheet that contains the month name (e.g., using =TEXT(A2,"mmmm")) and then group by that helper column.
Common Errors and How to Fix Them
One frequent mistake is forgetting to include all selected columns in GROUP BY. For example, select B, C, sum(D) group by B fails because C is not grouped or aggregated. The error message says: “Column C is not in the GROUP BY clause”. Fix: add C to the GROUP BY list.
Another issue is using where with a grouped column incorrectly. The WHERE clause filters rows before grouping, so you can use it on raw columns, but you cannot use aggregate functions in WHERE. If you need to filter groups based on an aggregate, use HAVING instead. Example:
=QUERY(A1:D20, "select B, C, sum(D) group by B, C having sum(D) > 100", 1)
This returns only groups where the total sales exceed 100.
Alternatives to QUERY for Grouping
If QUERY feels intimidating, you can use pivot tables (Insert > Pivot table) which allow grouping by multiple fields via drag-and-drop. Pivot tables are more interactive and allow you to expand/collapse groups. Another alternative is the UNIQUE function combined with SUMIFS, but that becomes unwieldy for multiple grouping columns. For simple grouping, you can also use the GROUP BY in a Google Sheets pivot table, which is often easier for non-technical users.
Frequently Asked Questions
Can I group by more than two columns? Yes, you can list as many columns as you need after GROUP BY, separated by commas. For example, group by A, B, C groups by all three.
Does the order of columns in GROUP BY matter? The order affects the sort order of the result. The query groups correctly regardless of order, but the output is sorted by the first grouping column, then the second, etc. If you want a different sort, use ORDER BY.
Why does my QUERY return an error when I use a column label? If you use column labels (like ‘A’ or ‘B’) in the select clause, they must match the actual column letters. If you use header names, you need to quote them and ensure they are exactly as in the header row. For example, select Region, Product, sum(Sales) group by Region, Product works only if the headers are exactly ‘Region’, ‘Product’, and ‘Sales’.
Can I use QUERY with a different sheet in the same spreadsheet? Yes, reference the sheet name with a range like 'Sheet2'!A1:D20 inside the QUERY function.
Does QUERY work in Excel? No, QUERY is a Google Sheets function. In Excel, you can use the GROUPBY function (Excel 365) or a pivot table to achieve similar grouping.
How do I group by a calculated column? You cannot directly group by a calculated expression in QUERY. Instead, add a helper column in your sheet with the formula, then group by that column.
Related
- Generate 6 Digit Random Number in Java
- Sign Out Shortcut Windows: 5 Fast Ways
- Google Sheets QUERY Clause Guide
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Data Analysis in Excel: Methods and ExamplesYes—Microsoft Excel can analyze data using formulas, PivotTables, charts, filters, and tools such as Power Query and the…
- Conditional Formatting in Excel: Rules and ExamplesConditional formatting in Excel automatically changes how cells look when their values meet conditions you define. Select the…
- Excel Shortcut for Filling a Cell YellowIn Excel for Microsoft 365 on Windows, press Alt, then H, then H to open the Fill Color…
- Project Tracker Spreadsheet in Google Sheets: Setup GuideYes. You can make a project tracker spreadsheet in Google Sheets by creating a sheet with one row…
- How to Change Your Outlook Inbox View and Layout SettingsTo change how your Outlook inbox looks, open the view or layout controls for your version of Outlook,…
- Excel Keyboard Shortcut to Insert a NoteIn Excel for Microsoft 365 on Windows, select a cell and press Shift+F2 to insert a note. If…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.