Google Sheets - Count how many Rows match these conditions - google-sheets

I got an issue with Queries in Google Sheets.
I have these 3 Columns:
I would like to COUNT how many Rows have "A" in the first column and the SUM of B and C is more than 2.
This is the Query I tried:
=QUERY(Sheet5!A1:C7,"select COUNT(A) where A='A' and B+C>1 LABEL COUNT(A) ''", 1)
(I used LABEL COUNT(A) '' just to remove the header, the result doesn't change without it)
What am i doing wrong?
By using this Query, I get 1 as result (and it should be 2)

Try
=QUERY(Sheet5!A1:C7,"select COUNT(A) where A='A' and B=1 and C=1 LABEL COUNT(A) ''", 1)

=QUERY(Sheet5!A1:C7,"select COUNT(A) where A='A' and B+C>1 LABEL COUNT(A) ''", 0)

=COUNTIF(FILTER(A:C,A:A="A",B:B+C:C>1),"<>")/3

You can also use COUNTIFS and specify the conditions and the ranges for which they are applied:
=COUNTIFS(A1:A7, "=A", B1:B7, ">0", C1:C7, ">0")
Result
Reference
COUNTIFS.

Related

Query sum with greater than cell reference not working - Google Sheets

Is it possible to combine query with numeric cell reference?
I have this formula :
=QUERY(A2:C,"select sum(C) where B > 1 label sum(C)''")
But when I change to this, it doesn't work :
=QUERY(A2:C,"select sum(C) where B > '"&D1&"' label sum(C)''")
I have 1 in cell D1. I don't have any idea how.
I need to make D1 as a dropdown choice.
Thank you
If D1 is numeric, remove '
=QUERY(A2:C,"select sum(C) where B > "&D1&" label sum(C)''")

How can I avoid having to put 0s into the NULL fields to get a correct query calculation in Google Sheets

I have a Google Sheets question, which I have not been able to figure out yet with Google-Fu and RTFM:
Take the following spreadsheet as an example:
https://docs.google.com/spreadsheets/d/1IvMVaUdUDfYOoKyG0Uwd2n0M1mLjOTE5yZQ9K2R3q2M/edit?usp=sharing
In case the sheet gets lost in time, I am going to post its contents here:
Sheet1:
foo
withdrawal
deposit
C
4
10
D
10
E
10
4
As you see here, the withdrawal field for the D value being foo is empty, i.e. null
Sheet2:
foo
balance
C
=INDEX(QUERY({Sheet1!$A$2:C}, "SELECT SUM(Col3) - SUM(Col2) WHERE Col1 = '"&A2&"'"), 2)
D
=INDEX(QUERY({Sheet1!$A$2:C}, "SELECT SUM(Col3) - SUM(Col2) WHERE Col1 = '"&A3&"'"), 2)
E
=INDEX(QUERY({Sheet1!$A$2:C}, "SELECT SUM(Col3) - SUM(Col2) WHERE Col1 = '"&A4&"'"), 2)
The result is
foo
balance
C
6
D
E
-6
As you see, the balance field for the category D is null, although it should be -10.
The fix for that is to put a 0 into the deposit field in Sheet1 explicitly.
In my example, I get that data using a csv-export, and fields are generally empty and not 0, and it is cumbersome to add the 0 there. Is there a way to have something like COALESCE in that sum there (like in SQL)?
Please let me know.
it seems like something quite a bit simpler would avoid the problem:
=SUMPRODUCT(Sheet1!C:C-Sheet1!B:B,Sheet1!A:A=A2)
for cell B2.
Why don't you just add this in cell A1 of Sheet2 instead of all the Query:
=arrayformula({Sheet1!A1,"balance";if(Sheet1!A2:A<>"",{Sheet1!A2:A,Sheet1!C2:C-Sheet1!B2:B},)})
Obviously ensure cells Sheet2!A2:A and Sheet2!B1:B are empty.
If you have duplicate values of foo, try:
=arrayformula(query({Sheet1!A1,"balance";if(Sheet1!A2:A<>"",{Sheet1!A2:A,Sheet1!C2:C-Sheet1!B2:B},)},"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2) 'balance'",1))
A better option for a single-cell formula, referencing multiple sheets would be:
=arrayformula(query(
{Sheet1!A:A,n(Sheet1!B:C);Sheet2!A2:A,n(Sheet2!B2:C);Sheet3!A2:A,n(Sheet3!B2:C)},
"select Col1,sum(Col3)-sum(Col2) where Col1 is not null group by Col1 label sum(Col3)-sum(Col2) 'balance' ",1))

Google Sheet Query and locale decimal numbers

I'm using Query function in Google Sheets but even if my files are set to locale Italy when I want to filters records with value that have numbers with decimal (commas not dot) example 10,45 the query return me error.
Example:
QUERY(A1:B3;"Select count(A) where B>" & B6 & " label count(A) ''")
where A1:B3 is a simple example table like:
Stefano 50,00
Fabio 42,00
Andrea 50,00
And B6 is a cell where you put the value you want. Example if I type 48 the query return me 2.
But if you put value with decimals, example 48,3, an error is returned.
Seems that locale in the properties of sheet is not recognized by Query command.
try:
=QUERY(A1:B3;
"select count(A)
where B > "&SUBSTITUTE(B8; ","; ".")&"
label count(A) ''")
or shorter:
=QUERY(A1:B3;
"select count(A)
where B > "&TEXT(B8; "#,#")&"
label count(A) ''")

Marrying Query and Arrayformula through cell reference in Google Sheets

Trying to understand if it is possible to apply ARRAYFORMULA to situations when QUERY is used in Google Sheets.
For example, I used QUERY for querying and aggregating a set of items, like so:
=QUERY($H$2:$I$17,"select sum(I) where H='"&A2&"' label sum(I) ''",0)
But in order to make that work across the spreadsheet, I will have to drag this formula down. There is also the ARRAYFORMULA thing, which is supposed to help with getting rid of excessive dragging, however it does not seem to work with QUERY, or am I just missing something?
A picture for a quick look:
And a shared file for the longer thinking:
https://docs.google.com/spreadsheets/d/1xOdqeESrFbrBknNYahSeF0ripA5fr2vVFQ-r--lkdA0/edit?usp=sharing
use this formula:
=QUERY(H2:I17, "select H,sum(I) where H is not null group by H label sum(I)''", 0)
and then you can do simple VLOOKUP like:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A, QUERY(H2:I17,
"select H,sum(I) where H is not null group by H label sum(I)''", 0), 2, 0)))
Here two method alternatively:
first ==>
=arrayformula(sumif(H2:H,"=" & unique(filter(H2:H,H2:H<>"")),I2:I))
second ==>
=arrayformula(
query(
filter({vlookup(H2:H,{A2:A,row(A2:A)},2,false),H2:I},H2:H<>"")
,"Select sum(Col3) group by Col1 label Sum(Col3) ''"
)
)

