How to put number in google sheet query - google-sheets

I want to do a query in google sheet something like:
=query({BY6:CH29},"
select Col2,sum(Col5*Col9*{144}/Col4),sum(Col5*Col10*{144}/Col4),sum(Col7*Col9*{144}/Col4),sum(Col7*Col10*{144}/Col4)
where Col2 is not null
group by Col2
order by Col1
",1)
How do I do it? I think the number 144 made the formula error
the query return #VALUE!
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "" " "" at line 2, column 21. Was expecting: ")" ...

Try this:
=query({BY6:CH29},"
select Col1, Col2,SUM(Col5)*SUM(Col9)*144/SUM(Col4),SUM(Col5)*SUM(Col10)*144/SUM(Col4),sum(Col7)*SUM(Col9)*144/SUM(Col4),sum(Col7)*SUM(Col10)*144/SUM(Col4)
where Col2 is not null
group by Col2,Col1
order by Col1
",1)

Related

Google Query Select All or Specific Text via Cell Reference

The formula below is working to select All, but throws any error when I go to search specific text. ON a seperate tab, I created another query that works with the specific text.
Cell reference A4 is the drop down list with All, Status 1, and Status 2 that I would like to be able to select data from.
=Query({QUERY(importrange("xxxxx", "'"&A1&"'!A3:Q150"), "SELECT * WHERE Col6 >= "&A3&" AND Col6 <= "&B3&"", 0)}, "SELECT Col1, Col2, Col3, Col4" & IF(A4= "All",, "Where Col4 = '"&A4&"'"),0)
I have the Status 1 and Status 2 working on a seperate query, and the All working on current query, trying to make them work on one sheet. Currently only All works. When status 1 or status 2 are selected it throws this error.
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "Col4 "" at line 1, column 36. Was expecting one of: "where" ... "group" ... "pivot" ... "order" ... "skipping" ... "limit" ... "offset" ... "label" ... "format" ... "options" ... "," ... "" ... "+" ... "-" ... "/" ... "%" ... "" ... "/" ... "%" ... "+" ... "-" ...
Try to add a space before where or after the first Col4
=Query({QUERY(importrange("xxxxx", "'"&A1&"'!A3:Q150"),
"SELECT * WHERE Col6 >= "&A3&" AND Col6 <= "&B3&"",0)},
"SELECT Col1, Col2, Col3, Col4" & IF(A4= "All",, " Where Col4 = '"&A4&"'"),0)
try:
=QUERY(QUERY(IMPORTRANGE("xxxxx", A1&"!A3:Q150"),
"where Col6 >= "&A3&"
and Col6 <= "&B3, ),
"select Col1,Col2,Col3,Col4"&
IF(A4="All",," where Col4 = '"&A4&"'"), )
you can drop all of these:
array brackets {} no need coz inner QUERY is not a range
single quotes within IMPORTRANGE coz it applies the same rules as with INDIRECT
zero 0 as the third QUERY argument coz it can be just omitted
select * if you want all columns then just skip the whole select argument
ending QUERY with &"" after B3 coz its totally redundant

Getting "Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: C" error

I am trying to pull data from a number of sheets to one dashboard sheet. When I add more than sheet I get this error: Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: C
It works fine with only one sheet (July2022) but as soon as I add one or more to it I get the error.
=QUERY({JULY2022!A4:EC34;AUGUST2022!A4:EC34;SEPTEMBER2022!A4:EC34;OCTOBER2022!A4:EC34;NOVEMBER!A4:EC34;DECEMBER!A4:EC34},
"select C, A
where A >= date'"&TEXT(TODAY(),"yyyy-mm-dd")&"'
and C is not null
ORDER BY A ASC LIMIT 8"
)
When you wrap the data into curly braces, which is necessary when you combine multiple sources, you've to refer to the columns as Col1, Col2, Col3,... You can't use the column letters.
You can use:
=QUERY({JULY2022!A4:EC34;AUGUST2022!A4:EC34;SEPTEMBER2022!A4:EC34;OCTOBER2022!A4:EC34;NOVEMBER!A4:EC34;DECEMBER!A4:EC34},
"select Col3, Col1
where Col1 >= date'"&TEXT(TODAY(),"yyyy-mm-dd")&"'
and Col3 is not null
ORDER BY Col1 ASC LIMIT 8"
)

Importhtml rename header name Google sheets

I have this formula
=QUERY({
IFERROR(QUERY(IMPORTHTML("url1","table",2),"select Col1, Col5,' '
where Col1 is not null
label ' ''Hour 2'",1), {"",""});
IFERROR(QUERY(IMPORTHTML("url2","table",2),"select Col1, ' ',Col5
where Col1 is not null offset 1",1), {"",""}) })
The auto generated first two column headers should be labeled 'Company', 'Hour 1'
I tried label Col1 'Company' or label A 'Company' but neither worked.
How to achieve this?
If I understood, you are joining 2 queries of IMPORTHTML another query.
However, in the first column, you are selecting 2 columns and in the second one, you are fetching other 2 columns.
If you want to label any column, you can address that by doing the following in the query section inside query:
select Col1, Col2 where Col1 is not null label Col1 'Company', Col5 'Hour 1'
^^^^ **** ^^^^^^^^^^^^^^^^^^^^ *************
You query can become the following:
{
IFERROR(QUERY(IMPORTHTML("url1","table",2),"select Col1, Col5,' '
where Col1 is not null
label Col1 'Company', Col5 'Hour1', ' ' 'Hour 2'",1), {"",""});
IFERROR(QUERY(IMPORTHTML("https://en.wikipedia.org/wiki/List_of_countries_by_population_(United_Nations)","table",4),"select Col1, ' ',Col5
where Col1 is not null offset 1",1), {"",""})
}
Note: I removed the query({something; something}) as it has the same result as just {something; something}

Query returning #VALUE

My query:
=Query(importrange(B5 ,"Sheet1!A1:G700"),"select Col3 WHERE Col4 = '"&Q145&"'",QUERY(importrange(B5 ,"Sheet1!A1:G700"),"select Col2 WHERE Col3 = '"&Q145&"'", 0))
is returning:
#VALUE! Query parameter 3 expects number values`. But W106 is a text and cannot be coerced to a number.
I have the cell format set to automatic, I've also tried setting it to plain text. Kind of lost, it works without the second query statement, unfortunately, this query is used on multiple sheets with a different number of columns so this is my attempt and making this query work with both formats.
it isn't clear what exactly you are after so try:
=QUERY({IFERROR(QUERY(IMPORTRANGE(B5, "Sheet1!A1:G700"),
"select Col3 where Col4 = '"&Q145&"'", 0),"");
IFERROR(QUERY(IMPORTRANGE(B5, "Sheet1!A1:G700"),
"select Col2 where Col3 = '"&Q145&"'", 0),"")},
"where Col1 is not null", 0)

How to use ARRAYFORMULA in query where clause?

In cell C1:C of my table I got 6 rows with ticket id's. I like to search different spreadsheets to search for those ticket id's and calculate the total hours spent on that ticket.
I have it working using the following formula:
=QUERY({IMPORTRANGE("SPREADSHEETID";"B3:B")\ARRAYFORMULA(TO_PURE_NUMBER(IMPORTRANGE("SPREADSHEETID";"F3:F")-IMPORTRANGE("SPREADSHEETID";"E3:E")))};"SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''")
In this example, C1 is where the ticket ID can be found.
Now I thought I could just wrap QUERY in a ARRAYFORMULA and use C1:C instead of just C1 but that won't work. Now I could just copy and paste the above formula in every cell but there must be a cleaner way.
ANSWER
I used the following formula to make it work, thanks to Max's answer below.
=QUERY(
{
IMPORTRANGE("SPREADSHEETID";"B3:B")\
ARRAYFORMULA(
TO_PURE_NUMBER(
IMPORTRANGE("SPREADSHEETID";"F3:F") - IMPORTRANGE("SPREADSHEETID";"E3:E")
)
)
};
"
SELECT Col1, SUM(Col2)
WHERE Col1 = '" & JOIN("' OR Col1 = '";FILTER(C:C; C:C <> "")) & "'
GROUP BY Col1
LABEL SUM(Col2) ''
")
Sample formula is:
=QUERY({A:B},"select * where Col1 = '"&JOIN("' or Col1 = '",FILTER(D2:D,D2:D<>""))&"'")
No, one cannot create an array of query strings and use arrayformula(query(...)) to run them all at once.
Alternative: instead of
SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''
use the query
SELECT Col1, SUM(Col2) GROUP BY Col1
elsewhere on the sheet, and then use vlookup to look up the sum for each value of Col1 that you want. vlookup can be used inside of arrayformula like this:
=arrayformula(vlookup(C1:C10, E:F, 2, 0))
looks up each of values in C1..C10 in the column E (exact match required) and returns the corresponding value in column F (2nd column of the searched range).

Resources