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
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),"#"))
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.
Is it possible to use the WHERE clause in combination with the PIVOT clause in the Google query language?
I started with a pivot table using the following query:
select B, sum(C) group by B pivot A
Now I would like to alter the pivot table so that only the columns of the years 2018 and after get displayed. I tried the following query:
select B, sum(C) group by B pivot A where A>=2018
Why do I get an error here and is there a way to achieve the goal of filtering a pivot table according to specific criteria?
You need to replace the where clause.
Please use the following formula:
=QUERY(A3:C9,"select B, sum(C) where A>=2018 group by B pivot A")
The order of the language clauses in a query are:
select, where, group by, pivot, order by, limit, offset, label, format, options
In Google Sheets, I need to extract the unique values in column B, for every unique value in column A, such that I can construct the following table:
Possible?
=Unique(A:B)
should be enough to return non-duplicate rows
https://support.google.com/docs/answer/3093198?hl=en
You can also use Sortn:
=sortn(A:B,9E+99,2,1,true,2,true)
=QUERY(QUERY(A1:B,
"select A, B, count(A)
group by A, B", 1),
"select Col1, Col2
where Col1 is not null", 1)
You can also install my add-on called Flookup and use the function below:
ULIST(colArray, [indexNum], [threshold])
colArray The range from which you want to return unique values.
indexNum The column index to analyse for unique values.
threshold The percentage similarity between the non-unique values. This helps eliminate potential duplicates.
So for your case you would simply type:
=ULIST(A1:B6, 2)
Find out more details at the official website.
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.