Search code examples
sqlgoogle-apps-scriptgoogle-sheetsformulagoogle-query-language

Joining ( {} ) 2 separately working queries gives a value error


I've got this query

=query(importrange("link";"Sales!A2:F");"Select Col1,Col3,Col4,Col5,Col6  where Col2='Manager'")

That gets the data in this format

Date | Name | Sum | DOP | Comment

And I've got this formula

=query(importrange("link";"Sales!A2:F");"Select Sum(Col4) where Col2='Manager' label Sum(Col4) ''")

That gives me the total of column "Sum"

They work fine separetly,but when I join them together with {} , like so:

={query(importrange("link";"Sales!A2:F");"Select Col1,Col3,Col4,Col5,Col6  where Col2='Manager'");query(importrange("link";"Sales!A2:F");"Select Sum(Col4) where Col2='Manager' label Sum(Col4) ''")}

I get a

In ARRAY_LITERAL, an Array Literal was missing values for one or more rows

The intended behavior is that the joined formula will output the table of data, and underneath the total of sum.

Reason for the formula, eventually there will be 12 of those that will additionally sort the source data by month and get the "Table/Total" of each month.

Maybe "query" is not the best way to do it, if so then I will be thrilled to learn a more efficient way!

Please keep in mind that in your region, the ";" inside formulas might need to be replaced with ","


Solution

  • The number of columns must be equal, when joining array literals vertically.

    =ARRAYFORMULA({query(importrange("link";"Sales!A2:F");"Select Col1,Col3,Col4,Col5,Col6  where Col2='Manager'");{""\""\query(importrange("link";"Sales!A2:F");"Select Sum(Col4) where Col2='Manager' label Sum(Col4) ''")\""\""}})