Fix QUERY CANNOT_GROUP_WITHOUT_AGG in Google Sheets
Google Sheets QUERY returns CANNOT_GROUP_WITHOUT_AGG when GROUP BY has no aggregate in SELECT. Add SUM or COUNT, or use UNIQUE for a distinct list.
本文目录
You try to list each sales rep once with Google Sheets QUERY, and the cell shows #VALUE! with this message:
Unable to parse query string for Function QUERY parameter 2: CANNOT_GROUP_WITHOUT_AGG
The usual cause is using GROUP BY as if it were DISTINCT. QUERY requires at least one aggregate in SELECT whenever you group rows.
Enter these headers in A1:C1 and the eight orders in A2:C9, typing the dates as dates rather than text.
| Date | Rep | Amount |
|---|---|---|
| 2026-11-24 | Maya | 420 |
| 2026-11-24 | Ethan | 180 |
| 2026-11-25 | Maya | 350 |
| 2026-11-25 | Olivia | 260 |
| 2026-11-26 | Ethan | 510 |
| 2026-11-26 | Maya | 290 |
| 2026-11-27 | Olivia | 390 |
| 2026-11-27 | Ethan | 240 |
This formula is the one that fails:
=QUERY(A1:C9,"select B group by B",1)
It returns the same #VALUE! 与 CANNOT_GROUP_WITHOUT_AGG message. If you only want a distinct list of reps, drop GROUP BY and use UNIQUE:
=UNIQUE(FILTER(B2:B9,B2:B9<>""))
That spilled Maya, Ethan, and Olivia, with no header and no blank. If you actually need totals, keep GROUP BY and add SUM 或 COUNT.
Why GROUP BY fails when you only select a column
The problem is not the column letter. Column B is valid, and grouping by B is a valid operation once the query includes an aggregate. Google Sheets does not treat GROUP BY as a substitute for DISTINCT.
The same #VALUE! 与 CANNOT_GROUP_WITHOUT_AGG message came back from:
=QUERY(A1:C9,"select * group by B",1)
and from:
=QUERY(A1:C9,"select A where B = 'Maya' group by A",1)
select * group by B still failed for the missing aggregate, even though columns A and C are also ungrouped. The no-aggregate check is the one that fired.
ORDER BY an aggregate does not satisfy SELECT
A less obvious version appears when you sort grouped results by an aggregate but leave that aggregate out of SELECT:
=QUERY(A1:C9,"select B where C > 0 group by B order by sum(C) desc",1)
This also returned #VALUE! 与 CANNOT_GROUP_WITHOUT_AGG. Putting sum(C) only in ORDER BY does not satisfy the requirement.
Include the aggregate in SELECT:
=QUERY(A1:C9,"select B, sum(C) where C > 0 group by B order by sum(C) desc",1)
The result, already in descending order by total, is Maya 1060, Ethan 930, and Olivia 650. An aggregate used only for sorting does not count as an aggregate in the selected output.
| Rep | sum Amount |
|---|---|
| Maya | 1060 |
| Ethan | 930 |
| Olivia | 650 |
A pivot clause without an aggregate is a different error
PIVOT also rearranges grouped data into columns, and it is not a replacement for an aggregate. This query tries to pivot dates without one:
=QUERY(A1:C9,"select B pivot A",1)
It returned #VALUE! with a different code: Unable to parse query string for Function QUERY parameter 2: CANNOT_PIVOT_WITHOUT_AGG. That is not CANNOT_GROUP_WITHOUT_AGG.
To see each rep’s sales by date, add both an aggregate and GROUP BY:
=QUERY(A1:C9,"select B, sum(C) group by B pivot A",1)
The output is a rep-by-date summary, with blank cells where that rep had no order on that date. Choose SUM because you want total sales, not merely to silence the error.
| Rep | 2026-11-24 | 2026-11-25 | 2026-11-26 | 2026-11-27 |
|---|---|---|---|---|
| Ethan | 180 | 510 | 240 | |
| Maya | 420 | 350 | 290 | |
| Olivia | 260 | 390 |
Choose the right replacement
Filter the rows without grouping
If you only want to filter rows, remove GROUP BY entirely:
=QUERY(A1:C9,"select A where B = 'Maya'",1)
This returned a Date header and Maya’s three order dates: 2026-11-24, 2026-11-25, and 2026-11-26. It does not return the amounts, because SELECT names only column A.
Use UNIQUE for a distinct list
For a list of reps, I would use:
=UNIQUE(FILTER(B2:B9,B2:B9<>""))
The formula returned Maya, Ethan, and Olivia once each, with no header. It also excluded blank cells. This is simpler than adding an unnecessary aggregate to QUERY.
If you already have a QUERY that filters the rows you need, you can wrap it in UNIQUE:
=UNIQUE(QUERY(A1:C9,"select B where C > 0",1))
That version kept the Rep header, then Maya, Ethan, and Olivia. For a header-free list, the UNIQUE(FILTER(...)) formula is easier to control.
You can also put several conditions in FILTER:
=UNIQUE(FILTER(B2:B9,C2:C9>200,B2:B9<>""))
This returned Maya, Olivia, and Ethan: each rep with at least one order above 200, in the order those qualifying rows first appear. For more examples of distinct lists, see 在 Excel 和 Google Sheets 中列出唯一值.
Add an aggregate when you need totals
If the goal is to summarize sales by rep, use:
=QUERY(A1:C9,"select B, sum(C) group by B label sum(C) 'Total Sales'",1)
Without ORDER BY, QUERY listed the reps alphabetically. Unlike UNIQUE, this formula calculates a meaningful total for each group.
| Rep | Total Sales |
|---|---|
| Ethan | 930 |
| Maya | 1060 |
| Olivia | 650 |
COUNT gives the number of orders per rep:
=QUERY(A1:C9,"select B, count(C) group by B label count(C) 'Orders'",1)
The output had headers Rep 与 Orders, then Ethan 3, Maya 3, and Olivia 2. The aggregate should answer a real question about the data; don’t add SUM 或 COUNT just to make an otherwise unnecessary grouping operation parse.
Keep one grouped column with a helper aggregate
There is another workaround when you want QUERY’s grouping and are willing to keep a helper column:
=QUERY(A1:B9,"select A, max(A) group by A label max(A) ''",1)
It returned four date groups. Both columns stored the same serials (46350 through 46353). The first column had a Date header and displayed 2026-11-24 through 2026-11-27; the helper column had a blank header and displayed the same dates as 11/24/2026 through 11/27/2026.
To drop the helper column, wrap the query in another QUERY:
=QUERY(QUERY(A1:B9,"select A, max(A) group by A label max(A) ''",1),"select Col1",1)
This returned a Date header and the four dates, formatted 2026-11-24 through 2026-11-27. The technique is useful when you specifically need QUERY’s grouping behavior, but I would not choose it for an ordinary distinct list. UNIQUE is clearer.
CANNOT_GROUP_WITHOUT_AGG is not ADD_COL_TO_GROUP_BY_OR_AGG
Both errors involve grouping, but they point to different problems. CANNOT_GROUP_WITHOUT_AGG means your grouped query has no aggregate function in its SELECT clause. ADD_COL_TO_GROUP_BY_OR_AGG means you selected a column that is neither included in GROUP BY nor aggregated.
For example:
=QUERY(A1:C9,"select B, C, sum(C) group by B",1)
returned #VALUE! with Unable to parse query string for Function QUERY parameter 2: ADD_COL_TO_GROUP_BY_OR_AGG: C. Column C appears directly in SELECT, but it is not grouped. The query already contains sum(C), so the problem is not the absence of an aggregate.
The fix is to remove the standalone C reference:
=QUERY(A1:C9,"select B, sum(C) group by B",1)
This returned headers Rep 与 sum Amount, then Ethan 930, Maya 1060, and Olivia 650. If you encounter other QUERY parsing errors involving missing columns, quotes, or clause syntax, see Google Sheets QUERY: Unable to parse query string. For a comparison of when to use QUERY and when to use FILTER, see FILTER vs QUERY in Google Sheets.
When UNIQUE or FILTER is enough
Use UNIQUE 或 UNIQUE(FILTER(...)) when you only need a list of distinct names, categories, dates, or other values, and use QUERY with GROUP BY and an aggregate when you need totals, counts, averages, maximums, or another calculation for each group. The distinction matters because adding COUNT 或 SUM to a query changes the output; it does not simply remove duplicates. If you don’t need that extra calculation, removing GROUP BY is the cleaner fix.