Query groupby and count separated values - google-sheets

I would like to create a table in google sheets, listing some elements (in column) and at the same time counting other elements, currently grouped in a string separated by semicolons.
The example is the following:
initial dataset
desired outcome
I have tried some queries but for the moment I have been able only to count the semicolon-separated elements, without being able to group them for "markets".
Any help is greatly appreciated!
Thank you in advance!

try:
=INDEX(QUERY(IFERROR(SPLIT(FLATTEN(source!A2:A&"×"&SPLIT(source!B2:B, ";")), "×")),
"select Col1,count(Col1) where Col2 is not null group by Col1 pivot Col2"))

Related

Efficient way to collate/aggregate specific data in Google Sheets

I'm looking for an efficient way to gather and aggregate some date in Google Sheets. I've been looking at the query function, pivot tables, and Index + Match formulas, but so far I've not found a way that brings me to the result I'm looking for. I have a set of data which looks more or less as follows.
The fields with an X represent irrelevant data which I don't want to show up in my end result. They only serve to illustrate that there are columns of data that I don't want in between the columns of data that I do want. The data in those columns is of varying types and of varying values per type, they are not actually fields with an "X" in it. Only the fields with numbers are of interest along with the related names at the top and left of those. The intent is to create a list that looks more or less like this.
I've highlighted those yellow fields because that data has been aggregated. For example, in the original file field D3 shows a relation between Laura and Pete with the number 1, and field L3 also shows a relation between Laura and Pete, so the number in that field is to be added to the number in the other field resulting in an aggregated total of 2 for that particular combination.
I would really appreciate any suggestions that can help me get to an elegant and efficient solution for this. The only solutions I can come up with would involve multiple "in-between" sheets and there just has to be a better way.
UPDATE:
Solved by applying the solution in player0's answer. I just had to switch around the order of Col1 and Col2 in the formula to get the table sorted the way I needed it. Formula looks like below now. Many thanks to both player0 and Erik Tyler for their efforts.
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"select Col2,Col1,sum(Col3)
where Col2 is not null
and Col3 is not null
group by Col2,Col1
label sum(Col3)''", ))
try:
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"where Col3 is not null and Col2 is not null", ))
update:
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"select Col1,Col2,sum(Col3)
where Col3 is not null
and Col2 is not null
group by Col1,Col2
label sum(Col3)''", ))
Given your current data set (which only appears to extend to Col N), place the following somewhere to the right of Col N:
=ArrayFormula(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(QUERY(FLATTEN(FILTER(IF(NOT(ISNUMBER(D2:N)),,D1:N1&"~ "&A2:A&"|"&D2:N),A2:A<>"")),"Select * WHERE Col1 Is Not Null"),"|"),"Select Col1, SUM(Col2) GROUP BY Col1 LABEL SUM(Col2) ''")&"~ "),,2)),"~ ",0,1))
It would be better if this were placed in a different sheet from the original data. Supposing that your original data sheet is named Sheet1, place the following version of the above formula into a new sheet:
=ArrayFormula(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(QUERY(FLATTEN(FILTER(IF(NOT(ISNUMBER(INDIRECT("Sheet1!D2:"&ROWS(Sheet1!A:A)))),,Sheet1!D1:1&"~ "&Sheet1!A2:A&"|"&INDIRECT("Sheet1!D2:"&ROWS(Sheet1!A2:A))),Sheet1!A2:A<>"")),"Select * WHERE Col1 Is Not Null"),"|"),"Select Col1, SUM(Col2) GROUP BY Col1 LABEL SUM(Col2) ''")&"~ "),,2)),"~ ",0,1))
This separate-sheet approach and formula allows for the original data to extend indefinitely past Col N.

How can I separate a column into multiple columns based on values?

