Joining more than two WHERE statements in Query Language - google-sheets

So I am trying to use a simple QUERY function in Google Sheets where I want to select based on TWO parameters. Simple logic, and documentation says use the AND operator. The problem is I am searching for text via Cell Reference.
So here is my function
=QUERY(A1:D6,"select A where C='" &K1&'"" & "and D='" &K2"'")
Unfortunately it throws up an ERROR. I understand that Cell References that are text based need to be in single quotes (which themselves need to be in double quotes), but I am unable to join two WHERE statements.
What is the right syntax for this?

Very close indeed, please try:
=query(A1:D6,"select A where C='"&K1&"' and D='"&K2&"' ")

Welp! I was missing an concatenation symbol (&) at the end of the final cell reference K2.
=QUERY(A1:D6,"select A where C='" &K1&'"" & "and D='" &K2&"'")

Related

How to alternate queries from drop down menu in Google Sheets

Hello Help Community,
I am trying to find a way to alternate queries from drop down menus. The idea is to be able to move between a list of queries and not have to rewrite the query when I want to use it again. I saw this done by writing the select statement in a drop down and referencing the cell containing the select statement in the query like this:
=QUERY(range, "" """"""&cell&"""""" "")
However, I cannot get this method to work. I can manipulate the same query using cell references and drop down menus but I would like to do more if it is possible.
Any idea how this could be achieved would be appreciated.
Regards, Andrew H"
example:
=QUERY(A2:D5, "where A = '"&B8&"'", 1)
if dropdown contains numbers use:
=QUERY(A2:D5, "where A = "&B8, 1)

Exact match in dget function with an array as the criteria

Example Sheet I'm trying to get an exact match with an array in the criteria section of dget. Maybe there is another way to work around this, but I'm trying to give it a dynamic component in the array.
=dget('Micro Data'!$A$1:J,"PCR Score",{"Micro Type","Stage Type","Tank","ID#";"PCR PAL","Bright",F2,H2})
Sometimes all criteria matches multiple data points except the "Tank". However the tanks won't exactly match. Ex. All the data is the same in two data sets, except the tanks are CT1 and CT18. This then comes up with the #NUM! error. I'm trying to find if there is a way to get an exact match in the array data while still allowing it to reference the cell?
I know there is the option of making it "=XXX" making it a txt string, but this would take away the dynamic function. I would also loose the auto updating aspect when more data is added.
Thanks
Ryan, see my solution using a query, in Retain Log-GK, cell F2. I think it is just as dynamic as the dget, but perhaps not. It will need some error wrapping to avoid errors if no result found.
Formula is basically:
=query('Criteria Source'!A2:J5,
"select J where B = '"&D9&"' and C = '"&D10&"' and E = '"&D11&"' and D ='"& D2 & "' ",0)
I made all of the criteria dynamic, though obviously you can do it whatever way suits you best...
Let me know of any questions. I'll check back later...

How to shorten this syntax in Google Sheet?

Can this (Google Sheet) =IFS syntax be improved?
=IFS(and(E42>E38;E42>E34;E42>E30;E42>E26;E42>E22;E42>E18); "Cattleman";
and(E38>E42;E38>E34;E38>E30;E38>E26;E38>E22;E38>E18); "Naturalist";
and(E34>E42;E34>E38;E34>E30;E34>E26;E34>E22;E34>E18); "Farmer";
and(E30>E42;E30>E38;E30>E34;E30>E26;E30>E22;E30>E18); "Carpenter";
and(E26>E42;E26>E38;E26>E30;E26>E34;E26>E22;E26>E18); "Blacksmith";
and(E22>E42;E22>E38;E22>E30;E22>E34;E22>E26;E22>E18); "Miner";
and(E18>E42;E18>E38;E18>E30;E18>E34;E18>E22;E18>E26); "Builder")
And how can I add a default value so that if this syntax returns FALSE it doesn't say #N/A! in the cell, but "No class" or something similar instead (or empty)?
One obvious way would be to replace the ANDs with a MAX. Why? In the first line, if E42 is greater than all of the other cells, it must be greater than the MAX of them. So the condition in this line
E42 > MAX(E38; E34; E30; E26; E22; E18)
which looks much cleaner. Repeat for the other lines.
Trying to simplify it more, the logic of the formula seems to be that depending on which of the cells is the greatest, you choose a particular literal value. There is a function for that! I'd try this (can't test it though without access to your data)
=CHOOSE(
MATCH(
MAX(E42; E38; E34; E30; E26; E22; E18);
{E42; E38; E34; E30; E26; E22; E18});
"Cattleman"; "Naturalist"; "Farmer"; "Carpenter"; "Blacksmith"; "Miner"; "Builder")
wrap it in IFERROR like this:
=IFERROR(IFS(
AND(E42>E38;E42>E34;E42>E30;E42>E26;E42>E22;E42>E18);"Cattleman";
AND(E38>E42;E38>E34;E38>E30;E38>E26;E38>E22;E38>E18);"Naturalist";
AND(E34>E42;E34>E38;E34>E30;E34>E26;E34>E22;E34>E18);"Farmer";
AND(E30>E42;E30>E38;E30>E34;E30>E26;E30>E22;E30>E18);"Carpenter";
AND(E26>E42;E26>E38;E26>E30;E26>E34;E26>E22;E26>E18);"Blacksmith";
AND(E22>E42;E22>E38;E22>E30;E22>E34;E22>E26;E22>E18);"Miner";
AND(E18>E42;E18>E38;E18>E30;E18>E34;E18>E22;E18>E26);"Builder");
"No class")

GSheets - How to query a partial string

I am currently using this formula to get all the data from everyone whose first name is "Peter", but my problem is that if someone is called "Simon Peter" this data is gonna show up on the formula output.
=QUERY('Data'!1:1000,"select * where B contains 'Peter'")
I know that for the other formulas if I add an * to the String this issue is resolved. But in this situation for the QUERY formula the same logic do not applies.
Do someone knows the correct syntax or a workaround?
How about classic SQL syntax
=QUERY('Data'!1:1000,"select * where B like 'Peter %'")
The LIKE keyword allows use of wildcard % to represent characters relative to the known parts of the searched string.
See the query reference: developers.google.com/chart/interactive/docs/querylanguage You could split firstname and lastname into separate columns, then only search for firstnames exactly equal to 'Peter'. Though you may want to also check if lowercase/uppercase where lower(B) contains 'peter' or whitespaces are present in unexpected places (e.g., trim()). You could also search only for values that start with Peter by using starts with instead of contains, or a regular expression using matches. – Brian D
It seems that for my case using 'starts with' is a perfect fit. Thank you!

Google Spreadsheet long IF statement?

I have this statement:
=if(
F1B!D3="1",50+FLOOR(D2/10,1),
if(F1B!D3="2",40),
if(F1B!D3="3",30),
if(F1B!D3="4",25),
if(F1B!D3="5",20),
if(F1B!D3="6",19),
if(F1B!D3="7",18),
if(F1B!D3="8",17),
if(F1B!D3="9",16),
if(F1B!D3="10",15),
if(F1B!D3="11",14),
if(F1B!D3="12",13),
if(F1B!D3="13",12),
if(F1B!D3="14",11),
if(F1B!D3="15",10),
if(F1B!D3="16",9),
if(F1B!D3="17",8),
if(F1B!D3="18",7),
if(F1B!D3="19",6),
if(F1B!D3="20",5),
if(F1B!D3="21",4),
if(F1B!D3="22",3),
if(F1B!D3="23",2),
if(F1B!D3="24",1));
But GoogleDocs return me "error: Wrong number of arguments to IF"
What I'am doing wrong?
You can't pass infinitely many arguments to IF. There's a single condition, a single "THEN", and a single "ELSE". You need to "nest" your IF statements, where each new IF() in part of the previous IF statement's ELSE. Something like this (abbreviated):
=if(
F1B!D3="1",50+FLOOR(D2/10,1),
if(F1B!D3="2",40,
if(F1B!D3="3",30,
if(F1B!D3="4",25,
if(F1B!D3="5",20,
if(F1B!D3="6",19,
if(F1B!D3="7",18)))))))
Trying to apply too many IFs, far more than necessary:
=IF(F1B!D3=1,50+FLOOR(F1B!D2/10,1),iferror(CHOOSE(F1B!D3-1,40,30,25),25-F1B!D3))
Also do not append ;.

Resources