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.

CampaignWeekChannelAmount
Spring2026-11-02搜尋120
Spring2026-11-02Social85
Spring2026-11-09搜尋12
Spring2026-11-09Social0
Spring2026-11-16搜尋12.00
Spring2026-11-16Social
Fall2026-11-02搜尋$45.00
Fall2026-11-09搜尋200
Fall2026-11-16Social75

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

Oscar

寫 Excel 與 Google Sheets 的公式和排錯。

Google Sheets, 1 December 2026, new private unshared spreadsheet (locale en_US, 1000-row grid, America/New_York). Formulas were entered as user input through the Sheets API. Error text was read from the Sheets API error message. On the mixed Amount column (five numbers, three text amounts, one blank), SUM(D2:D10), QUERY select sum(D), and SUM(FILTER(D2:D10,ISNUMBER(D2:D10))) each returned 480. AVERAGE(D2:D10), QUERY select avg(D), and QUERY select sum(D)/count(D) each returned 96. COUNT 5, COUNTA 8, SUMPRODUCT of ISNUMBER 5, of ISTEXT 3. D4 (text 12), D6 (text 12.00 plus a trailing space, LEN 6), and D8 (text $45.00) were ISTEXT TRUE; D5 (0) was a number; D7 (blank) was neither. VALUE of D6 and of D8 both succeeded in this en_US file (12 and 45). QUERY of the VALUE helper returned 549 (header sum Amount_num). SUMIF Spring 205, SUMIF Fall 275. QUERY group by A returned Spring 205 and Fall 275 (headers Campaign, sum Amount). On a majority-text copy of the same amounts, with only D10 stored as the number 75, SUM returned 75 and QUERY select sum(D) returned #VALUE!, "Unable to parse query string for Function QUERY parameter 2: AVG_SUM_ONLY_NUMERIC". Excel was not run; QUERY does not exist there.

本主題下一篇:表格錯誤與動態陣列 修復 IMPORTRANGE 的 #REF! 「You Need to Connect These Sheets」錯誤