Remove multiple column labels from Google Sheets Query - google-sheets

Trying to remove the column labels from a query and I've tried the following two ways but still getting error. What's the best way to do this?
=QUERY('Form Responses 1'!2:115, "SELECT C, label(AVG(D),''), label(AVG(E),''), label(AVG(F),''), label(AVG(G),''), label(AVG(H),''), label(AVG(I),''), label(AVG(J),''), label(AVG(K),''), label(AVG(L),'') WHERE C IS NOT NULL GROUP BY C ORDER BY C", 0)
=QUERY('Form Responses 1'!2:115,"SELECT C,AVG(D),AVG(E),AVG(F),AVG(G),AVG(H),AVG(I),AVG(J),AVG(K),AVG(L) WHERE C IS NOT NULL GROUP BY C ORDER BY C label(D),label(E),label(F),label(G),label(H),label(I),label(J),label(K),label(L) ",0)

You just have to write label once, followed by the columns you want to label and the label you want to give them.
=QUERY(data,"... label A '', B '', C''...")

try shorter:
=QUERY(QUERY('Form Responses 1'!2:115,
"SELECT C,AVG(D),AVG(E),AVG(F),AVG(G),AVG(H),AVG(I),AVG(J),AVG(K),AVG(L)
WHERE C IS NOT NULL
GROUP BY C
ORDER BY C"), "offset 1", )

Related

How do I return the cell address of every instance of a key in a 1D array?

I’m trying to find every instance of a name in a list of names, and compile their locations in a list. I already know TEXTJOIN can do this with comma delineation, but I can’t figure out how to implement it.
I’ve tried using FIND, SEARCH, and MATCH in various ways, but can’t get it to work.
Here’s a mockup, where the filled in section is the goal.
This is the formula I’m trying to use:
=INDEX(IFNA(VLOOKUP(E2:E, QUERY(L:O, "select L,sum(O) where L is not null group by L label sum(O)''"), 2))
Update:
And here’s an actual graph photo.
For reference, Base Stakes and Results work perfectly, the only issue being that they are referring to the ELO column(the one for which the desired result is filled in), which is broken. How should I translate the working formula to function without detecting circular logic?
try:
=QUERY(D:F; "select D,sum(F) where D is not null group by D label sum(F)''")
if you insist on injection use:
=INDEX(IFNA(VLOOKUP(A2:A; QUERY(D:F;
"select D,sum(F) where D is not null group by D label sum(F)''"); 2; )))
update:
use in F2:
=INDEX(IFNA(VLOOKUP(E2:E, QUERY({L:L, O:O},
"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''"), 2, )))
Formula in Table 1 $A$2
=MAP(SORT(UNIQUE(D2:D)),
LAMBDA(n, IF(n<>"",
{n,SUM(INDEX(FILTER(D:F,D:D=n),,3))},"")))

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),"#"))

How can I avoid having to put 0s into the NULL fields to get a correct query calculation in Google Sheets

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))

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.

Translating each result row of a Google Sheets QUERY operation via VLOOKUP

I have the multi-column results of a QUERY in Google Sheets, where I want to translate the strings in one column based on a lookup table in another named range. I can accomplish this indirectly with an additional VLOOKUP call, but I'd like to do it all in one go, with no intermediary steps. See this sheet for an example of my format.
The initial query I make looks something like this:
=QUERY(votes, "SELECT B, SUM(C) GROUP BY B LABEL B 'Option', SUM(C) 'Votes'")
I can then translate each row with something like this in a new column...
=VLOOKUP(A2, options, 2, 0)
... and then just pick out the columns I need:
={C2:C4,B2:B4}
How can I combine all this? I think I need to do this with an ARRAYFORMULA, but this doesn't seem to be it:
=ARRAYFORMULA(VLOOKUP(options, QUERY(votes, "SELECT B, SUM(C) GROUP BY B LABEL B 'Option', SUM(C) 'Votes'"), 2, FALSE))
=ARRAYFORMULA({VLOOKUP(INDEX(
QUERY(votes, "select B,sum(C) group by B LABEL sum(C)''"),,1), options, 2, 0),
QUERY(votes, "select sum(C) group by B LABEL sum(C)''")})
=ARRAYFORMULA(QUERY({IFERROR(VLOOKUP(Votes!B2:B, options, 2, 0)), Votes!C2:C},
"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''", 0))

Resources