Google Sheets – QUERY – SUM() based on empty column

google sheetsgoogle-apps-scriptgoogle-sheets-query

google sheets image

I need to obtain the sum(B) if the column has empty values I want to reflect 0.
I am automating this process for multiple sheets using app-script and need to use QUERY.
Any suggestions?

Best Answer

Use n() to convert blanks to zeros, like this:

=arrayformula( query( n(A2:B), "select sum(Col1), sum(Col2)", 0 ) )