Translating each result row of a Google Sheets QUERY operation via VLOOKUP

I have the multi-column results of a QUERY in Google Sheets, where I want to translate the strings in one column based on a lookup table in another named range. I can accomplish this indirectly with an additional VLOOKUP call, but I'd like to do it all in one go, with no intermediary steps. See this sheet for an example of my format.
The initial query I make looks something like this:
=QUERY(votes, "SELECT B, SUM(C) GROUP BY B LABEL B 'Option', SUM(C) 'Votes'")
I can then translate each row with something like this in a new column...
=VLOOKUP(A2, options, 2, 0)
... and then just pick out the columns I need:
={C2:C4,B2:B4}
How can I combine all this? I think I need to do this with an ARRAYFORMULA, but this doesn't seem to be it:
=ARRAYFORMULA(VLOOKUP(options, QUERY(votes, "SELECT B, SUM(C) GROUP BY B LABEL B 'Option', SUM(C) 'Votes'"), 2, FALSE))
=ARRAYFORMULA({VLOOKUP(INDEX(
QUERY(votes, "select B,sum(C) group by B LABEL sum(C)''"),,1), options, 2, 0),
QUERY(votes, "select sum(C) group by B LABEL sum(C)''")})
=ARRAYFORMULA(QUERY({IFERROR(VLOOKUP(Votes!B2:B, options, 2, 0)), Votes!C2:C},
"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''", 0))

Resources