Query : sum for all key containing given substring - google-sheets

I have the following table:
A
B
Luka, Jean
1
Luka
2
Jean
3
Luka, Jean, Etienne
4
Luka, Jean
5
I would like to have the sum total of every lane containing the name "Luka" and the name "Jean". I am able to have this result but for each unique key containing these names.
My query is : =QUERY(A1:B,"select A, sum(B) where A contains 'Luka' and A contains 'Jean' group by A")
The result is:
sum
Luka, Jean
1
Luka, Jean, Etienne
4
However I would like to have the total result for this query in one unique cell. I think I need to have nested queries but I don't know how to do it.
Expected result :
5 (in one cell without any header or key)

You can try =INDEX(QUERY(QUERY(A1:B,"select A, sum(B) where A contains 'Luka' and A contains 'Jean' group by A"), "select sum(Col2)", 1), 2)
or an actually easier way using your formula itself =INDEX(QUERY(A1:B,"select sum(B) where A contains 'Luka' and A contains 'Jean'"), 2)

First, it seems to me that the total given the sample data in your post should be 10, not 5 (it seems you were not including row 5 for some reason).
Assuming I am correct:
=SUM(FILTER(B:B,ISNUMBER(SEARCH("Luka",A:A)*SEARCH("Jean",A:A))))

Related

Query Select Row and Column together one statement

I work on a google sheet, where I should see the number of holidays per day for each team.
The table looks like this:
[table]
A
B
C
D
E
1
Employee Name
Team Name
01/03/2022
02/03/2022
03/03/2022
2
Employee 1
Team A
hol
hol
hol
3
Employee 2
Team B
early
early
early
4
Employee 3
Team A
hol
late
late
5
6
Team A
7
01/03/2022
8
RESULT HERE
I want to have a result that tells me that Team A had on 01/03/2022 2 holidays.
=countif(query(A1:E4,"select C where B contains '"&A6&"'" ),"hol")
A6 contains the team I am looking for.
A7 contains the date I am looking for.
A8 should show me the number of hol.
Currently, I have a fixed column to look inside which is "C". I want to replace that with the date from A7 - How do I do that?
I tried to play around with transpose and filter but had success.
try:
=COUNTIF(FILTER(FILTER(C2:E4, C1:E1=A7), B2:B4=A6), "hol")
Use XMATCH to get the Col number for QUERY and count inside query instead of COUNTIF:
=QUERY(
{B1:E4},
"Select count(Col1)
where Col"&XMATCH(A7,B1:E1)&"='hol'
and Col1='"&A6&"'
label count(Col1) ''",
1
)

Total Sum With Vlookup

I have two spreadsheets with names and times. One is specific session times and on the second sheet, I want to sum up the total times based on each instance from sheet one.
Sheet 1: Session Time
Name | Time
David 5
Mike 2
Daniel 3
David 2
Daniel 8
Sheet 2: Total Time (The one for which I need a forumula)
Name | Total Time
David 7
Mike 2
Daniel 11
I tried a countif and vlookup but I couldn't get it match more than one instance of the name on sheet 1. I also tried this suggested formual from a suggested post but its not summing a second instance of the user name
=ARRAYFORMULA(SUM(ifna(VLOOKUP(A2, 'Sheet 1'!A3:AA16, 5, false))))
A2 = David (On Sheet 2)
Sheet 1'!A3:AA16 = List of names in columns A and all the way to AA is a series of columns with numerical values
5 = the column number from with I want to return the sum for every instance of David (2 in this example with a total of 7)
False = its not sorted
Any help is mucho appriciado!
You can use this formula in your Sheet2!B1:
={"Total Time";arrayformula(if(A2:A<>"",sumif(Sheet1!A1:A,A2:A,Sheet1!B1:B),""))}
Or simply use =arrayformula(if(A2:A<>"",sumif(Sheet1!A1:A,A2:A,Sheet1!B1:B),"")) in Sheet2!B2
Get the sum of the range based on a specific criteria using SUMIF()
Basically, get the sum of Sheet1!B1:B if Sheet1!A1:A matches the current cell being checked in arrayformula A2:A
Output:
This can be accomplished with a simple QUERY, e.g.,
=QUERY('Session Time'!A:B, "Select A, SUM(B) WHERE A Is Not Null GROUP BY A LABEL SUM(B) 'Total Time'")

Google Query Language for sum of result using cell reference in query

