Google Sheet Query - Group / concatenate multiple rows - google-sheets

I'm running a QUERY with a SUM and GROUP BY, but I'd like to aggregate multiple distinct values from the rows into a single row and column. I'm looking to concatenate all those values together.
Current Table:
Person
Widget
Count
Bill
Red
12
Bill
Blue
9
Sarah
Yellow
4
Bill
Yellow
1
Sarah
Orange
10
Expected Table:
Person
Widget
Count
Bill
Red, Blue, Yellow
22
Sarah
Yellow, Orange
14

You can use the filter and join functions to help:
To get a unique list of names:
=UNIQUE(A3:A)
To join the widgets:
=join(",",filter(B:B,A:A=E3))
To sum the values:
=sum(filter(C:C,A:A=E3))

Related

Google spreadsheet query multiple columns with a count column

I have a table like this:
Animal ID eye color
-------------------------
Rabbit 90 blue
Rabbit 90 brown
Cat 91 blue
Cat 91 green
Squirrel 92 brown
What I have is
=QUERY(A2:C6;"select A,count(A) group by A")
which returns two columns. Now I want to add the ID column to it.
The desired outcome basically is:
Animal ID Count
----------------------
Cat 91 2
Rabbit 90 2
Squirrel 92 1
I realize I can do a
=QUERY(A2:C6;"select A,B ")
But can't combine that with a count on A nor a count on B.
Is there some not too complicated way to do that?
If the same animal has the same ID, you can group by both:
=QUERY(A2:C6,"select A,B,count(A) group by A,B")
or choose max or min or avg value of ID per group:
=QUERY(A2:C6,"select A,min(B),count(A) group by A")

Combine data across multiple sheets while keeping columns but ignoring blanks

I have three sheets in a workbook where people enter data (text values) in different columns, with different row lengths.
For example:
Sheet 1
Group 1
Group 2
Group 3
Apple
Apple
Apple
Orange
Orange
Banana
Banana
Peach
Pear
Sheet 2
Group 1
Group 2
Group 3
Onion
Onion
Onion
Tomato
Tomato
Leek
Leek
Garlic
Potato
I'm looking to combine this data into a single sheet, displayed as such:
Group 1
Group 2
Group 3
Apple
Apple
Apple
Orange
Orange
Banana
Onion
Banana
Peach
Tomato
Pear
Onion
Leek
Onion
Leek
Tomato
Garlic
Potato
I've tried this formula:
=QUERY({Sheet1!A3:G;Sheet2!A3:G;Sheet3!A3:G},"select * where Col1<>'' or Col2<>'' or Col3<>''",0)
But it adds in blanks for as many as the longest column is on each sheet, like so:
Group 1
Apple
Orange
Onion
Tomato
Leek
Is there anything I can change to have it just list the items per column in the order queried, skipping blank cells as opposed to rows? I found lots of guidance in other questions about consolidating into a single column, but I want to keep the columns separated and consolidate rows instead.
You have to use multiple queries, one for each column. Even after that, we can't stack the arrays horizontally using {,} because arrays are jagged(jagged-arrays). We can use SORT each column to give a appearance of removing blanks, as blanks are sorted to the bottom. Then stack them back. To automate this process, we can use REDUCE to loop:
=REDUCE(
,
SEQUENCE(COLUMNS(A:C)),
LAMBDA(a,c,
LAMBDA(
s,
IF(a="",s,{a,s})
)(SORT(INDEX({Sheet1!A2:C;Sheet2!A2:C},0,c)))
)
)
This removes the original order though
use:
=INDEX(TRANSPOSE(SPLIT(FLATTEN(QUERY(
IF({Sheet1!A2:C; Sheet2!A2:C}="",,"​"&{Sheet1!A2:C; Sheet2!A2:C}),,9^9)), "​")))

How to do a full outer join?

