Related
=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),"#"))
For each of the email id, I want to get latest 10 records by timestamp. How do I get the results with arrayformula? Query function is not important as long as I can still achieve this with arrayformula. Here is the sample data:
https://docs.google.com/spreadsheets/d/1YAHA02VM-5MXzVKhkxu_eODPKObpoz441mGX8lOFu5M/edit?usp=sharing
Try this on another sheet, row 1:
=arrayformula(query({query({Sheet1!$A:$C},"order by Col1 desc,Col2",1),{"Dupe position";countifs(query({Sheet1!$A2:$C},"select Col2 order by Col1 desc,Col2",0),query({Sheet1!$A2:$C},"select Col2 order by Col1 desc,Col2",0),row(Sheet1!$A2:$C),"<="&row(Sheet1!$A2:$C))}},"select Col1,Col2,Col3 where Col1 is not null and Col4 <= 10 order by Col1",1))
You can adjust the number of records found by adjusting Col4 <= 10, and also the final sort by altering order by Col1 at the end of the formula.
Explanation
This gets the data from Sheet1, sorts it by date desc then email asc:
query({Sheet1!$A:$C},"order by Col1 desc,Col2",1)
Then to the side of this data, a COUNTIFS() is used to get the number each time an email appears in the list above (since it's sorted desc, 1 represents the most recent instance).
countifs(<EmailColumnData>,<EmailColumnData>,row(<EmailColumn>),"<="&row(<EmailColumn>))
In place of <EmailColumnData> in the COUNTIF() is:
query({Sheet1!$A2:$C},"select Col2 order by Col1 desc,Col2",0)
In place of <EmailColumn> above, we only want the row number so we don't need the actual data. We can use:
Sheet1!$A2:$C
Various {} work as arrays to bring the data together.
Eg., {a,b,c;d,e,f} would result in three columns, with a, b, c in row 1 and d, e, f in row 2. , is a new column, ; is a return for a new row.
A final query around everything gets the 3 columns we need, where the count number in col 4 is <=10, then sorts the output by Col1 (date asc).
On second thoughts, maybe this is bit cheeky, but this might do it ( taken from conditional rank idea )
=ArrayFormula(filter(A2:C,countifs(A2:A,">="&A2:A,B2:B,B2:B)<=10,A2:A<>""))
EDIT
The above assumes (because the data is time-stamped) dups shouldn't occur. If they do and the data is pre-sorted, you can use row number as a proxy for time stamp as suggested by #Aresvik.
Alternatively, you could count separately
(a) only rows with a later timestamp
plus
(b) rows with the same time stamp but with earlier (or identical) row number
=ArrayFormula(filter(A2:C,countifs(A2:A,">"&A2:A,B2:B,B2:B)+countifs(A2:A,"="&A2:A,B2:B,B2:B,row(A2:A),"<="&row(A2:A))<=10,A2:A<>""))
I have added a new sheet ("Erik Help") with the following formula in A1:
=ArrayFormula({"Submitted Time","Email","Score";SORT(SPLIT(FLATTEN(QUERY(SORT(TRANSPOSE(SPLIT(TRANSPOSE(QUERY(IF(Sheet1!B2:B=TRANSPOSE(UNIQUE(FILTER(Sheet1!B2:B,Sheet1!B2:B<>""))),Sheet1!A2:A&"|"&Sheet1!B2:B&"|"&Sheet1!C2:C,),,COUNTA(Sheet1!A2:A)))," ",0,1)),SEQUENCE(MAX(COUNTIF(Sheet1!B2:B,Sheet1!B2:B))),0),"LIMIT 10")),"|",1,0),1,0)})
The number of records is set after LIMIT.
The order is set by the final two numbers: 1,0 (meaning "sort by column 1 in reverse order," which, as currently set, is sorting in reverse order by date/time).
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.
I try to convert one table to another.
What is wrong with my syntax?
=QUERY(B21:D24, "select A,C where A is not null group by A pivot B",1)
Unable to parse query string for Function QUERY parameter 2: CANNOT_GROUP_WITHOUT_AGG
Issue:
CANNOT_GROUP_WITHOUT_AGG
means The query cannot use group columns without a AGGregate function.
select A,C where A is not null group by A pivot B
You're asking it to group by A. If the query groups by Column A, What to do with Column C? Column C cannot be displayed side by side. Although your sample doesn't have duplicates in B, think what will happen, if Column B is a,b,a instead of a,b,c. Column C needs to be aggregated. Said otherwise, For each group in A, What do you want to do with C(if there are multiple items in C for the same group in A)?
Solution:
Provide a Aggregate function for column C like, sum, count,max etc. For each group in A, The corresponding column C will be summed or counted or a max value of C in that group will be taken.
Snippet:
=QUERY(B21:D24, "select A, sum(C) where A is not null group by A pivot B",1)
Please use the following formula to get the 0's as well.
It is the IF function that will give you the 0's in your pivot.
=ArrayFormula(IF(QUERY(M1:O22, "select M, sum(O) where M is not null group by M pivot N")="",0,
QUERY(M1:O22, "select M, sum(O) where M is not null group by M pivot N")))
As for the syntax, it is mentioned that
If you use a pivot clause, then every column listed in the select clause must either be listed in the group by clause, or be wrapped by an aggregation function
Functions used:
ArrayFormula
IF
QUERY
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.