I have searched on a lot of pages but I cannot find a solution to my problem except in reverse order. I have simplified what I do, but I have a query that comes looking for information in my data sheet. Here there are 3 columns, the date, the amount and the source.
I would like, with a query function, to be able to make different columns which counts the information of column C based on the values of its cells per month, like this
I'm okay with the start of the formula
=QUERY(A2:C,"select month(A)+1, sum(B), count(C) where A is not null group by month(A)+1")
But as soon as I try a little different things by putting 2 query together in an arrayformula, obviously the row count doesn't match as some minus are 0 for some sources.
Do you have a solution for what I'm trying to do? Thank you in advance :)
Solution:
It's not possible in Google Query Language to have a single query statement that has one result grouped by one column and another result grouped by another.
The first two columns can be like this:
=QUERY(A2:C,"select month(A)+1, sum(B) where A is not null group by month(A)+1 label month(A)+1 'Month', sum(B) 'Amount'")
To create the column labels for the succeeding columns, use in the first row, in my example, I1:
=TRANSPOSE(UNIQUE(C2:C))
Then from cell I2, enter this:
=COUNTIFS(arrayformula(month($A$2:$A)),$G2,$C$2:$C,I$1)
Then drag horizontally and vertically to apply to the entire table.
Results:
try:
=INDEX({
QUERY({MONTH(A2:A), B2:C},
"select Col1,sum(Col2) where Col2 is not null group by Col1 label Col1'month',sum(Col2)'amount'"),
QUERY({MONTH(A2:A), B2:C, C2:C},
"select count(Col3) where Col2 is not null group by Col1 pivot Col4")})

How to combine all the columns in into one and count them respectively in google sheet?

Data
Hi everyone,
I have 3 columns of data for the age of people in a city and also 3 columns of the count for the ages. I want to combine all the 6 columns into 2 columns which means there will be only one column of age data and one column for number of count. I try to use query function in googlesheet but not sure how to use it. Please give me some advice on this if there is other method that can achieve the same result. Thank you.
Try
=query({G8:H; J9:K; M9:N}, "Select Col1, sum(Col2) where Col1 is not null group by Col1 label sum(Col2) 'Count of people'", 1)
and see if that works?
and see if that works?

Use Query to display two columns and group rows based on one column's count?

I'm using an array to bring 2 columns of data together from 3 sheets.
There are duplicates in the second column, and I would like to group those duplicates together and display both Col1 and Col2, ordered alphabetically by Col1.
This is the formula I have right now:
=QUERY({'Sheet1!'A:B;'Sheet2!'A:B;'Sheet3!'A:B}, "Select Col1, count(Col2) where Col1 is not null group by Col1",1)
Which only displays Col1.
I've tried nesting QUERY, but I can't get it to work and can't find any direction anywhere online.
Here's an example sheet I made to show what I'm trying to do:
https://docs.google.com/spreadsheets/d/1_x0mXZC0ZjsHDCd6I0dDf9OI19lrzEcPYqfcMxuK74Y/edit?usp=sharing
In the example if an employee is listed twice the name may change but the email is consistent. I'm hoping to group by the email addresses and return only one name (it doesn't really matter which name).
I'm not sure if this is possible without formulas in more than one cell. Thank you either way!
#confuseddesk, try this array formula:
=ArrayFormula(QUERY({VLOOKUP(UNIQUE({Sheet2!B2:B;Sheet3!B2:B;Sheet4!B2:B}),{Sheet2!B2:B,Sheet2!A2:A;Sheet3!B2:B,Sheet3!A2:A;Sheet4!B2:B,Sheet4!A2:A},2,FALSE),UNIQUE({Sheet2!B2:B;Sheet3!B2:B;Sheet4!B2:B})},"Select * Where Col2 Is Not Null"))

How can I COUNT instances of Value next to another Value

As a game designer, I am working on Google Sheets to make collections of animals with specific outfits. I need to make sure I use all combinations are evenly used.
I could just use for-loop, but scripting here would add unnecessary complexity for the whole process and it would be nice to just do it in the Sheets like where I make the data needed for our game. I tried different combinations of MATCH, INDEX and COUNTIFS-functions. Dabbled a bit with Pivot Tables and QUERY, but my brain is stuck and I could use some help.
My data looks something like this:
Collection1:[Animal1][Outfit1][Animal1][Outfit7][...]
Collection2:[Animal6][Outfit3][Animal2][Outfit18][...]
[...]
So rows for collections with animal FOLLOWING the outfit it has in the next cell.
I'd like to have a sheet with Rows of animals with Columns for Outfits and Counts for those Outfits for that Animal.
I can't brain this and any hints would help. Sheets can be so unintuitive for me.
cell J1:
=QUERY({B:C;D:E;F:G},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1) 'animal count'")
cell J6:
=QUERY({B:C;D:E;F:G},
"select Col2,count(Col2)
where Col1 is not null
group by Col2
label count(Col2) 'outfit count'")
cell J12:
=QUERY({B:C;D:E;F:G},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
pivot Col2")

Resources