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)''")
Related
Good morning!
I want to ask if it is possible that the cell reference for "houses" on "worksheet!C3" return values for A&B only or A&C only or B&C only while only using one cell ("worksheet!C3") as cell reference, or is there a better approach on trying to return the values?
formula I'm using:
sum(query(Encoding!$A$4:$K$9,"Select D where (A contains '"&C$2&"') and (B contains '"&C$3&"') and (C = date '"&text($B$4,"yyyy-mm-dd")&"')"))
Sample data is attached,
https://docs.google.com/spreadsheets/d/1sY6YQRQ6Im-zTdzf0u31rphnQR0eL_YzDahpgMQ7E1Y/edit#gid=37782805
Try, as far as I understood the subject correctly
=sum(query(Encoding!$A$4:$K$9,"Select D where (A contains '"&C$2&"') and (B contains '"&C$3&"' or B contains '"&C$4&"') and (C = date '"&text($B$4,"yyyy-mm-dd")&"')"))
you can also put one formula in A5 as follows
=sort(query(arrayformula(split(flatten(Encoding!A4:A9&"~"&Encoding!B4:B9&"~"&Encoding!C4:C9&"~"&Encoding!D3:K3&"~"&Encoding!D4:K9),"~",true)),
"select Col4,sum(Col5) where Col3="&B4&" and (Col2 contains '"&C$3&"' or Col2 contains '"&C$4&"') and Col1="&C2&" group by Col4 label sum(Col5) ''"),1,0)
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.
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))
I need if function to count amount of size from column in C and D automatically.
Could anybody help me?
I count it manually by sorting male and female first then I used formula =COUNTIF(C2:C6,"XXL") to get how many male with size XXL.
Below is the picture in spreadsheet, and the green color is the result I want.
Sample
Clear E:J and put it in E1
=QUERY(C:D,"select D,count(D) where D is not null group by D pivot C",1)
Or only clear G:G and J:J, put it to G2 first and then copy and apply to J2
=ARRAYFORMULA(IF(E2:E="",,COUNTIFS($D:$D,E2:E,$C:$C,E1)))
Simply use
=QUERY(A1:D, ”SELECT D, COUNT(D) GROUP BY D PIVOT C”,
1)
I'm using Google Sheets and using the following formula without any issue:
=QUERY(Sheet!A:D,"select A, max(B) where not B is null group by A label A 'Client', max(B) 'Most Recent'")
Obviously, this is returning columns A and B of the referenced data
However, I would like to also return the corresponding cells from column C.
sample data: https://docs.google.com/spreadsheets/d/1CJKiDNPUWMMUbLLb-UDA6iRA2PBnlMHDsEB9vELe0Rk/edit?usp=sharing
Using this example, what I would like to see is cell G2 populated with "Pizza" (i.e., from C3), and so on.
Am I using the right formula? What do I need to change?
What you are trying to do is not very SQL-like, because max(B) does not point to any particular row: it's just the maximum value of B among the selected rows. This value could be attained by several rows, making the choice of C, D ambiguous. I don't think that a single query can do this.
Here is an implementation with unique and several query commands.
In E2, enter =unique(A:A) to get the list of unique client names.
In F2, enter
=query(A:D, "select B,C,D where A ='"&E2&"' order by B desc limit 1")
and drag this formula down. This selects all rows with the A value matching E2, and picks one with maximal B value.
You don't want to have a header row in the output of these queries so either add label B '', C '', D '' or just don't include the header row in the queried range.
Version with grouping by C,D
To also select C and D within a single query, expand the select clause
select A, max(B), C, D
which will then require including those in the group by clause:
group by A, C, D
The formula will be
=QUERY(A:D,"select A, max(B), C, D where not B is null group by A, C, D label A 'Client', max(B) 'Most Recent'")
This does mean that the only rows to be grouped together will be those where not only A but also C and D are equal.
A possible one-formula solution:
=ArrayFormula(IFERROR(VLOOKUP(UNIQUE(A2:A),SORT(A2:C,2,0),{1,2,3},0)))
Here is what I did that finally worked.
In E1, enter =unique(A:A) to get the list of unique client names. In F2, enter
=query(A$2:D101, "select B,C,D where A ='"&F2&"' order by B desc limit 1")
and drag this formula down. This selects all rows with the A value matching E2, and picks one with maximal B value.
You don't want to have a header row in the output of these queries so just add desired text in header row.