I'm having trouble figuring out a fairly simple QUERY statement in Google Spreadsheets. I'm trying to use a cell reference instead of static values and I'm running into trouble. Below it the code I'm using, but I keep getting a "Error: Formula parse error."
=QUERY(Responses!B1:I, "Select B where G contains"& $B1 &)
I'm sure it is a simple error, but can someone please show me how to write the above so the QUERY is pulling data from B where G contains the value in cell B1 (cell reference)?
Copied from Web Applications:
=QUERY(Responses!B1:I, "Select B where G contains '"&$B1&"'")
I only have a workaround here.
In this special case, I would use the FILTER function instead of QUERY:
=FILTER(Responses!B:B,Responses!G:G=B1)
Assuming that your data is on the "Responses" sheet, but your condition (cell reference) is in the actual sheet's B1 cell.
Hope it helps.
UPDATE:
After some search for the original question: The problem with your formula is definitely the second & sign which assumes that you would like to concatenate something more to your WHERE statement. Try to remove it. If it still doesn't work, then try this:
=QUERY(Responses!B1:I, "Select B where G matches '^.\*($" & B1 & ").\*$'") - I have not tried it, but it helped in another post: Query with range of values for WHERE clause?
I know this is an old thread but I had the same question as the OP and found the answer:
You are nearly there, the way you can include cell references in query language is to wrap the entire thing in speech marks. Because the whole query is written in speech marks you will need to alternate between ' and " as shown below.
What you would need is this:
=QUERY(Responses!B1:I, "Select B where G contains '"& B1 &"' ")
If you then wanted to refer to multiple cells you could add more like this
=QUERY(Responses!B1:I, "Select B where G contains '"& B1 &"' and G contains '"& B2 &"' ")
The above would filter down your results further based on the contents of B1 and B2.
I found out that single quote > double quote > wrapped in ampersands did work. So, for me it looks like this:
=QUERY('Youth Conference Registration'!C:Y,"select C where Y = '"&A1&"'", 0)
Here is working code:
=QUERY(Sheet1!$A1:$B581, "select B where A = '"&A1&"'")
In this scenario I needed the interval to stay fixed and the reference value to change when I drag it.
none of the above answers worked for me. This one did:
=QUERY(Copy!A1:AP, "select AP, E, F, AO where AP="&E1&" ",1)
To make it work with both text and numbers:
Exact match:
=query(D:E,"select * where D like '"&C1&"'", 0)
Convert search string to lowercase:
=query(D:E,"select * where D like lower('"&C1&"')", 0)
Convert to lowercase and contain part of the search string:
=query(D:E,"select * where D like lower('%"&C1&"%')", 0)
A1 = query/formula
yellow / A:B = result area
green / C1 = search area
blue / D:E = data area
If you get error when the input is text and not numbers; move the data and delete the (now empty) columns. Then move the data back.
Old Thread but I found this on my journey to the below answer and figure someone else might need it too.
=IFERROR(ArrayFormula(query(index(Sheet3!A:C&""),"select* Where Col1="""&B1&""" ")), ARRAYFORMULA({"* *","no cells","match"}));
Here is a simply built text Filter from a 3 column data set (A,B and C) located in "sheet3" into the current sheet and calling a comparison to a cell from the current sheet to filter within Col1(A).
This bit is just to get rid of the #N/A error if the filter turns up no results //ARRAYFORMULA({"* *","no cells","match"}))
Your question-title is very broad despite the question-body is asking for only one type of 'cell reference'. I'll try to answer some of the other scenarios in case other people come here looking for them.
A - String comparison with cell reference
=TRANSPOSE(QUERY(MIX!A16:F, "SELECT B,C,D,E,F WHERE (UPPER(A) = '"&F1&"') "))
=QUERY(MIX!A16:F, "SELECT B,C,D,E,F WHERE (UPPER(A) = '"&F1&"') ")
F1 = StringToSearch
B - String comparison with cell reference + SheetName is called using cell reference + Dates comparison using cell reference
F6 = SheetName
F2 and F3 = StringToSearch
I4 and I5 = dates with this format = 2022-01-01 = yyyy-mm-dd
If the dates you have are in a different format, you can use the formula below to convert them
I4 = TEXT(H5,"yyyy-mm-dd")
=QUERY(INDIRECT(F6&"!A2:C") , "select A,B,C where ((upper(B) contains '"&F2&"') or (upper(B) contains '"&F3&"')) AND (A >= date '"&I4&"') AND (A <= date '"&I5&"') ")
C - Data to query comes completely using cell reference
B1 = Sheetname!Range = Sheet1!A1:C
A36 = stringToFind
=QUERY(INDIRECT($B$1),"SELECT * WHERE upper(B) CONTAINS upper('"&A36&"') ")
The "$" in $B$1 make the reference static for column and row, so if you copy/paste the CELL containing the formula (NOT the text of the formula), the A36 adapts accordingly, but B1 stays fixed.
D - Data to query comes completely using cell reference + String comparison using cell reference + Number comparison using cell reference
=QUERY(INDIRECT($B$1),"SELECT * WHERE upper(B) CONTAINS upper('"&A40&"') AND C > "&A41&" ")
B1 = Sheetname!Range = Sheet1!A1:C
A40 = stringToFind = blah
A41 = Number = 100
E - Nested query (query inside query) + String comparison using cell reference + Number comparison using cell reference, using LIMIT and OFFSET
=QUERY( transpose( query(IMPORT!$A$7:AAL,"select * where A contains '"&$C34&"' ")) ,"SELECT * LIMIT " & $A34 & " OFFSET " & $B34, 0 )
The final '0' is there to NOT include the header in the results.
Related
I have a google sheet (Final Calculation) where I am trying to get sum of all values in Column G (in Data tab) against a set of values in Column B, L and D (in Data tab). However, condition for values in Column D is dynamic and would likely change every month. I have created a tab Advertiser List where the condition for filter in Column D gets updated. How, do I use this condition in Query function used in C5 cell in Final Calculation tab?
I tried referencing the condition in Advertiser List sheet using indirect but I am getting an error. I have also added a hard coded formula is other cells in Final Calculation tab to show what am I trying to achieve.
Below is the formula that I have written and is showing an error:
=iferror(QUERY(Data!$A:$L, "Select Sum(G) where B = '"&$B5&"' and L = '"&C$3&"' and indirect('"Advertiser List"'&'"!"'&'"D2"') label sum(G)'' "),0)
Here is the link of the Google Sheet (Link)
You don't need INDIRECT to get a text value from another tab:
=QUERY(Data!$A:$L, "Select Sum(G) where B = '"&$B5&"' and L = '"&C$3&"' and (" & 'Advertiser List'!D1 & ") label sum(G)'' ")
When in doubt, try to build the "select ... " string in a separate cell until you see the expected result. Then wrap it with query and check again. Adding IFERROR should always be your last step, because you will want to see the errors that your query is throwing.
I can't seem to find the right equation to find a cell from a row that matches only a few specific characteristics. In this example, I am trying to find the equation for Column D which would be the cell in A that has the same cells for B & C.
Hope this makes sense!
I'll provide two options.
If you're sure your data will only ever have zero or one match, you can place the following formula into D2 of an otherwise empty range D2:D...
=ArrayFormula(IF(A2:A="",,SUBSTITUTE(VLOOKUP(B2:B&C2:C,{B2:B&C2:C,A2:A},2,FALSE)&VLOOKUP(B2:B&C2:C,SORT({B2:B&C2:C,A2:A,ROW(A2:A)},3,0),2,FALSE),A2:A,"")))
However, if you think more than one match may turn up and you want "None" to be returned if there is no match, you can use the following formula in D2 or an otherwise empty range D2:D...
=ArrayFormula(IF(A2:A="",,REGEXREPLACE(REGEXEXTRACT(REGEXREPLACE(SUBSTITUTE(VLOOKUP(B2:B&C2:C,TRIM(SPLIT(FLATTEN(QUERY(QUERY({B2:B&C2:C&"~",A2:A&","}, "Select MAX(Col2) where Col2 IS NOT NULL GROUP BY Col2 PIVOT Col1"),, 9^9)),"~")),2,FALSE),A2:A,""),"^[,\s]+$","None"),"([^,\s].+[^,\s])[,\s]*$"),"[,\s]+",", ")))
The second formula will work even if there will only ever be zero or one match; it's just not necessary to have it be that lengthy. And the second formula is only as lengthy because it was unclear from your posted examples whether the data in Col A, B and C will really only ever be one word or not; so the formula is built to assume there will not always be one-word strings in those columns.
Either formula will provide results for the entire column without dragging.
Here's an option, You can use this formula in column D2:
=iferror(textjoin(", ",true,query($A$2:$C,"Select A where A is not null and A != '"&$A2&"' and B = '"&$B2&"' and C = '"&$C2&"'",0)),"None")
Limitation:
You need to manually drag the formula to its succeeding rows. Arrayformula() cannot be used in looping the query string values.
What it does?
Using query(), filter the data from A2:C that has the same current row last name(Column B) and food(Column C) at the same time having a different first name(Column A)
If there are multiple results, use textjoin() to combine them with ", " as its delimiter.
If there is no matched found, it will return an error, hence use iferror() to set the default value to "None"
Output
I'm trying to select certain data from responses of a form on a google sheet. I just want to select all the data if one cell is equal to a specific word. I've created a new sheet inside the same archive, and tried with several formulas:
=QUERY("respuestas!A2:M50","select * where G = 'Felino'")
setting name range respuestas!A2:M50 = raw:
=QUERY(raw,"select * where G = 'Felino'")
importing range:
=QUERY(importrange("https://docs.google.com/spreadsheets/d/1y1WxrJ9ErkqX1gR5su37dAPe97fI9KZoeQtxhuSC2lA/edit","respuestas!A2:M50"),"select * where G = 'Felino'")
importing range with name range:
=QUERY(importrange("https://docs.google.com/spreadsheets/d/1y1WxrJ9ErkqX1gR5su37dAPe97fI9KZoeQtxhuSC2lA/edit","respuestas!A2:M50"),"select * where G = 'Felino'")
and trying with Col1 attribute instead Column name, like A2, in the select property. Also trying to select just a column like:
"select A"
or
"select Col1""
and none of these works...
I dont know what else to try??
Ohh that works!!
Thanx...
It was just the semicolon... I've replaced ":" with ";"
I have some trouble with my queries i have try so hard to get it work. but its seems that Query can only get data from cell that have only text in it. If i try to get data from ex. = funktion its dont work.
Exempel for my Querys.
IN CELL B1
=query('DATA-IN-All-stocks'!A:B ;"select A where B = '" & TEXT( A2;"") & "'" ;-1)
RESULT = This generet IMNP
IN CELL B2
=QUERY('DATA-IMNP-Swedbank'!A:M;"select C,D where B = '"&B1&"' and C = 'Köp'" ;0)
RESULT = This generet N/A = dont find anny...
When i Change B1 and only typing in text IMNP i get the RESULT correctly
If you check =LEN(B1) you will see it is 14.
This might be because the import has fixed width fields used to split between columns.
You can easily circumvent this in your application by wrapping TRIM around the formula creating the ticker within B1.
Suppose I've got a text values column (named Data), generated by =unique() function. Also, there is an array of patterns to find and replace for (Find and Replace columns).
Which formula should I use to scan each cell in Data for multiple patterns in Find and replace it, if match?
Data Find Replace Result
1 a c z a
2 b f y b
3 c e x z
4 d d
5 e x
6 c z
Tried =SUBSTITUTE() and =IF() functions, but it fails, when I set an array of patterns, instead of single one.
If the table you is in range A1:E7, try this formula
=TRANSPOSE(SPLIT(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(ARRAYFORMULA(CONCATENATE($B$2:$B$7&"|")),$C$2,$D$2),$C$3,$D$3),$C$4,$D$4),"|"))
You can read further about this in an older post and google docs forum.
Array solution
This formula takes range to replace, so it can be used for variable number of patterns:
=QUERY(ARRAYFORMULA({REGEXMATCH(A2,$B$2:$B$4),
REGEXREPLACE(A2,$B$2:$B$4,$C$2:$C$4)}),
"select Col2 order by Col1 desc limit 1")
or this one:
=INDEX(ArrayFormula(REGEXREPLACE(A2,$B$2:$B$4,$C$2:$C$4)),IFERROR(MATCH(A2,$B$2:$B$4,0),1))
or this:
=IFERROR(INDEX($C$2:$C$4,MATCH(A2,$B$2:$B$4,0)),A2)
The formula is need to be dragged down.
Single Formula & Array solution
Also this single ArrayFormula will do the trick:
=ArrayFormula(trim(transpose(query({IF(--REGEXMATCH(TRANSPOSE(A2:A7),$B$2:$B$4)=1,
REGEXREPLACE(TRANSPOSE(A2:A7),$B$2:$B$4,$C$2:$C$4),"");
TRANSPOSE(if(--not(REGEXMATCH(A2:A7,JOIN("|",B2:B4))),A2:A7,""))},,COUNTA(A2:A)))))
or this shorter formula:
=ArrayFormula(IFERROR(VLOOKUP(MATCH(A2:A7,B2:B4,0),{ROW(INDIRECT("a1:a"&COUNTA(C2:C4))),C2:C4},2,0),A2:A7))
Please, see explanations in Sample file