Google Sheets query ("SELECT A WHERE B <> '0'") - google-sheets

I have a table for event planning where A is the items and B is the quantities.
I then want it to turn into a list where it just has the items if B is more than 0. I have tried using =QUERY(Job!A3:B16, SELECT A WHERE B <> '0') but I get #VALUE! back and the error pop up says
In Array Literal, an Array Literal was missing values for one or more rows.
Is there a better formula to use or a better way to use this?

Try
=QUERY(Job!A3:B16, "SELECT A WHERE B <> 0 ")
you can also use FILTER
=FILTER(Job!A3:A16,Job!B3:B16<>0)

Related

Reverse columns of spreadsheet

I want to reverse columns of spreadsheet. Sample is "p227", "s121", "p117", "p252", "s215" in column "A1:E1". Values are random. I want to reverse this column like "s215", "p252","p117", "s121", "p227".
I tried =TRANSPOSE(SORT(TRANSPOSE(A1:E1),1,false)). But output is "s255", "s121", "p212", "p187", "p121". Values are sorted. This doesn't reverse columns. Is there way to solve this? Should I use GAS?
Sample
A B C D E
1 p227 s121 p117 p252 s215
Expected result
A B C D E
1 s215 p252 p117 s121 p227
Thank you so much for your time.
Try
=TRANSPOSE(SORT(TRANSPOSE(A1:E1),TRANSPOSE(COLUMN(A1:E1)),0))
SORTs using COLUMN numbers in descending order
TRANSPOSE is needed as SORT only works on rows
Here's a general one
Method:
Transpose
Add row numbers
Sort descending by row number
Transpose
Remove row numbers
Formula:
=ArrayFormula(query(TRANSPOSE(sort({row(indirect("1:"&columns(A1:E2))),transpose(A1:E2)},1,false)),"select * offset 1"))
EDIT
On reflection this would have been a bit neater, adding column numbers before transposing and avoiding the Indirect:
=ArrayFormula(query(TRANSPOSE(sort(transpose({COLUMN(A1:E2);A1:E2}),1,false)),"select * offset 1"))
You can manually reassemble the columns into the order you like by using the array literal syntax:
={A:A,F:F,E:E,D:D,C:C,B:B}
Alternately, use QUERY() to re-order your columns.
QUERY(A:F, "SELECT A, F, E, D, C, B")

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)

Using Google Spreadsheets QUERY to Filter

I have the following google sheet where:
Col a= quantities
Col b= product codes, which i´ve split between C and H.
I want to know the quantity according to different "filters"... this filters are the fields between C11 and H11, and they are optional. There are 6 possible filters.
It works using =QUERY formula located in H12 and it returns the sum of quantity values where the filters match...
BUT there´s the possibility of leaving a filter empty to get "all"...
the query is as follows:
=QUERY(A1:H7, "select sum(A) where C contains '"&C11&"' and lower(D) contains lower('"&D11&"') and E contains '"&E11&"' and lower(F) contains lower('"&F11&"') and lower(G) contains lower('"&G11&"') and lower(H) contains lower('"&H11&"') ",-1)
My problem is with the match type: where C contains '"&C11&"' and...
instead of using "contains" it should compare using "matches". this way it would count like it should, but then it won´t accept empty filters.
How can I get it to count only if the field is filled??
What´s wrong with the filter right now? It´s counting "4" matches because model matches "1" happens when model is 1 or 21, also with column D where i´m looking for value X and is also counting value XN as it contains "X". if formula is changed to "matches" instead of "contains", it won´t allow me to leave it blank.
Thank you!
Karl_S formula is great, but it does not sum the quantities in column A. Adapting his approach to SUMIFS seems to do it:
=SUMIFS(A2:A7,C2:C7, IF(ISBLANK(C11), "<>" ,C11),D2:D7, IF(ISBLANK(D11), "<>" ,D11),E2:E7, IF(ISBLANK(E11), "<>" ,E11),F2:F7, IF(ISBLANK(F11), "<>" ,F11),G2:G7, IF(ISBLANK(G11), "<>" ,G11),H2:H7, IF(ISBLANK(H11), "<>" ,H11))
Use this formula instead:
=COUNTIFS(C2:C7, IF(ISBLANK(C11), "<>" ,C11), D2:D7, IF(ISBLANK(D11), "*",D11), E2:E7, IF(ISBLANK(E11), "<>",E11), F2:F7, IF(ISBLANK(F11), "*",F11), G2:G7, IF(ISBLANK(G11), "*",G11), H2:H7, IF(ISBLANK(H11), "*",H11))
If one of the options is blank, it will match all items in that column. Otherwise it should do an exact match.

Google sheets conditional formatting based on =QUERY result

I am trying to conditionally format a row in Google Sheets based on the result of a QUERY operation. I can get the QUERY to return the value I want (either 0 or non-zero), however QUERY insists on returning a header row.
Since the QUERY now takes up 2 rows for every row of data, changing the format of the row based off the QUERY result starts to get weird after just a few rows.
The problem I am ultimately trying to solve in the case where I enter a list of names, and I want to compare each name to a list of "NSF Names". If a name is entered, and it exists on the NSF list, I would like to flag the entry by highlighting the row red.
Help is greatly appreciated.
Edit: Formula, as requested:
=query(A:D,"select count(A) where C contains '"&E1&"' and D contains '"&E2&"'")
A:D is the data set (A is a numeric ID, B is full name, C and D are first and last names respectively).
E1 and E2 are placeholders for the person's first and last name, but would eventually be replaced with parsing the person's name, as it's inputted on the sheet (TRIM(LEFT(" ", A2) etc...)
I can get the QUERY to return the value I want (either 0 or non-zero),
however QUERY insists on returning a header row.
There might be better ways to achieve what you want to do, but in answer to this quote:
=QUERY(A:D,"select count(A) where C contains '"&E1&"' and D contains '"&E2&"' label count(A) ''")
A query seems a long-winded approach (=countifs(C1:C7,E$1,D1:D7,E$2) might be adequate) but the label might be trimmed off it by wrapping the query in:
index(...,2)

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