Google Query SELECT statement concatenated with a NESTED IF result - google-sheets

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.

Related

How to get unique values in a column, including cells with multiple values seperated by commas, in Google Sheet?

I have a column in a Google Sheet, which in some cases, includes multiple values separated by commas — like this:
Value
A example
B example
C example
D example
A example, E example
A example, F example
G example, D example, C example
I would like to count all occurrences of the unique values in this column, so the count should look like:
Unique value
Occurrences
A example
3
B example
1
C example
2
D example
2
E example
1
F example
1
G example
1
Currently, however, when I use =UNIQUE(A2:A), the result gives this:
Unique value
Occurrences
A example
1
B example
1
C example
1
D example
1
A example, E example
1
A example, F example
1
G example, D example, C example
1
Is there a way I can count all of the instances of letters, whether they appear in individually in a cell or appear alongside other letters in a cell (comma-seperated)?
(This looks like a useful answer in Python, but I'm trying to do this in Google Sheets)
try:
Formula in C1:
=INDEX(QUERY(IFERROR(FLATTEN(SPLIT(A1:A,", ")),""),"Select Col1, count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"))
Or, as per the comments, split on the combination instead:
=INDEX(QUERY(IFERROR(FLATTEN(SPLIT(A1:A,", ",0)),""),"Select Col1, count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"))
2nd EDIT: To order descending by count use:
=INDEX(QUERY(IFERROR(FLATTEN(SPLIT(A1:A,", ",0)),""),"Select Col1, count(Col1) where Col1 is not null group by Col1 Order By count(Col1) desc label count(Col1) ''"))
Assuming data in A1:A7:
In C1:
=SORT(UNIQUE(FLATTEN(ARRAYFORMULA(SPLIT(A1:A7,", ")))))
In D1:
=ARRAYFORMULA(MMULT(0+ISNUMBER(SEARCH(", "&ColumnCSpilledRange&", ",", "&TRANSPOSE(A1:A7)&", ")),ROW(A1:A7)^0))
Replace ColumnCSpilledRange appropriately.

Need to know with formula in Google sheet

I have date in column A, Name in column B and Product sale data in column C. Now I want formula which gives me in return in another table, If that person has sold something on a particular date that sale data entry is shown
Do it with a pivot table so that you will have the answer without any formula.
Try query() with a pivot clause, like this:
=query(A1:C, "select A, B, count(B) where A is not null group by A, B pivot C", 1)
May be it can help too
=Query('Sales '!A1:C, "Select Col1, Col2, Col3 Where Col3 is not null",0)
'Sales ' is data source worksheet
use ,1 if you don't want to include the same headers of of your data source worksheet

How to sort a pivot table obtained with the query function in Google Sheet

I know that it is not possible to "order by" after a pivot in query function, that is why I apply a nested query.
=QUERY(
QUERY(Dataset,"select B,count(B) where A>date'2019-10-01' group by B pivot year(A),month(A)",1),
"Select * order by Col2 Desc ",1)
However the result is not sorted by count(B) (Col2 of the inner query) as a total.
It is sorted only by the Col2 generated because of the pivot. The final table is sorted incorrectly (Option 10 should be in fifth place and not at the bottom).
Example of the result
Any workaround to solve this?
Instead of a second query use the SORT function outside the first query.
Try something like:
=SORT(QUERY(Dataset,"select B,count(B) where A>date'2019-10-01' group by B pivot year(A),month(A)",1),2,0)

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

sum same condition in multiple columns to get a total

I've been trying to sum the same condition in different columns (and different sheets) to get a total.
For example, I have a list of items in 3 different columns with 3 different sums.
See here:
I solved this with an SQL query within Google sheets but I'm looking for a more simple/ elegant solution.
This is what I have:
=UNIQUE({QUERY(A2:I8; "SELECT SUM(C) where B LIKE 'bananas'");
QUERY(A2:I8; "SELECT SUM(F) where E LIKE 'bananas'");
QUERY(A2:I8; "SELECT SUM(I) where H LIKE 'bananas'")})
And the result looks like this:
Does someone know a shorter version that fits in one cell?
=SUM(QUERY({A:C;D:F;G:I}; "where Col2 = 'bananas'"; 0))
=QUERY({A:C;D:F;G:I}; "select sum(Col3) where Col2 contains 'banana' label sum(Col3)''")
=SUM(FILTER({C:C;F:F;I:I}; {B:B;E:E;H:H}="bananas"))

Resources