Query for does not contain - google-sheets

I have a sheet that queries three others in the same document and selects a bunch of rows where the column G contains no. Ideally I would prefer it be a query that does not contain the word yes or Yes. I have tried using all of the following mechanisms:
where not (G contains 'yes')
where not (G contains 'es')
where not (G matches '[yY]es')
where G contains 'no'
and no matter which one I pick I run into one of two issues:
Issue number 1 is that part of the query fails because only one of the values is present. Ie there is a Yes but not a yes, which results in the output of #VALUE. This is problematic because I am trying to allow for both scenarios since I cannot control what the end user of this will input into the sheet.
or Issue number 2 which is far worse. When I specifically say select all where G contains no one of the rows that displays contains a yes. Note that this also occurs with some of the other mechanisms as well but I would think specifically saying no should prevent this and does not.
The full query I am using is:
=ARRAYFORMULA({QUERY(Sheet1!A2:I500, "Select A, B, C, D, E, F, G, H, I where G contains 'no'");(QUERY(Sheet2!A2:I500, "Select A, B, C, D, E, F, G, H, I where G contains 'no'"));(QUERY(Sheet3!A2:I500, "Select A, B, C, D, E, F, G, H, I where G contains 'no'"))})
What am I doing wrong here?

The specific issue here was the query range. But the actual answer to the question (how do I query not like / not contain) is different.
I couldn't find this in the query reference but the answer is to put not straight after the where:
"Select A, B, C, D, E, F, G, H, I where not G contains 'yes'"
To further improve, you should lower(G) to make it exclude 'Yes' and 'yes'

The problem is the query range. It starts a Row2 which contains data rather than labels. By default the first row of a query is treated as heading for the output (so selection criteria are not applied to it). Hopefully a fix is just to adjust the query to exclude headings. Please try:
=QUERY(Sheet1!A2:I500,"Select * where G = 'no'", 0)

Related

InsertRowBefore() + Defined Range question

I have a bit of code that inserts a blank row to the top of a sheet and copies data into it.
I also have a defined range on that sheet.
Every time a row is inserted, the defined range moves down a row.
The defined range is helping power a query on another sheet and therefore is always missing the inserted rows.
If i delete the query and use importrange, it works no issue.
However the query doesn't work with importrange.
The easiest fix I'm assuming would be to 'hard-code' the range, but how would i call that in my query?
Here is the query, followed by the same query using importrange.
=IFERROR(QUERY(STOCK_IN_DATA,"SELECT *" & IF((LEFT(D4,1))="*","WHERE D STARTS WITH '"&RIGHT(D4,LEN(D4)-1)&"' AND C CONTAINS '"&$C4&"' AND E CONTAINS '"&$E4&"' AND F CONTAINS '"&$F4&"' AND G CONTAINS '"&$G4&"' AND H CONTAINS '"&$H4&"'","WHERE C CONTAINS '"&$C4&"' AND D CONTAINS '"&$D4&"' AND E CONTAINS '"&$E4&"' AND F CONTAINS '"&$F4&"' AND G CONTAINS '"&$G4&"' AND H CONTAINS '"&$H4&"'")),)
=IFERROR(QUERY(IMPORTRANGE("SHEETNAME","MASTER_IN!$B$1:M"),"SELECT *" & IF((LEFT(D4,1))="*","WHERE D STARTS WITH '"&RIGHT(D4,LEN(D4)-1)&"' AND C CONTAINS '"&$C4&"' AND E CONTAINS '"&$E4&"' AND F CONTAINS '"&$F4&"' AND G CONTAINS '"&$G4&"' AND H CONTAINS '"&$H4&"'","WHERE C CONTAINS '"&$C4&"' AND D CONTAINS '"&$D4&"' AND E CONTAINS '"&$E4&"' AND F CONTAINS '"&$F4&"' AND G CONTAINS '"&$G4&"' AND H CONTAINS '"&$H4&"'")),)
What the query does is create a nice little search filter in the cells shown on row 4.
QUERY can use A, B, C columns names only when a reference is used as the first argument. When an array is used as the first argument, as in this case IMPORTRANGE, you should use Col1, Col2, Col3 as column names instead of A, B, C respectively.

Ignore case sensitive for given word

I have a search Query where I'm looking for values in my table. And I want to dynamically search for a value, for example, "AMAZON".
="select B, C, L, E, O, P, Q WHERE B >= date '"&TEXT(A2; "yyy-mm-dd")&"' and B <= date '"&TEXT(B2; "yyy-mm-dd")&"' and L matches '.*"&C2&".*' and E LIKE '%"&D2&"%'"
"C2" is my empty cell, for searching for a string. I, for example, input "amazon" into "C2"
My problem is that is doesn't return anything as in my table it's written "AMAZON" or probably "Amazon" so it won't find anything as I have a direct link to "C2"I couldn't figure out that way of phrasing the regular expression to ignore the case sensitive.
It would return anything as soon as I write "AMAZON" in "C2".
Goal:
I want to make it non case sensitive. Cause at the moment my formula won't print anything as soon as I don't write it in initial letters.
Please try lower in all cells you reference as well as all depending, returning columns.
So your formula would be:
="select B, C, L, E, O, P, Q WHERE B >= date '"&TEXT(A2; "yyy-mm-dd")&"' and B <= date '"&TEXT(B2; "yyy-mm-dd")&"' and lower(L) matches '.*"&lower(C2)&".*' and lower(E) LIKE '%"&lower(D2)&"%'"

