query with group by and count missing zero counts Google sheets - google-sheets

I can get the counts for column E (there are no empty cells in the range) with
=QUERY(Sheet1!D2:H,"select D, count(E) where F = 'person-person' group by D")
But if I add a second where clause
=QUERY(Sheet1!D2:H,"select D, count(E) where F = 'person-person' and H = 'Acquaintence' group by D")
I am missing the zero counts which I need
Thanks for any assistance with this
Google sheet w/data
https://docs.google.com/spreadsheets/d/1p0gM4fHWjPf9k40h_GrJjqj8KrxSJDQ-E10LAOgS--Y/edit?usp=sharing

Try this:
=ARRAYFORMULA(QUERY({Sheet1!D2:D,N(Sheet1!F2:F&Sheet1!H2:H="person-personAcquaintence")},"select Col1,SUM(Col2) where Col1<>'' group by Col1"))
SUM()'ing in a query lets you show 0s in a way that counting doesn't really.

Related

Why doesn't LIKE's percentage % match new lines with this query?

Any ideas why LIKE doesn't find a word in multiple lines cell if we are trying to pivot results
Example below:
[We have a table, where 1st line has 2 lines in cell]
if i do query and try to pivot results the line which has 2 lines is ignoring as a result and not posted in pivot table
Formula as follows
=QUERY(B:G;"SELECT D, E, SUM(F), AVG(F) WHERE B IS NOT NULL AND B LIKE '%"&H1&"%' GROUP BY D,E pivot C")
[Result]
https://docs.google.com/spreadsheets/d/19IjdsKJK_sXCn-Syv3Lh4wfR5jmCy2Vp6YqHzVspUCw/edit?usp=sharing
use:
=QUERY(B:G;
"select D,E,sum(F),avg(F)
where B is not null
and B contains '"&H1&"'
group by D,E
pivot C")

Struggling with repeating query error - "Parameter 2 cannot be in group by"

=ARRAYFORMULA(TEXT(QUERY(Event!A:Z,"select B,COUNT(B),C where C>=date'"&A1&"' and C<=date'"&A2&"' group by count(b) order by COUNT(B) desc label B 'Usernames', C 'Date', COUNT(B) 'Frequency'",0),"#"))
The columns I'm referencing are like this (screenshot: https://gyazo.com/c0b0098da3b50f01fc1d40e769495b72 ) and so on. The date values are correct and work when tested, but when I attempt to count the number of responses per username per week-long period, it continues to give me this error:
Unable to parse query string for Function QUERY parameter 2: CANNOT_BE_IN_GROUP_BY: COUNT(b)
I've gotten this working before in the past, but scrapped it all since I decided to do what I was working on in a different way. This is annoying me because it's not working when a couple hours ago it was
try:
=ARRAYFORMULA(TEXT(QUERY(Event!A:Z,
"select B,count(B),C
where C >= date '"&A1&"'
and C <= date '"&A2&"'
group by B,C
order by count(B) desc
label B 'Usernames',C 'Date',count(B)'Frequency'", 0), "#"))
The issue here is that group by has to be followed by an array, a “list” of elements. However, you are currently feeding the group by with a number, which is what count(B) returns. You should replace count(B) with something like B, which returns a list; and since later on you want to use a label in the C column, you should group by B,C, so your code could end up looking like:
=ARRAYFORMULA(TEXT(QUERY(Event!A:Z,"select B,COUNT(B),C where C>=date'"&A1&"' and C<=date'"&A2&"' group by B,C order by B,C desc label B 'Usernames', C 'Date', COUNT(B) 'Frequency'",0),"#"))

QUERY Function: How to count with multiple counting criterias

[Goal]
I'm trying to count the number of tickets per employee in one column that has a status with either "Finished," "Finished (Scope)," or "Routed (Sales)" for a specific week. In another column I also want to count the number of tickets for a specific week without criteria. The data that I'm pulling from to count the tickets has the following column names.
Column A: Date,
Column B: Ticket ID,
Column E: Employee,
Column H: Finished Week
Column K: Week
In the formula, you'll notice that it's referring to cell H1, which the cell contains the current week which is this formula: =TODAY()-MOD(TODAY()-2,7)-1
[Current Formula]
=QUERY('Data'!$A$3:$J,"Select E,
COUNT(B) where D matches 'Finished|Finished \(Scope\)|Routed \(Sales\)'
AND H = "&H1&" GROUP BY E LABEL COUNT(B) 'Total Finished Tickets'",0)
[What it should look like]
I've created a sample spreadsheet that you can refer to.
Link: https://docs.google.com/spreadsheets/d/1MQLgt_SSbUIKv1rEwx-Y21hooxNOOgcUm_j1rFehHdg/edit?usp=sharing
[Issue]
I was able to create a table that counts the number of tickets per employee with the status as "Finished" OR "Finished (Scope)" OR "Routed (Sales)." Which is the "Current Result" table (Link: https://docs.google.com/spreadsheets/d/1MQLgt_SSbUIKv1rEwx-Y21hooxNOOgcUm_j1rFehHdg/edit#gid=0).
However, as I tried to add another count criteria, it gave me errors and I don't understand how to properly make this work. I wanted to look like the table of the title "Ideal Result" in the shared link. Can someone please help?
You can use the pivot clause to get a breakdown by the Status column like this:
=query(
Data!A3:J,
"select E, count(E)
where H = " & E4 & "
group by E
pivot D
label E 'Employee' ",
0
)
The downside is that the grand total must then be calculated separately, but that can be done with a simple sum() formula.
Alternatively, get the totals first, and then do a lookup to get the number of finished tickets, like this:
=query(
Data!A2:J,
"select E, count(D)
where H = " & E4 & "
group by E
label E 'Employee', count(D) 'Total new tickets' ",
0
)
=arrayformula(
iferror(
vlookup(
E12:E,
query(
Data!A2:J,
"select E, count(D)
where H = " & E4 & "
and (D = 'Finished' or D = 'Finished (Scope)')
group by E
label count(D) 'Finished tickets' ",
1
),
2,
false
)
)
)
Note that this serves just to illustrate how to aggregate the data into a report. Your question leaves it unclear as to which status values should be counted for each type of aggregation. No rows with status Routed (Sales) appear in the data, and I cannot see how the expected results you show could be derived from the data.
See your sample spreadsheet.
H1, which the cell contains the current week which is this formula
=TODAY()-MOD(TODAY()-2,7)-1
You may want to try the weeknum() function.
To get two independent counts, you can't use a Where clause because that would exclude cases from both counts, but you could use the fact that Query does not count empty cells something like this:
=ArrayFormula(query({if(regexmatch(D3:D,"Finished$|Finished \(Scope\)$|Routed \(Sales\)$"),true,),E3:E,if(K3:K>=H1,true,)},"select Col2,count(Col3),count(Col1) where Col2 is not null group by Col2 label count(Col1) 'Finished', count(Col3) 'New'",1))

Google Query SELECT statement concatenated with a NESTED IF result

Is it possible to return a Nested IF result from a CELL that will be concatenated to the SELECT statement in the QUERY function?
For example, I am trying to return the result for the following Nested IF function into the Query Function:
https://docs.google.com/spreadsheets/d/15i1E8AZHORRmPlu1VQqFRN1_7-aUyAz-hlYMOUtIlY4/edit?usp=sharing
Appreciate it, if anyone could take a look.
Regards
JVA
its done like this:
=QUERY(TESTDATA!A1:D16, "SELECT A, D, SUM(C) WHERE 1=1 "&
IF(AND(M3="NAME",N3="Customer"), " GROUP BY A, D PIVOT B",
IF(AND(N3 = "Customer"," AND A = '"&M3&"' GROUP BY A, D PIVOT B"),
" AND A = '"&M3&"' GROUP BY A, D PIVOT B",
" AND A = '"&M3&"'
AND D = '"&N3&"' GROUP BY A, D PIVOT B")), 1)
Sometimes, it's easier to FILTER the results before applying QUERY:
=ArrayFormula(QUERY(FILTER(A1:D16, A1:A16=M3, D1:D16=N3), "SELECT Col1, Col4, SUM(Col3) GROUP BY Col1, Col4 PIVOT Col2 LABEL Col1 'Name', Col4 'Customer'",0))
As you can see, this requires using Colx notation instead of letters to indicate columns in the SELECT clause; but this is actually (in my opinion) more versatile, since you don't have to rewrite the QUERY if you ever insert columns before the existing source data.
You'll also notice that I needed to LABEL the first two columns, since FILTER will have FILTERed out the headers. (In fact, for this reason, the ranges in the formula could just as easily have begun with row 2, e.g., A2:A16, etc.)
Finally, at least in your sample spreadsheet, you didn't need the sheet name to reference the source ranges, since the result is in the same sheet.

Sum column in google excel using 2 conditions

I have a table with 4 columns in a google spreadsheet [timestamp, item, amount, category] and would like to get all rows that belong to a specific month and a specific category and sum the amount column.
So far I have tried
=QUERY(Sheet1!A:D,"SELECT SUM(C) WHERE MONTH(A) = 1 AND D = 'some text' GROUP BY A",1)
and
=IF(AND(EQ(Sheet1!D2:D,B17),MONTH(Sheet1!A2:A)=1),SUM(Sheet1!C2:C))
Where B17 is a text that I want a comparison to be made.
In Google spreadsheets, try
=sumproduct(Sheet1!D2:D=B17,MONTH(Sheet1!A2:A)=1, Sheet1!C2:C)
and see if that works?
If you'd prefer query, you can try
=QUERY(Sheet1!A:D,"SELECT SUM(C) WHERE MONTH(A) = 0 AND D = '"&B17&"' label SUM(C) ''",1)
Note that MONTH() in query is zero-indexed: so January would be month 0.
You can change this one:
=IF(AND(EQ(Sheet1!D2:D,B17),MONTH(Sheet1!A2:A)=1),SUM(Sheet1!C2:C))
with
=sum(filter(Sheet1!C2:C,(Sheet1!D2:D=B17)*(MONTH(Sheet1!A2:A)=1)))

Resources