Using multiple AND and OR statements in QUERY function - Google Sheets - google-sheets

I have a Google spreadsheet with a tab called 'Updates'. In a separate tab, I want to use drop-down menus (contained in cells B3, B4 and B5 in the code below) to filter and subsequently view the data in the 'Updates' tab according to its text in columns B, C and D.
I have written the following code. Basically I want to be able to filter the data according to selections made in all 3 drop down menus (B3, B4 and B5), or just in two of them (e.g. B3 and B4, but B5 is left blank), or just in one of them (e.g. B3, and B4 and B5 are left blank).
=query(Updates!A1:E, " select * (where B = '"&B3&"' AND C = '"&B4&"' AND D = '"&B5&"') OR (where B = '"&B3&"' OR C = '"&B4&"' OR D = '"&B5&"') OR (where (B = '"&B3&"' OR C = '"&B4&"') AND D = '"&B5&"') OR (where B = '"&B3&"' AND (C = '"&B4&"' OR D = '"&B5&"') ) OR (where C = '"&B4&"' AND (B = '"&B3&"' OR D = '"&B5&"') ")
The AND and OR functions work separately in their own query functions, but when I combine them together I get the following error message:
Error
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "(" "( "" at line 1, column 11. Was expecting one of: "where" ... "group" ... "pivot" ... "order" ... "skipping" ... "limit" ... "offset" ... "label" ... "format" ... "options" ...
Any help? Thanks!

you can use where only once in one QUERY. try:
=QUERY(Updates!A1:E,
"where 9=9 "&
IF(B3="",," and B = '"&B3&"'")&
IF(B4="",," and C = '"&B4&"'")&
IF(B5="",," and D = '"&B5&"'"), )

Related

Check if cell is contained in group of cells to add its value to another cell

I have a Google Sheets document that looks like this:
A B C D
1
2 X X 3
3 Z Y 2
4 W Z 6
5
How can I do so that if the element in column A is one of the elements of column B, its corresponding value in column C is added to column D? In this case, column D must have this values:
D2 = 3, because X is in column B
D3 = 2, because Z is in column B
D4 = 0, because W is not in column B
Many thanks in advane!
use:
=INDEX(IF(A2:A="",,IFNA(VLOOKUP(B2:B,
FILTER(B2:C, COUNTIF(B2:B, A2:A)), 2, 0))*1))
You could use:
=INDEX(IF(A2:A="",,COUNTIF(B2:B,A2:A)*C2:C),,)

Custom formula in conditional formatting rule sometimes works wrong

I am using the formula = C2 + B2 <> A2 to highlight the cells where this condition is true. For some reason, this formula does not work correctly in some cells. For example, in cells C1, C17.
Please understand the reason.
All numbers in columns A through C are integers.
Your formula is using relative references, but not the right ones. In row 1 you want to compare the values in row 1, in row 2 values of same row and so on.
In your image you've selected C1 and the rule there should be = C1 + B1 <> A1, not = C2 + B2 <> A2
C1 is being highlighted because C2 + B2 is not equal to A2. 12115+460=12575, which is different from 12573.
Same for C17. Your conditional formula is looking 1 row below. Try using
= C1 + B1 <> A1

z3py optimization as variables chosen from a list

For the example, how to write code in z3py to maximize value of a+b+c+d as a,b,c,d are all chosen from list [1,2,3,4,5,6,7,8], which is given as the input. a, b, c, d are distinct values.
a, b, c, d = Ints("a b c d")
o = Optimize()
list = [1,2,3,4,5,6,7,8]
o.maximize(a+b+c+d)
how to write code corresponding to choose "a b c d" value from list. The correct out value of a+b+c+d is 26
Thanks!
Here's one way:
from z3 import *
a, b, c, d = Ints("a b c d")
o = Optimize()
list = [1,2,3,4,5,6,7,8]
vars = [a, b, c, d]
for v in vars:
o.add(Or([v == e for e in list]))
o.add(Distinct(*vars))
goal = Int("goal")
o.add(goal == a+b+c+d)
o.maximize(goal)
if o.check() == sat:
print o.model()
else:
print "not satisfiable"
When I run this, I get:
$ python a.py
[d = 5, c = 8, b = 6, a = 7, goal = 26]

Adding hyperlink to returned value in QUERY results Google sheets

I have the following query which is working just fine.
but now I want to take the returned value in A which is a unique number and hyperlink it.
The resulting display should be just the number. The URL for the link is fixed with the number appended at the end e.g.
number = 123456
URL = https:\google.com\123456
my current query is:
=iferror(
if(C2="Closed",
QUERY(DATA!A:AA, "
SELECT A,B, D, T, N
WHERE( AA CONTAINS '" & lower(C1) & "' AND L = " & J1 & ")
Order by T Asc"),
QUERY(DATA!A:AA, "
SELECT A,B, D, R, N
WHERE( AA CONTAINS '" & lower(C1) & "' AND L = " & J1 & ")
Order by T Asc")
)
how can I manipulate the results of the query so each line returned the value in A is hyperlinked?
B8: =QUERY(B2:E5, "select C where E contains 'h11'", 0)
B14: =ARRAYFORMULA("www.abc.com/"&QUERY(B2:E5, "select C where E contains 'h11'", 0))

F# unclear function effects

I'm reading the F# for C# developers book and there's this function that I can't seem to understand what are the effects of this function
let tripleVariable = 1, "two", "three"
let a, b, c = tripleVariable
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a,b,c in l do
printfn "triple is (%d,%d,%d)" a b c
the output is
triple is (1,2,3)
triple is (2,3,4)
triple is (3,4,5)
why a, b, c are initialized with tripleVariable? Is it because it was needed in the for loop to know their type (or its type, since it's a Tuple)?
The code snippet is using variable shadowing when defining the variables a, b and c. The variables are first initialized to the values of tripleVariable (line 2), but then they are shadowed by a new definition inside the for loop (line 4).
You can think of these as different variables - the code is equivalent to the following:
let tripleVariable = 1, "two", "three"
let a1, b1, c1 = tripleVariable
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a2, b2, c2 in l do
printfn "triple is (%d,%d,%d)" a2 b2 c2
Variable shadowing simply lets you define a variable with a name that already exists in the scope. It hides the old variable and all subsequent code will only see the new one. In the above code snippet, the old (shadowed) variables b and c even have different types than the new ones.
The code contains 2 samples. The first one is
let tripleVariable = 1, "two", "three"
let a, b, c = tripleVariable
The 2nd one
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a,b,c in l do
printfn "triple is (%d,%d,%d)" a b c
They can be run independently.
The values a, b, and c in the for loop hide the a, b, and c defined outside of the loop. You can print a, b, and c after the loop to see that they still contain the values from tripleVariable:
let tripleVariable = 1, "two", "three"
let a, b, c = tripleVariable
let l = [(1,2,3); (2,3,4); (3,4,5)]
for a,b,c in l do
printfn "triple is (%d,%d,%d)" a b c
printfn "tripleVariable is (%A,%A,%A)" a b c
Result:
triple is (1,2,3)
triple is (2,3,4)
triple is (3,4,5)
tripleVariable is (1,"two","three")

Resources