Google Query Select All or Specific Text via Cell Reference - google-sheets

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

Related

How to put number in google sheet query

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)

Google Sheets Query Filter by Multiple Variables

Is there a better way of writing this formula out? I mean it works but there has to be a better way of doing it where I can have multiple Search parameters.
I am using a search box to then that splits the cell with this formula =if(E1="","", if(REGEXMATCH(E1,";"),SPLIT(E1,";"),E1))
https://docs.google.com/spreadsheets/d/1-ymDylwRjd0zYnu3m0uxCz8hAavnL5Sxx39YrtlWylc/edit?usp=sharing Example Sheet
=if(E1="",QUERY(IMPORTRANGE("Spreadsheetidabcd", "RawData!A:Z"), "Select Col1,Col12,Col2,Col10,Col3 where (Col11='')",1),QUERY(IMPORTRANGE("Spreadsheetidabcd", "RawData!A:Z"), "Select Col1,Col12,Col2,Col10,Col3 where Col1 contains '"&$F$1&"' or Lower(Col12) contains Lower('"&$F$1&"') or Lower(Col12) contains Lower('"&$G$1&"')or Lower(Col10) contains Lower('"&$F$1&"')or Lower(Col10) contains Lower('"&$G$1&"')or Lower(Col2) contains Lower('"&$F$1&"')or Lower(Col2) contains Lower('"&$G$1&"')or Col3 contains '"&$F$1&"' or Col3 contains '"&$G$1&"'or Col4 contains '"&$F$1&"'or Lower(Col5) contains Lower('"&$F$1&"')"))
You can try this one, although this returns the row if any of the columns contain the values of either F1 or G1 (I assume that's what your search box is for, to find the rows containing the search term).
Formula:
=if(E1="",
QUERY(IMPORTRANGE("Spreadsheetidabcd", "RawData!A:Z"), "Select Col1,Col12,Col2,Col10,Col3 where (Col11='')",1),
arrayformula(split(query({transpose(split(textjoin(",", true, QUERY(IMPORTRANGE("Spreadsheetidabcd", "RawData!A:Z"), "Select Col1, Col12, Col2, Col10, Col3, ';' label ';' ';'")), ",;,",))}, "where lower(Col1) contains lower('"&$F$1&"') or lower(Col1) contains lower('"&$G$1&"') or Col1 contains 'Timestamp'"), ",")))
Basically, what the formula does step by step is:
It appends a column that marks the end of the row when we combine them into 1 column.
Combines all columns into 1 using , as delimiter (, marks per column and ; marks per row)
Then we split them by ,;, to separate each rows (each row contains the textjoined result)
After that, we filter the data using query where a row should contain the values of F1 and G1 (and including Timestamp to include the header).
Filtered data will then split back to their original form
Output:
EDIT:
To be dynamic, I modified it to check F1:1 range instead.
Formula:
=if(E1="",
QUERY(IMPORTRANGE("Spreadsheetidabcd", "RawData!A:Z"), "Select Col1,Col12,Col2,Col10,Col3 where (Col11='')",1),
arrayformula(split(query({transpose(split(textjoin(",", true, QUERY(IMPORTRANGE("Spreadsheetidabcd", "RawData!A:Z"), "Select Col1, Col12, Col2, Col10, Col3, ';' label ';' ';'")), ",;,",))}, "where lower(Col1) contains lower('"&join("') or lower(Col1) contains lower('", filter($F$1:$1, not(isblank($F$1:$1))))&"') or Col1 contains 'Timestamp'"), ",")))
Output:

Google Sheets Query return autoincrement in results

I need to return a two column table from query where the first column shows the position order and then full name. So essentially in MySQL form it would be an autoincrement but I cannot get it to work. I'm using =arrayformula(QUERY({G4:G18, arrayformula(row(G4:G18)) & ". " & H4:H18&" "&I4:I18, J4:J18}, "SELECT Col2, Col3 WHERE Col1 = 'Yes' ORDER BY Col3 ASC LABEL Col2 '', Col3 ''")) which I realize the row(G4:G18) is just going to return the row number but I've tried everything else I can think of and can't get it to work. Any help is greatly appreciated. Note: I want to keep this in query form versus filter for various reasons. thanks.
Sample sheet to see in action
I have come up with a solution but to do it, I need to have 2 queries. 1 that returns the Full name with incrementing numbers, and the other one which returns the Person ID. Please see screenshots below:
1st query(for Full names with incrementing numbers):
=INDEX(arrayformula(ifna(arrayformula(row(G4:G18)-3) & ". " & QUERY({G4:G18, H4:H18&" "&I4:I18, J4:J18}, "SELECT Col2, Col3 WHERE Col1 = 'Yes' ORDER BY Col3 ASC LABEL Col2 '', Col3 ''"), "")), 0, 1)
2nd query(for the Person ID)
=INDEX(arrayformula(QUERY({G4:G18, H4:H18&" "&I4:I18, J4:J18}, "SELECT Col2, Col3 WHERE Col1 = 'Yes' ORDER BY Col3 ASC LABEL Col2 '', Col3 ''")), 0, 2)
They are basically the same query, but I split them into two in order to concatenate an incrementing value to the Full names. I tried doing it using only 1 query but what happens is that the incrementing value will also be seen in the Person ID column(eg. 1. 2, 2. 3, 3. 5). Please let me know if this solution solves your problem.

QUERY Function Question Regarding WHERE clause

I have a query where I am trying to match the range value with the value that is in the first column (A) of the current row:
=QUERY(IMPORTRANGE("https://URL.com", Spreadsheet!A:Z), "SELECT COl4 WHERE Col1 = "' & CONCATENATE('A', ROW()) & '")
So essentially, I am trying to select the value of col4 if col1 matches the value of "A235" or A + whatever the current row is.
However the Concatenate part doesn't seem to be working (Formula parse error) and if someone could point me in the right direction, it will be awesome.
Thanks!
Repeating a working solution from my comment.
Try this:
=QUERY(IMPORTRANGE("https://URL.com", Spreadsheet!A:Z), "SELECT COl4 WHERE Col1 = '" & A:A & "'")

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