QUERY SUM or AVG Is Too Low: Mixed Types Are Treated as Blank
QUERY's SUM can match SUM and still be short of the amounts you can see, because text that looks like a number is treated as blank. When the column is mostly text, QUERY sum returns #VALUE!.
本文目錄
The amounts you can see in the column add up to 549. SUM and QUERY’s sum both return 480. There is no parse error. Three cells just are not numbers, even though they look like amounts, so they never enter the total.
That is a different failure from an empty QUERY result (#N/A, “Query completed with an empty output”) and from “Unable to parse query string.” Those write-ups are QUERY #N/A: Query Completed With an Empty Output 與 QUERY unable to parse. These figures were measured in Google Sheets. Excel has no QUERY function.
Put SUM and QUERY on the same column
Enter this in A1:D10. The Amount cells are not all the same type. In D4 type a leading apostrophe and 12, so the cell is text. In D6 type a leading apostrophe, 12.00, and one trailing space (LEN of that cell is 6). Leave D7 empty. In D8 type a leading apostrophe and $45.00. D5 is the number 0, not a blank.
| Campaign | Week | Channel | Amount |
|---|---|---|---|
| Spring | 2026-11-02 | 搜尋 | 120 |
| Spring | 2026-11-02 | Social | 85 |
| Spring | 2026-11-09 | 搜尋 | 12 |
| Spring | 2026-11-09 | Social | 0 |
| Spring | 2026-11-16 | 搜尋 | 12.00 |
| Spring | 2026-11-16 | Social | |
| Fall | 2026-11-02 | 搜尋 | $45.00 |
| Fall | 2026-11-09 | 搜尋 | 200 |
| Fall | 2026-11-16 | Social | 75 |
If you add every displayed figure by hand you get 549. The cells that are actually numbers are 120, 85, 0, 200, and 75, which add to 480.
=SUM(D2:D10)
=QUERY(A1:D10,"select sum(D)",1)
=AVERAGE(D2:D10)
=QUERY(A1:D10,"select avg(D)",1)
SUM returned 480. QUERY returned a header sum Amount and the same 480. They agree with each other, and both are short of 549. The text amounts are invisible to both functions, not only to QUERY.
AVERAGE returned 96, which is 480 divided by the five numeric cells (the 0 counts, the blank and the text do not). QUERY avg(D) returned a header avg Amount and the same 96.
Google’s QUERY documentation says that when a column mixes types, the majority type wins and minority values are treated as null. That majority-type rule is also a short trap in FILTER vs QUERY. This page is the too-small aggregate, not a comparison of the two functions.
Find the cells that never entered the total
=ISNUMBER(D4)
=ISTEXT(D4)
D4 (the text 12), D6 (the text 12.00 plus a trailing space), and D8 (the text $45.00) are text: ISTEXT is TRUE and ISNUMBER is FALSE. D5 is a number. D7 is blank: both ISNUMBER 與 ISTEXT are FALSE.
=COUNT(D2:D10)
=COUNTA(D2:D10)
=SUMPRODUCT(--ISNUMBER(D2:D10))
=SUMPRODUCT(--ISTEXT(D2:D10))
COUNT and the ISNUMBER product were 5. COUNTA was 8 (the blank is not counted). The ISTEXT product was 3.
Coerce, then re-run QUERY
A helper in E2, filled down:
=IF(D2="","",VALUE(D2))
In this en_US file, VALUE turned the text 12 into 12, the text 12.00 plus a trailing space into 12, and the text $45.00 into 45. The blank stayed blank. The helper then held 120, 85, 12, 0, 12, a blank, 45, 200, and 75.
=QUERY({A1:C10,E1:E10},"select sum(Col4)",1)
That returned a header sum Amount_num 與 549. Changing the QUERY string alone will not bring a minority text value back while it still sits in a numeric-majority Amount column. Methods for a larger import are in Convert numbers stored as text.
When the column is mostly text
On a copy of the same rows, enter most of the amounts as text (120, 85, 12, 0, 12.00 plus a trailing space, a blank, $45.00, 200) and leave only the last cell as the number 75.
=SUM(D2:D10)
=QUERY(A1:D10,"select sum(D)",1)
SUM returned 75. QUERY returned #VALUE!: “Unable to parse query string for Function QUERY parameter 2: AVG_SUM_ONLY_NUMERIC.” This is the setup in which SUM and QUERY actually disagree, and QUERY does not fail quietly with a zero. It is a parse-flavored message; the string select sum(D) is valid, but the column is not numeric.
One bad cell in a group
=QUERY(A1:D10,"select A, sum(D) where A is not null group by A",1)
=SUMIF(A2:A10,"Spring",D2:D10)
=SUMIF(A2:A10,"Fall",D2:D10)
On the original mixed column, both SUMIF results were the numeric-only totals: Spring 205 (120 + 85 + 0) and Fall 275 (200 + 75). QUERY returned the same two group totals, with headers Campaign 與 sum Amount. Each group is short of the figures you can see (Spring’s displayed amounts add to 229, Fall’s to 320), because the text amounts sit in both campaigns.
Arithmetic inside QUERY, and a FILTER check
=QUERY(A1:D10,"select sum(D)/count(D)",1)
=QUERY(A1:D10,"select avg(D)",1)
=SUM(FILTER(D2:D10,ISNUMBER(D2:D10)))
sum(D)/count(D) returned 96, the same as avg(D). Its header in this file was quotient(sum Amountcount Amount), with a leading space and no separator between the two labels. Neither form returned a type error on the majority-numeric column. The FILTER check returned 480, the same numeric-only total as SUM.
Sources
- Google Docs Editors Help: QUERY function (mixed data types: majority type wins, minority is null): https://support.google.com/docs/answer/3093343
- Google for Developers: Query Language Reference (aggregation:
sum,avg,count): https://developers.google.com/chart/interactive/docs/querylanguage - Google Docs Editors Help: SUM, AVERAGE, VALUE, ISNUMBER, ISTEXT