Compare dates, if date is most recent, then return adjacent cell data

I'm working on a Google Sheets document for tracking covid-19 cases reported in nursing homes, institutions, etc. I'm heavily employing formulas across my sheets to minimize the number of ways my coworkers can accidentally screw up the numbers. I've made a copy of the pertinent sheet here so you can take a look.
I've run into a problem, though. So, on one sheet, we have all records of articles that mention statewide case/death numbers. We want to continue to keep track of those, but also be able to easily retrieve/display the most recently reported numbers for any given state. Essentially what I'm trying to do is:
Delimit search to only rows where column A contains a specific state (AL, FL, GA, etc)
Of those rows, compare the date values in column B, identify the most recent one
Once the most recent entry for a given state is found, then output the value of column E (or D, or C, doesn't matter, I assume that changing that part will be straightforward enough)
I got as far as =MAXIFS(B3:B100, A3:A100,"CT") but I'm not sure syntactically what to tell it next, or if I'm going about this the right way.
Update: We have gotten closer with =query(A1:E,"select A, C where A = 'CO' and B = date '"&TEXT(DATEVALUE(max(B2:B)),"yyyy-mm-dd")&"'",1) and =query(A1:E,"select A, C, D, E where A = 'CO' and B = date '"&TEXT(DATEVALUE(max(B2:B)),"yyyy-mm-dd")&"'",1), but in practice this only returns rows with the absolute most recent date, rather than the most recent date relative to which state's dataset you're looking at.
Need to be able to display the relative most recent entry per state.
UPDATE UPDATE: I figured it out! (sheet name added because I'm referencing it from a different sheet)
=query('Statewide Reported'!A1:E,"select A, B, C, D where A = 'NJ' and B = date '"&TEXT(maxifs('Statewide Reported'!B3:B100, 'Statewide Reported'!A3:A100, "NJ"),"yyyy-mm-dd")&"'",1)
Thanks so much for pointing me in the right direction!!
You could use query() to select the number of deaths for CO matching the most recent date using this formula:
=query(A1:E,"select A, C where A = 'CO' and B = date '"&TEXT(DATEVALUE(max(B2:B)),"yyyy-mm-dd")&"'",1)
If you want deaths, cases, and total affected you can use:
=query(A1:E,"select A, C, D, E where A = 'CO' and B = date '"&TEXT(DATEVALUE(max(B2:B)),"yyyy-mm-dd")&"'",1)
Update:
This will find the max date for a specific State (CO):
=query($A$1:$E,"select A, C, D, E where A = 'CO' and B = date '"&TEXT(maxifs($B$2:$B,$A$2:$A,"CO"), "yyyy-mm-dd")&"'",1)
Or if you want to reference a State name in a cell (H1) you can use this formula:
=query($A$1:$E,"select A, C, D, E where A = '"& $H$1 &"' and B = date '"&TEXT(maxifs($B$2:$B,$A$2:$A,$H$1), "yyyy-mm-dd")&"'",1)

Exclude Header QUERY

I want to construct a Query that takes data from multiple sheets and lists the contents of them all. All sheets have the same variables and should, therefore, be combined. When typing in 0 as the last argument of the query I always receive an error.
I am using the following formula and have tried to input 0 as well as FALSE as the last argument. Every time I received an error. In addition to this, I tried including label AI '' at the end of the statement which worked but I could not seem to also have a blank for AJ. Furthermore, I suspect that the blank spaces will still be shown which I don't want.
={QUERY(PRIO!A:AX, "select D, E where B matches 'Negative' order by D");
QUERY(collated!A:AJ, "select AI, AJ where AG matches 'Negative' order by AJ")}
not sure what went wrong because you didn't provide a copy of your sheet but try:
={QUERY(PRIO!A1:AX, "select D, E where B matches 'Negative' order by D", 1);
QUERY(collated!A2:AJ, "select AI, AJ where AG matches 'Negative' order by AJ", 0)}

Google Spreadsheet's Query function returns empty data with order by

Google Spreadsheet's Query function returns empty data with order by.
Here is the Query that is working properly:
=QUERY('OtherListName'!A1:C;"Select A, B, C";1)
This query returns exactly what you'd expect: the contents of the range A1:C (which has 6 lines in my case).
Then I try to order the returned data:
=QUERY('OtherListName'!A1:C;"Select A, B, C order by A, B";1)
This query only return the first line (that contains headers), and nothing more.
The original set of data in the OtherListName contains only strings and integers.
What I want is to get the data ordered by column A and then by column B. Both columns only contain strings. The corresponding integers are in the column C.
Please share some advice on this, I haven't found anything yet. Meanwhile I'll continue with my experiments to find out the reason of this "error".
Thanks.
try filtering out the empty rows...
=QUERY('OtherListName'!A1:C; "Select A, B, C where A <>'' order by A, B";1)
Or, if the data in A is numeric
=QUERY('OtherListName'!A1:C; "Select A, B, C where A is not null order by A, B";1)

Resources