Google Sheets With OR in Query - google-sheets

I am using OR criteria in query to exclude data with specific text but it seems that OR criteria is not working. Check The below image in which I used the query function to exclude data where Column B is 'West' or 'East' but the its not working.
Any help on above will be appreciated.
Formula used:-
=QUERY(A1:B14,"Select * Where A is not null and (B<>'West' or B<>'East')")

A1 is not null, B1 is not "East". Both are true for C1. You're looking for and. See De Morgan's law: When you negate, you use and.
=QUERY(A1:B14,"Select * Where A is not null and B<>'West' and B<>'East'")
Or
=QUERY(A1:B14,"Select * Where A is not null and not (B='West' or B='East')")

use:
=QUERY(A1:B14, "where A is not null and not B matches 'West|East'", )

Related

Query Function with unique Filter 2 column when the criteria match in google sheet

I have a Maintenance Schedule sheet for Fleet maintenance in that I need to copy the data to another sheet whenever service status is "Live" and Equipment, Types of service to filter is unique. I need help to find out the formula.
Sample formula I tried whether it is right or wrong:
Spreadsheet Link
=ARRAYFORMULA(query('Master Schedule'!A1:O,"Select B,E,F,M,N,O where O='Live'",1),
Query('Master Schedule'!A1:O,"Select * "&UNIQUE('Master Schedule'!B2:E)&"",1))
=Query(Query('Master Schedule'!A1:O,"Select B,E,F,M,N,O where O='Live'"),
"Select '"&UNIQUE({'Master Schedule'!B:B, 'Master Schedule'!B:B&'Master Schedule'!E:E, 'Master Schedule'!E:E})&"'",1)
=Arrayformula(VLookup(Filter(Unique('Master Schedule'!B:B &'Master Schedule'!E:E);
Unique('Master Schedule'!B2:E)<>"");Query('Master Schedule'!A:O; "Select B, E,F,M,N,O ";1); {1\2\3\4\5\6};0))
Master Schedule
Result Required
Try this
=query(arrayformula(IFERROR(VLOOKUP(unique((Data!B:B&"~"&Data!E:E&"~"&Data!F:F&"~"&Data!M:M)),
query({arrayformula(Data!B:B&"~"&Data!E:E&"~"&Data!F:F&"~"&Data!M:M),Data!B:B,Data!E:E,Data!F:F,Data!M:O},"select * where Col7='Live'"),
{2,3,4,5,6,7},0))),"select * where Col1 is not null")
or with semicolon notation
=query(arrayformula(IFERROR(VLOOKUP(unique((Data!B:B&"~"&Data!E:E&"~"&Data!F:F&"~"&Data!M:M));
query({arrayformula(Data!B:B&"~"&Data!E:E&"~"&Data!F:F&"~"&Data!M:M)\Data!B:B\Data!E:E\Data!F:F\Data!M:O};"select * where Col7='Live'");
{2\3\4\5\6\7};0)));"select * where Col1 is not null")
explanation
build a unique reference as unique(Data!B:B&"~"&Data!E:E&"~"&Data!F:F&"~"&Data!M:M)
build a new matrix with this reference and only 'live' rows as query({arrayformula(Data!B:B&"~"&Data!E:E&"~"&Data!F:F&"~"&Data!M:M),Data!B:B,Data!E:E,Data!F:F,Data!M:O},"select * where Col7='Live'")
then apply vlookup

query and variable in google spreadsheet