Hello and thanks for your help. I'm new to GQL but have good SQL experence and think I may be missing something small.
I have 2 sheets i'm working with
Main sheet
Colum G
InstanceID
i-554532f4693fc6186
i-09554fcda5f2f3262
i-0047551ae514412d5
-
Data Sheet
Colum A Colum B
i-554532f4693fc6186 10.12
i-554532f4693fc6186 12.12
i-554532f4693fc6186 13.12
i-554532f4693fc6186 17.12
i-554532f4693fc6186 30.12
I am trying to write a query that will find all the rows that match the Instance ID in column G against the datasheet Column A and return the AVG of all the matches in column B, the top 5 max, and top 5 min.
I'm finding that I can't point the query to a cell for referencing the instance ID. Is there a way?
I'm using this to try to get the max and it works for 1 but I ned the top 5 or any number.
=sort(query('HeC-Metrics'!A:B,"select max(B) Where A = 'i-044532f4693fc6186'"))
I'm OK needing to do different queries for each of the required results, AVG, min, max. I would also like to reference the cell in the G column so I don't have to manually enter the InstanceID.
Thanks your time.
Stephen
So it's just a case of getting the right syntax to use a cell value as a match in the query
=query(Sheet2!A:B,"select avg(B) where A='"&G2&"' group by A label avg(B) ''",1)
Note that you don't really need the group by if you already have a list of distinct ID's to compare against, but you can't have an aggregate like avg without it.
To get the bottom 5, you can use filter & sortn
=transpose(sortn(filter(Sheet2!B:B,Sheet2!A:A=G2),5))
(I have transposed the result to get it in a row (row 2) instead of a column)
or you could use a query
=transpose(query(Sheet2!A:B,"select B where A='"&G2&"' order by B limit 5 label B '' ",1))
Similarly to get the top 5 you could use
=transpose(sortn(filter(Sheet2!B:B,Sheet2!A:A=G2),5,,1,false))
or
=transpose(query(Sheet2!A:B,"select B where A='"&G2&"' order by B desc limit 5 label B '' ",1))
This begs the question of whether you could get these results (a) without needing a list of distinct values and (b) in a single array formula without copying down.
You could certainly get the distinct ID's and averages straight away from a query. Getting the top or bottom n values from a number of groups is much more difficult. I have attempted it in a previous question, but it requires a long and unwieldy formula.

Count Filtering and Arrayformulas in Google Sheets

I have a table of issued documents that I need to have numbered based on the name of the document and when it was issued, ie. the issuing of document 'a' on the 3/20/2039 was the 3rd document 'a'. Like this:
name date order
a 3/20/2039 2
a 20/10/2099 3
a 10/12/2001 0
a 2/11/2019 1
b 2/12/2010 0
b 3/24/2017 1
b 3/20/2139 2
a 3/24/2111 4
a 3/24/3019 5
a 3/24/3034 6
I have a formula which is able to filter out and count the older dated versions of all documents under that same name:
=COUNTIF(FILTER(A:A,A:A=A2,B:B<B2),A2)
However, I can't create an arrayformula which works to do the same thing. I thought it could be:
=COUNTIF(FILTER(A:A,A:A=A2:A,B:B<B2:A),A2:A)
But this seems to be comparing and counting the entire range each time. Anybody know how to do this? I'm just getting to grips with Arrayformulas and this would be a fantastic help.
For anyone interested, an example table is shared here:
https://docs.google.com/spreadsheets/d/15xnauVjACbWow1aTXVLMBawBuhZ0QcXdsh4HWHVYjyU/edit?usp=sharing
You could use query like this:
=query(A2:A,"Select A, count(A) where A is not null group by A label count(A) ''")

vlookup return array (formula only, no code please)

I have a table like so:
A B C D
1 3 2 1
2 3 2 1
3 0 2 2
I want to vlookup the first column so that it returns the 3 columns after.
For example if I look up 2, i want the return to be 3 2 1.
i need it in one formula because it will go into a sumproduct expression.
Does not have to be a vlookup. I just need something that will return me an array though some type of indexing or lookup.
One of the ways to achieve this is query. So for the data given by you (A, B, C are column), you can use the following formula to get all three columns for the lookup value 2
=QUERY($A$1:$D$3,"select B,C,D where A = 2")
Then you can apply any aggregate function on top of this too. For eg:
=sum(QUERY($A$1:$D$3,"select B,C,D where A = 1"))
Check this link

Resources