I have a Google Sheets database that I want to query. I want the query to be based on data in columns B, C, and D and return all data if the conditions are met. I have input cells for the search criteria for those three columns, and I'd like the query to do the following:
Ignore search criteria where there is no value entered in the search cell
Use AND logic where there is a value in a given cell
For example, search criteria in input cells:
B = "1"
C = Blank
D CONTAINS "the"
I want the query to return data where C is anything and B = "1" and D CONTAINS "the".
Another example:
B = "1"
C = "E"
D CONTAINS "the"
I want the query to return data where all three conditions are met
I'd like D to not be null in all cases.
I'm having trouble incorporating the empty criteria into the query. I've tried dynamically using AND/OR depending on whether there's data in a given search cell, but I can't get it to work.
So, I created a crazy-long query to account for all possible combinations with B, C, and D. This works, but I want to incorporate more search criteria, and this equation becomes exponentially more complicated with 4, 5 or more search criteria.
Here is an image of the query input and query results for the first example above (I'm not sure why some of the values in the Name column don't contain "the").
Image of Query Sheet
And here's an image of the database that I'm querying against:
Image of Database Sheet
Here is the equation I'm using:
=if(and(B4="",C4=""),query(DATABASE!A1:E,"SELECT * WHERE D contains '"&D4&"' and D IS NOT NULL order by D asc",1),
if(and(B4<>"",C4=""),query(DATABASE!A1:E,"SELECT * WHERE D contains '"&D4&"' and B matches '"&B4&"' and D IS NOT NULL",1),
if(and(B4="",C4<>""),query(DATABASE!A1:E,"SELECT * WHERE D contains '"&D4&"' and C matches '"&C4&"' and D IS NOT NULL",1),
if(and(B4<>"",C4<>""),query(DATABASE!A1:E,"SELECT * WHERE D contains '"&D4&"' and B matches '"&B4&"' and C matches '"&C4&"' and D IS NOT NULL",1),
if(D4="",query(DATABASE!A1:E,"SELECT * WHERE B matches '"&B4&"' "&IF(AND(B4<>"",C4<>""),"AND","OR")&" C matches '"&C4&"' and D IS NOT NULL",
1),FALSE)))))
My ask: I'd like to simplify this equation so that it can be extended to multiple search criteria (more than 3). Thank you for your help!
Try this:
=QUERY(
DATABASE!A:E,
"WHERE D IS NOT NULL"
& IF(B4 = "",, " AND B = " & B4)
& IF(C4 = "",, " AND C = '" & C4 & "'")
& IF(D4 = "",, " AND D CONTAINS '" & D4 & "'")
& " ORDER BY D",
1
)
Related
Struggling with arrayFormula and vlookup with match to reference data in another sheet tab to pull a percentage based on row and column membership.
I currently have: =ArrayFormula(vlookup(Orange&7,{A1:A&B1:B,C1:E},match("Skill 3",A1:E1,0)-1,0))
In the shared Google sheet, I have placed notes and explanations. This is a reference sheet for a game, where it refers to Hero skills statistics.
MISSION: Using inputted Hero Class "colour" and "Skill Level" get the "percentage" from the appropriate skill column. This will be needed 3 times to cater for the 3 different skill types in question.
Here is the sheet: TEST Sheet
Approach
Creating a VLOOKUP using more than one criteria will require a helper column made by the concatenation of the two(or more) criteria.
In your case you should build a column that concatenates the Color & Level. Your Lookup will search that value and return the corresponding percentage in the 4th column.
Having your table in Hero Lead like this:
+==========================================+
| A | B | C | D | E | F |
+------+-----+-----+-------+-------+-------+
|Lookup|Class|Level|Skill 3|Skill 4|Skill 7|
+------------------------------------------+
You can use this formula to retrieve the correct value of Skill 3
=VLOOKUP("Orange"&7,'Hero Lead'!A:D,4,0)
Just replicate the formula with the other parameter to retrieve the correct percentage level.
Here is a solution:
=IFERROR(
INDEX(
QUERY(
'Hero Lead'!$A$2:$E,
"select C, D, E
where A = '" & $C$36 & "' and
B = " & G36,
-1
),
0,
MATCH(
"Skill " & F36,
'Hero Lead'!$C$1:$E$1,
0
)
),
0
)
This gets you the right skill's boost from the row filtered using QUERY by level and colour.
If you need all three at once for fixed colour and fixed level (in a column thus TRANSPOSE):
=TRANSPOSE(
FILTER(
'Hero Lead'!$C$2:$E,
'Hero Lead'!$A$2:$A = $C$36,
'Hero Lead'!$B$2:$B = G36
)
)
or:
=TRANSPOSE(
QUERY(
'Hero Lead'!$A$2:$E,
"select C, D, E
where A = '" & $C$36 & "' and
B = " & G36,
-1
)
)
I am using a query formula to import data from a different sheet. However, after querying the data, I only need it to show data with value only. How to remove data without value when I query?
My query formula is the following:
=QUERY(Paste!A:G,"SELECT A,B,C,D WHERE B ='"&$C$6&"' or B ='"&$C$7&"' or B ='"&$C$8&"' or B ='"&$C$9&"' or B ='"&$C$10&"' or B ='"&$C$11&"' ORDER BY D DESC")
My spreadsheet link is the following:
https://docs.google.com/spreadsheets/d/1A6lGIlU147Y_0WFD7Btkq4IMuY-Z3D1HsNiTgjLjm0o/edit?usp=sharing
At above image can see data without value also showing. I want to Query data with value only.
use:
=QUERY(Paste!A:G,
"select A,B,C,D
where D <> 0
and D is not null
and (B ='"&$C$6&"'
or B ='"&$C$7&"'
or B ='"&$C$8&"'
or B ='"&$C$9&"'
or B ='"&$C$10&"'
or B ='"&$C$11&"')
order by D desc")
I'm trying to write a query that looks compares strings within a cell on two different pages. This is the query I have written but it says unable to parse query string because there is no column b4.
=QUERY(Teams!B:F, "select F where B = B4", 0)
Note column B is on a different page than B4.
if B4 is number do:
=QUERY(Teams!B:F, "select F where B = "&B4, 0)
if B4 is not number do:
=QUERY(Teams!B:F, "select F where B = '"&B4&"'", 0)
I've been using the following function:
=query(Sheet1!A2:D," select A, B, C where A matches '"&JOIN("|", A2:A)&"' and D matches 'yes'")
Is there anyway that I can make is so that every row that starts with a match will be added a comma separated list in which each column occupies one cell with no duplicates as shown in sheet3.
https://docs.google.com/spreadsheets/d/1YDxIUnZzzYde9hcexPoDegv4HBuiUwk2wLKSXazu9hE/edit?usp=sharing
Sheet 2 has the function that I used and the result.
It is not entirely clear what you want to do, but try this. In Sheet1 in E2 put this combining Col A and D:
=arrayFormula(A2:A & if(isBlank(D2:D),""," ") & D2:D)
In F2 combine Col C and D with this:
=arrayFormula(B2:B & if(isBlank(C2:C),"",",") & C2:C)
In G2 find the unique values from Col F with this:
=UNIQUE(E2:E)
In H2 put this and drag the formula down:
=join(",",query(E2:F,"select F where E contains '"& G2 &"'"))
Hide Cols E & F
I have a list of number using this query formula.
=query(uploadData!A:M,"Select B, C, H, I, M where not(C) contains '"&JOIN("|",filter!A:A)&"' and B contains 'Incoming' and not B is null and not H is null ",1)
You can see I'm trying to filter the results so as to NOT include any matching numbers found in filter!A:A. However, it doesn't like the fact that the result and filter sheet use the telephone number format (###) ###-####.
I believe using regexreplace will help resolve this issue but I'm not knowledgeable enough with regexreplace to know how to incorporate it.
Can anyone tell me how I can achieve this?
I'm not sure, google sheets query understand pipes | as OR logic, so you may rty this formula instead:
=query(uploadData!A:M,"Select B, C, H, I, M where not C contains '"&JOIN("' and not C contains '",filter(filter!A2:A,filter!A2:A<>""))&"' and B contains 'Incoming' and not B is null and not H is null ",1)
I used JOIN("' and not C contains '",filter(filter!A2:A,filter!A2:A<>"")):
join + "' and not C contains '" to get all conditions one by one
filter(filter!A2:A,filter!A2:A<>"") to have only not empty cells