Google Sheets Query with Logic Statements - google-sheets

I am trying to create a list of data from a data range in Google Sheets. I thought of using the query function, but with that, you can't seem to use regular logical statements.
I know that this snippet of code is wrong and doesn't work, but hopefully it makes it clear what I'm trying to do.
=Query(E2:E103, OR(AND(D2:D103=A$2,G2:G103=A$5),AND(D2:D103=A$3,G2:G103=A$5),D2:D103=A$1))
In this code, A$2, A$5, A$3, and A$1 are all just string variables in the corresponding cells that tell the logic statement what to compare.
If there is another way to write this or a different function that accomplishes what I would like to do, it would be greatly appreciated if you could share it with me.

Basic case
The basic syntax for building a query logic:
"where F = '" & A1 & "'"
assumes A1 is a string, values in column F are strings
adds single quotes, makes the resulting query string: where F = 'sample text'
Logic
Your case looks like this:
where (A) or (B) or C =>
where (A1 and A2) or (B1 and B2) or C =>
where (D = 'text1' and G = 'text2') or (D = 'text3' and G = 'text4') or D = 'text5'

Related

Using Query to select column based on the headers

I have a dynamic table where columns are moved every now and then. Therefore, I would like to reference my column name in my query. Unfortunately I do not know so well and the internet raises more questions.
My formula looks like this:
`=query('X Source'!A:AP, "select D, E, AA, AM, X, A where "&if(month(now())=1,"(month(A)<11)","(month(A) <=month(now())-2)")&" and (V like 'C & G' or V like 'SAS' or V like 'SXS D' or V like 'DIR') Order By A desc")
D = Cinter
E = Cluster
AA = Creation Date
AM = Change Ow
X = Title
A = Date`
Do you have any idea ? I would like not to write a script.
I have already tried with the function filter to bypass but there I get no further because of the filtering after month.
`={FILTER('X Source'!AA:AA, 'X Source'!V:V="SAS",'X Source'!X:X<>"%BY SB%",'X Source'!X:X<>"%SB ONLY%", month('X Source'! AA:AA)=month(today())-1);FILTER('X Source'!AA:AA,'X Source'!V:V="SXS D",'X
Source'!X:X<>"%BY SB%",'X Source'!X:X<>"%SB ONLY%"`
You can define the following Named Function. (Data > Named functions > Add new function)
Name:
BETTERQUERY(range, better_query, headers)
Definition
=QUERY({range},IF(IFERROR(SPLIT(better_query,"`")=better_query,1),better_query,
REGEXREPLACE(REDUCE(better_query,REGEXEXTRACT(better_query,REGEXREPLACE(
REGEXREPLACE(better_query,"([()\[\]{}|\\^$.+*?])","\\$1"),"`(.*?)`","`($1)`")),
LAMBDA(acc,cur,SUBSTITUTE(acc,cur,IFNA("Col"&MATCH(cur,INDEX(range,1),0),cur)))),
"`(Col\d+)`","$1")),headers)
And then use it like this:
=BETTERQUERY('X Source'!A:AP',"select `Cinter`, `Cluster`, `Creation date` ...",1)
For more information on how this works see How to Use Column Names in QUERY
You could use MATCH to find the numbers of columns, and grab your range in curly brackes so you can refer them as Col1,Col2,Col3 instead of A,B,C
Just to make it more dinamic and you could change your range, I wrapped it in LAMBDA. With the headers I matched all your values and joined them with comma. 'date' only matched that colum. Column V I had my doubts about if it was a mistake when you said which columns was located at which headers. Please change "Title" in ""Col"&MATCH("Title",INDEX(range,1),0)" to the actual desired header title (that now is in V column) so it matches correctly:
=LAMBDA(range,
LAMBDA(headers,date,title,query({range}, "select "&headers&" where "&if(month(now())=1,"(month("&date&")<11)","(month("&date&") <=month(now())-2)")&" and ("&title&" like 'C & G' or "&title&" like 'SAS' or "&title&" like 'SXS D' or "&title&" like 'DIR') Order By "&date&" desc"))(
JOIN(",",INDEX("Col"&MATCH({"Cinter","Cluster","Creation Date","Change Ow","Title","Date"},INDEX(range,1),0))),
"Col"&MATCH("Date",INDEX(range,1),0),
"Col"&MATCH("Title",INDEX(range,1),0)
))
('X Source'!A:AP)
In my dummy example with random columns, the inside part of the query would look like this:
"select Col7,Col3,Col15,Col20,Col11,Col12 where (month(Col12)<11) and
(Col11 like 'C & G' or Col11 like 'SAS' or Col11 like 'SXS D' or Col11
like 'DIR') Order By Col12 desc"
UPDATE with other Locale
=LAMBDA(range; LAMBDA(headers;date;pl;query({range}; "select "&headers&" where "&if(month(now())=1;"(month("&date&")<11)";"(month("&date&") <=month(now())-2)")&" and ("&pl&" like 'C & G' or "&pl&" like 'SAS' or "&pl&" like 'SXS D' or "&pl&" like 'DIR') Order By "&date&" desc";1))( JOIN(",";INDEX("Col"&MATCH({"Cinter";"Cluster ";"Creation Date";"Change Owner";"Title";"Date"};INDEX(range;1);0))); "Col"&MATCH("Date";INDEX(range;1);0); "Col"&MATCH("PL";INDEX(range;1);0) )) ('X Source'!A1:AS)

Arrayformula - Cell contains text (Google Sheets)

I have the formula written below. On the condition where Column E = "EPIs", there is a second condition to verify if Column G contains the string ("CC", "Hemo" or "odonto").
But that second condition is not returning correctly the 'contains the specified string' part.
It gets true for Col E = "EPIs", but false for the second cases.
How can I make it work?
=ARRAYFORMULA(
IF(B3:B="";"";
IF(M3:M="";"Verificar";
IF(M3:M="Realizado - Sem Certificado";"Verificar";
IF(M3:M="Manutenção";"Verificar";
IF(M3:M="Verificar";"Verificar";
IF(M3:M="Verificar se está ativo";"Verificar";
IF(M3:M="Não realizado";"Verificar";
IF(M3:M="Desativado";"DESATIVADO";
IF(E3:E="EPIs";
(IF(G3:G="*CC*";IF(DATE(YEAR(M3:M);MONTH(M3:M)+6;DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M);MONTH(M3:M)+4;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido");
IF(G3:G="*hemo*";IF(DATE(YEAR(M3:M);MONTH(M3:M)+6;DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M);MONTH(M3:M)+4;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido");
IF(G3:G="*odonto*";IF(DATE(YEAR(M3:M)+2;MONTH(M3:M);DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M)+2;MONTH(M3:M)-2;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido");
IF(DATE(YEAR(M3:M)+1;MONTH(M3:M);DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M)+1;MONTH(M3:M)-2;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido")
))));
IF(E3:E="Odonto Intra";IF(DATE(YEAR(M3:M)+2;MONTH(M3:M);DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M)+2;MONTH(M3:M)-2;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido");
IF(DATE(YEAR(M3:M)+1;MONTH(M3:M);DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M)+1;MONTH(M3:M)-2;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido") ) ))))))))))
The formula you wrote is quite a bit more convoluted than you needed it to be.
I think using EDATE() and LOOKUP() you can achieve the desired effect more easily.
=ARRAYFORMULA(IF(M3:M="";;LOOKUP(TODAY()-EDATE(M3:M;IF(REGEXMATCH(G3:G;"Hemo|CC");6;IF(REGEXMATCH(G3:G;"Odonto");24;12)));{-9^9;-60;0};{"OK";"Realizar";"Vencido"})))

Query based on cell reference on multiple conditions on Google Sheets

I am trying to create a search function where users are able to select from a list in Google Sheets and the below query function will return that data that corresponds from the users' selections.
=query(Overview!A:AF,"SELECT D,I,M,AA WHERE AA = '"B27"' AND L ">=" "&E27&" AND S CONTAINS '"&H27&"'",1)
I expected to get the list (D, I, M, AA) but I keep getting #ERROR, what is the problem?
this is the correct syntax:
=QUERY(Overview!A:AF; "SELECT D,I,M,AA
WHERE AA = '"&B27&"'
AND L >= '"&E27&"'
AND S CONTAINS '"&H27&"'"; 1)

Is it possible to create a variable and make its assignment based on certain conditions in a cypher query?

I am trying to create an array of values that will be assigned based on the outcome of a case test. This test will be inside a query that I already know works with a preset value in the query.
The query I am trying to embed in the case test is something like this:
WITH SPLIT (('07/28/2015'), '/' AS cd
MATCH (nodeA: NodeTypeA)-(r:ARelation)->(nodeB: NodeTypeB)
WITH cd, SPLIT (nodeA.ADate, '/') AS dd, nodeA, nodeB, r
WHERE
(TOINT(cd[2])> TOINT(dd[2])) OR (TOINT(cd[2]= TOINT(dd[2]) AND ((TOINT(cd[0])> TOINT(dd[0])) OR (TOINT(cd[0])= TOINT(dd[0]) AND (TOINT(cd[1])>= TOINT(dd[1])))))
RETURN nodeA, nodeB, r
I want to replace the current date with whatever date will be 6 months from the current date, and I came up with something like this, though I am not sure where I would put it in my query or if it would even work (do I initialize the new variable for instance somehow?):
WHEN ((TOINT(cd[0])> 6))
THEN
TOINT(fd[2])=TOINT(cd[2])+1, TOINT(fd[0])=TOINT(cd[0])-6, TOINT(fd[1])=TOINT(cd[1])
ELSE
TOINT(fd[2])=TOINT(cd[2]), TOINT(fd[0])=TOINT(cd[0])+6, TOINT(fd[1])=TOINT(cd[1])
fd would then replace the cd in the original query's WHERE segment. Where would my case test go, is it correctly written (and if not, what is wrong), and would I need something else added to make it all work?
Just use a WITH block to do a computation and bind it to a new variable, like this:
WITH 2 + 2 as y RETURN y;
That basically assigns the value 4 to y.
In your query, you already have a big WITH block. Just put your computations in those, bound to new variables, and you can then refer to those variables in subsequent expressions.
Don't try to modify these variables, just create new ones (with new WITH blocks) as needed. If you need variables that can actually change, then...well hey you're working with a database, the ultimate way to store and update information. Create a new node, and then update it as you see fit. :)
This is my proposed solution
Explanation: I have declared four variables in my query i.e. name1, name2, ken and lana and I am using these variables for creating MATCH pattern (in the MATCH clause) and filtering those in the Where clause.
WITH "Lau" AS name1,
"L" AS name2,
"Keanu Reeves" AS ken,
"Lana Wachowski" AS lana
MATCH(x:Person{ name: ken})-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(y:Person),
(x1:Person{name: lana})-[:DIRECTED]->(m)<-[:DIRECTED]-(y1:Person)
WHERE y.name CONTAINS name1 OR
y.name CONTAINS name2 OR
(y.name CONTAINS name1 AND y.name CONTAINS name2)
RETURN x, m, y, x1;

String array in database. match and return values

I have the following data in a column letters in a mysql database. I saved them in varchar:
letters
["a","b"]
["a","b","d"]
["a","d"]
["d","c","e"]
["e","c","f"]
["c","f"]
["f","e"]
I am trying to match some elements. When I have params[:lttrs] as"a", I want to return:
["a","b"]
["a","b","d"]
["a","d"]
When I have params[:lttrs] as "c,e", I want to return:
["d","c","e"]
["e","c","f"]
My attempt is to retrieve all the rows and then match each of them with include?('a'), but with that, I can only do one element at a time. Is that the approach?
You can use the LIKE operator with wild card matching. This solution will be faster than using ruby. But it will be slow if you have a large table. If you provide more details about the reason for your data design we will be able to suggest alternative approaches.
like_params = params[:lttrs].split(",").map{|letter| "%#{letter}%"}
like = like_params.map{ "LIKE ?"}.join(" OR ")
Model.where(like, like_params)
You could try doing something like:
query = "letters "
r = 1
letters_arr = params[:lttrs].split(",")
letters_arr.each do |l|
if r == 1:
query << " like '%#{l}%'"
else
query << " or like '%#{l}%'"
end
end
letters_found = WhateverModel.where(query)
Obviously you need to make sure that the params are handled more safely than that, but that should get you on your way.

Resources