I have the following query in google spreadsheet (!classement H6)
=QUERY(classement!A2:C367;"SELECT COUNT(B) WHERE B='worry' AND C=2")
This query works and I have good result
If I want to reference a cell with the same value I12=2 my result is empty
=QUERY(classement!B2:D367;"SELECT COUNT(B) WHERE B='worry' AND C= '"&classement!I12 &"'")
here is my google spreadsheet exemple
Do you have an idea ?
Thanks
Noliverte
You just need to remove the ticks (') as in your first the number is not with ticks, but in the second it is, this is because you are looking for a number value
=QUERY(classement!B2:D367;"SELECT COUNT(B) WHERE B='worry' AND C= "&classement!I12 &"")

Output a normalized column with a calculated field in QUERY

In Google Spreadsheet, I have the expression:
=QUERY(database; "select b,c, where a='anyone-condition' order by c desc";-1)
The column a,b are strings and c is a number.
How can I include in this query one calculated field, c/sum(c)?
Although Ed Nelson is perfectly right, you can be creative with multiple nested Queries and get desire result without an extra column.
I have build simple example that will show you the way
Data
Solution 1 - simpler but with ugly column name
=QUERY(
A1:C3,"select A,B,C/"&QUERY(
QUERY(A1:C3,"select sum(C)"),"select * offset 1",0)&"" ,1
)
Solution 1.1 - more complex but with custom column name
=QUERY(
A1:C3,"select A,B,C/"&QUERY(
QUERY(A1:C3,"select sum(C)"),"select * offset 1",0)&
" label C/"&QUERY(
QUERY(A1:C3,"select sum(C)"),"select * offset 1",0)&
" 'C/sum'",1
)
Finals:
You can go further and add formating to last column as well
Link to working copy
For your specific example probably code should be like this:
=query(database; "select b,"&query(query(database; "select sum(c), where a='anyone-condition' "),"select * offset 1",0)&" where a='anyone-condition' order by c desc";-1)
But, it should be tested against real data
Is that serves your needs?
You can't do a sum without doing a group which you don't want. You can add a column D to your sheet to sum. Try =sum($C$2:$C) in D and copy down. Then this will work:
=query(database, "select B,C/D where A='anyone-condition' order by C desc label C/D ''",-1)
The problem is with the Google Spreadsheet 'kernel', not with my coding. Look the scream that I printed
https://drive.google.com/file/d/1yJbcfVZ1tDmW8WoG224PALRIwyEbX9LH/view?usp=sharing

IS NOT NULL - A simple google sheets query issue

I want my google sheets query to just filter out the rows with a blank value and possibly the ones containing specific words as well
Here's an example spreadsheet, I've tried using "where B is not null" in the query but it's not working! I'm tearing my hair out over this as i'm sure it's a simple solution.
https://docs.google.com/spreadsheets/d/1xZH1MKeqLa8r1gRGL9wYHEWe02wQoNNpqqL_ca1fGf0/edit?usp=sharing
Thanks in advance for your help!
you may need to test for empty string instead of null, you can add AND to remove other values:
=QUERY(A2:B6,"select A, B where B<>'Pizza' and B<>''")
This just worked for me
=QUERY(A2:B6,"select A, B where B!='' ",0)

Google Spreadsheet multiple column filter using OR

I have a Google Spreadsheet with 3 columns that are either blank or have a value. I want to get the count of the number of rows that has A and either B or C populated. If I were writing a SQL query it would be
select count(*)
from Table
where A is not null and (B is not null or C is not null)
But I can't for the life of me figure out how to get this in a Google Spreadsheet
The formula below should do what you are after:
=ROWS(FILTER(A2:A, NOT(ISBLANK(A2:A)), NOT(ISBLANK(B2:B))+NOT(ISBLANK(C2:C)) ))
And to explain:
ROWS counts the rows of the argument (filtered, in our case)
FILTER returns the rows of arg1 (A2:A) that all subsequent arguments match
The + (addition) symbol combines two predicates with a logical OR
Finally, if you are not using header columns you can change the references from A2:A to A:A
Alternatively, you can use the QUERY function:
(Broken into multiple lines for readability)
=ROWS(QUERY(A2:C,
"SELECT A WHERE A IS NOT NULL AND (B IS NOT NULL OR C IS NOT NULL)"))
For more information on the syntax of the queries, see the Visualization API Query Language Reference and specifically the Language Reference
=SUMPRODUCT(((A:A<>"")*((B:B<>"")+(C:C<>"")))>0)
if there is only one argument for SUMPRODUCT() it works just as SUM(ARRAYFORMULA(N( )))

Resources