I am trying to do the full join for the data below in two different sheets.
Sheet 9:
Product ID
Name
Quantity
1
addi
55
2
fadi
66
3
sadi
33
Sheet10
Product ID
Variants
Model
1
xyz
2000
2
differ
2001
3
saddd
336
4
fsdfe
2005
Desired output sheet :
Product ID
Name
Quantity
Variants
Model
1
addi
55
xyz
2000
2
fadi
66
differ
2001
3
sadi
33
saddd
336
4
fsdfe
2005
Please also share if we have more columns to join like in sheet 1 and sheet 2 has two more columns like Year, product label etc then what should I change in your proposed solution
I am using this formula but its not returning the desired result
=ARRAYFORMULA({QUERY(SORT(UNIQUE({Sheet9!A1:D; Sheet10!A1:D})), "where Col1 is not null"),IFERROR(VLOOKUP(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SORT(UNIQUE({Sheet9!A1:D; Sheet10!A1:D})), "where Col1 is not null")),,999^99)), TRANSPOSE(QUERY(TRANSPOSE(Sheet9!A1:D),,999^99)), Sheet9!C1:C}, 2, 0),""),IFERROR(VLOOKUP(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SORT(UNIQUE({Sheet9!A1:D; Sheet10!A1:D})), "where Col1 is not null")),,999^99)), {TRANSPOSE(QUERY(TRANSPOSE(Sheet10!A1:D),,999^99)), Sheet10!C1:C}, 2, 0),"")}})
EDITED to consider dynamic row matching.
See this spreadsheet to illustration, but overall there's a question of your setup, but I would break your problem into two steps.
Get distinct list of ID's
You can get that with this formula:
=unique(transpose(split(textjoin(",",true,
iferror(INdex(Sheet2!$A$2:$Z,0,MATCH(A1,Sheet2!1:1,0)),""),
iferror(INdex(Sheet1!$A$2:$Z,0,MATCH(A1,Sheet1!1:1,0)),"")),",")))
Rest of Headers
Then for each header, will they each always only be in 1 exclusively or 2 (not both)? Assuming so, this should work for each additional column. If two values ever exist in the two sheets, will join them in the same column.
=filter(
iferror(VLOOKUP($A$2:$A,Sheet1!$A:$Z,match(E$1,Sheet1!1:1,0),false),"")
&iferror(VLOOKUP($A$2:$A,Sheet2!$A:$Z,match(E$1,Sheet2!1:1,0),false),"")
,$A$2:$A<>"")
There's probably a way to use the join function to do this more elegantly (if someone posts an answer showing me I'll upvote).

How to I count the number of times text in column A appears anywhere in column B

I have 2 columns of data and want to count the number of times text in column A cells appears in column B cells.
Here's an example
column a:
apple,
pear,
peach,
plum,
grape,
column b:
apple pie,
cheeseburger,
peach jam,
green salad,
grape jelly
The answer here is 3 -- words from column A appear 3 times in column B (apple pie, peach jam, grape jelly).
How do I wrote a formula to execute this count?
Use wildcard * with MATCH to get matching row
COUNT the number of matched rows
=ARRAYFORMULA(COUNT(MATCH("*"&A2:A5&"*",B2:B5,0)))
for infinity try:
=ARRAYFORMULA(COUNT(MATCH("*"&A:A&"*", B:B, 0))-COUNTBLANK(A:A))
or shorter:
=ARRAYFORMULA(SUM(N(REGEXMATCH(B:B, TEXTJOIN("|", 1, A:A)))))

How to do a sum of a column if a particular cell of each row matches a particular value?

I'm trying to make a budget sheet for a trip.
I have 2 sheets in a Google Spreadsheet. One contains the steps of the trip. The other contains a dashboard with the sum for each of these 5 groups of steps: "food", "activities", "shopping", "hotels" and "vehicle".
My first sheet is like that (with 4 columns - the 3rd is the price):
1. Go to museum 45 Activity
2. Playing cards 5 Activity
3. Sleeping at Bohaha Hotel 123 Hotel
4. Take breakfast 10 Food
In the other sheet, I want to make different sums conditioned by the value of the 4th column, and using the values of the 3rd column.
The result of the second sheet in this example would be:
Food 10
Activities 50
Shopping 0
Hotels 123
Vehicle 0
How can I do that?
I can use a function if necessary.
Simple SUMIFS:
=SUMIFS(Sheet1!C1:C99, Sheet1!D1:D99, A1)
Assuming description in second sheet matches description in 4th column (the formula is not going to do the stemming for you to convert Activity to Activities - you can use 1 column for exact match string and 1 column as reporting label...)
I found the perfect formula :)
=SUM(FILTER('Sheet 1'!H5:H; 'Sheet 1'!G5:G="Food"))

Resources