Google Sheets Query - merge column data to rows - google-sheets

I want to merge data (name + value/s) in different rows based on another column (name). See example below:
name | value
A | ab
A | ac
A | ad
B | bc
B | bd
I want the output to be like this (in rows and concatenated in one cell):
A ab ac ad
B bc bd
Is this possible with Google sheets query?

I have added a new sheet ("Erik Help"). To achieve the exact results you wanted:
=ArrayFormula({"Result";IF(A2:A="",,IF(COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))>1,,VLOOKUP(A2:A&"*",REGEXREPLACE(TRIM(SPLIT(FLATTEN(QUERY(QUERY({A2:B}, "Select MAX(Col2) where Col2 IS NOT NULL GROUP BY Col2 PIVOT Col1"),, 9^9)),"~")),"[,\s]+$",""),1,FALSE)))})
This is a more complex formula than I can typically share on this free forum, so I will need to leave the understanding of the formula to you and others who may be interested.
I also left a second formula, which shows only the aggregated list, as a "bonus" for you:
=ArrayFormula({"Aggregated List";REGEXREPLACE(TRIM(SPLIT(FLATTEN(QUERY(QUERY({A2:B}, "Select MAX(Col2) where Col2 IS NOT NULL GROUP BY Col2 PIVOT Col1"),, 9^9)),"~")),"[,\s]+$","")})

Related

How to query count of unique features in google sheets?

I have this table:
Name | Age
Ann | adult
Ann | adult
Andrew | adult
Mike | adult
Ann | teenager
John | teenager
John | teenager
I want this output:
Age | count Name (distinct Names)
adult | 3
teenager | 2
Unfortunately, I can't go further then this formula:
=QUERY(table; "select B, count(A) group by B"; 1)
where the 'table' is the named range with input data. And it gives me this:
Age | count Name
adult | 4
teenager | 3
I need something like:
=QUERY(table; "select B, count(unique(A)) group by A"; 1)
which obviously doesn't work.
So, how can I achieve my target output with querying?
I know, I can do that with pivot tables with countunique function, but I want to go without pivot tables.
One option could be QUERY + UNIQUE:
=QUERY(UNIQUE(A2:B),"SELECT Col2, COUNT(Col1) WHERE Col2 IS NOT NULL GROUP BY Col2")
You can also make use of this formula:
=ARRAYFORMULA(QUERY(UNIQUE({B:B, B:B & A:A, A:A}), "SELECT Col1, COUNT(Col1) WHERE Col1 IS NOT NULL GROUP BY Col1 ORDER BY COUNT(Col1) DESC LABEL COUNT(Col1)'count Name'", 1))
After
Explanation
The formula makes use of the following functions:
ARRAYFORMULA
UNIQUE
QUERY
In order to find the unique values, the UNIQUE is used for the range needed for the query (A:B) such that the sorting and counting is done on this range. The LABEL is used as well in order to set the header name for the resulted column.
Reference
ARRAYFORMULA;
UNIQUE;
QUERY.

How to append 3 tables with different number of columns in google sheet?

Hi everyone,
I have 3 tables with different number of columns where some columns are actually repeated in these 3 tables (as shown in the screenshot above).
Table 1: Col A to Col D
Table 2: Col F to Col H
Table 3: Col J to Col L
I want to merge these 3 tables together to reduce the number of columns in my google sheet. The screenshot below is how I process these 3 tables before merging.
I'm using QUERY to create extra columns for each table so that the number of columns and the position are aligned between 3 tables. After that, I use QUERY again to append the 3 processed tables as shown in the screenshot below:
However, this method is very tedious when I have 10 tables or more. Is there any other easier ways or tricks to use so that I can achieve the same expected output as shown in the 3rd screenshot?
This is my sheet:
https://docs.google.com/spreadsheets/d/1H1gJAhp1RVax2fy8D-uEtFxdjb-zAHutkPFv5WZT_TY/edit#gid=0
Any help will be greatly appreciated!
You would need a really complicated formula to get the desired output which is a combination of multiple formula's
I added a new tab in you Google Sheet called "Solution" with this formula included
=QUERY(ARRAYFORMULA({
IFERROR( ArrayFormula(SPLIT(FLATTEN(Sheet1!B3:D3&"~"&Sheet1!A4:A&"~"&Sheet1!B4:D),"~")),"");
IFERROR( ArrayFormula(SPLIT(FLATTEN(Sheet1!G3:H3&"~"&Sheet1!F4:F&"~"&Sheet1!G4:H),"~")),"");
IFERROR( ArrayFormula(SPLIT(FLATTEN(Sheet1!K3:L3&"~"&Sheet1!J4:J&"~"&Sheet1!K4:L),"~")),"")}),
"SELECT Col2, SUM(Col3) WHERE Col2 is not null GROUP BY Col2 PIVOT Col1 LABEL Col2 'Student Name' ")
Steps:
Unpivot each table =IFERROR( ArrayFormula(SPLIT(FLATTEN(Sheet1!B3:D3&"~"&Sheet1!A4:A6&"~"&Sheet1!B4:D6),"~")),"")
Combine all tables into one table: =ARRAYFORMULA({__Unpivot1__; __Unpivot2__, __Unpivot3__})
Pivot above data in Step2: =Query(__Step2__, "SELECT Col2, SUM(Col3) GROUP BY Col2 PIVOT Col1 LABEL Col2 'Student Name' ")
Add WHERE Col2 is not null in the query to remove any possible blank rows

Combine first column of n sheets to one colume

So in one spreadsheet file we have n sheets.
Sheet1
Names
a
b
Sheet2
Names
b
c
d
and so on.. assume n sheets with n values in the first column
Then there's one super sheet.
Super sheet
Supersheet
Supersheet
Supersheet
Supersheet
Sheet1
Sheet2
Sheet3
...
Names
Count
a
1
b
2
c
1
d
1
Assume A1:1 is a list of all of the names of the sheets we want to fetch names from. We want the names pasted into A3:A.
My first thought was to do
=unique(filter({INDIRECT(A1&"!A3:A");INDIRECT(B1&"!A3:A");INDIRECT(C1&"!A3:A")},{INDIRECT(A1&"!A3:A");INDIRECT(B1&"!A3:A");INDIRECT(C1&"!A3:A")}<>""))
and expand, but that doesn't take care of N sheets.
So what I thought I could do was
=ARRAYFORMULA(INDIRECT(A2:C2&"!A2:A3"))
but that only takes care of sheet1. INDIRECT doesn't work with arrayformula
So what can I do?
you can either use some script or put your sheets in array {} and pre-program it:
=QUERY({INDIRECT(Sheet1!A1&"!A3:A");
INDIRECT(Sheet2!A1&"!A3:A")},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''")
since this is one column future sheets can be added this way:
=QUERY({INDIRECT(Sheet1!A1&"!A3:A");
INDIRECT(Sheet2!A1&"!A3:A");
IFERROR(INDIRECT(Sheet3!A1&"!A3:A"));
IFERROR(INDIRECT(Sheet4!A1&"!A3:A"))},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''")
IMPORTRANGE would be:
=QUERY({IMPORTRANGE("id", Sheet1!A1&"!A3:A");
IMPORTRANGE("id", Sheet2!A1&"!A3:A");
IFERROR(IMPORTRANGE("id", Sheet3!A1&"!A3:A"));
IFERROR(IMPORTRANGE("id", Sheet4!A1&"!A3:A"))},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''")
ofc, every importrange formula needs to be authorized separately

Grouping by Name in Query Formula not working

So I am using a query function to count the number of instances a particular name appears in column A of another sheet, and display that result in Column B of this sheet with the respective name in Column A. Here is the function:
=ArrayFormula(QUERY(Attendance!A:A&{"",""},"select Col1, count(Col2) where Col1 != '' group by Col1 label count(Col2) 'Count'",1))
The problem is, while it works for the most part, some of the names appear twice, for instance Fred Jones appears as:
Col A | Col B
Fred Jones | 5
Fred Jones | 2
I have looked at the names, and there is no discernible difference between them, I do not understand why it is not grouping. Is there a way I can use wildcard or something to get Google to combine the names if they are nearly identical? Any help would be appreciated, thanks as always.
try:
=ARRAYFORMULA(QUERY(TRIM({Attendance!A:A}),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)'Count'", 1))

Setting up Google sheets formula

I have a sheet that has 2 columns - id and comment. In a new sheet I need a formula that is set up like the following: First column is the id, the second column is the frequency of the id and the third column is a list of all the comments associated with that id.
Currently I got the first two columns working with the following formula but don't know how to add the third column (list of comments):
=ArrayFormula(QUERY(IF({1,1},TRANSPOSE(SPLIT(CONCATENATE('Oct. 2017'!C:C&CHAR(9)),CHAR(9))))&"","select Col1, count(Col2) group by Col1 order by count(Col2) desc label Col1 'ID', count(Col2) 'Frequency'",0))
Assuming your formula populates A1 onwards and your sheet with two columns is called Oct. 2017 please try in C1 and copied down to suit:
=join(", ",filter('Oct. 2017'!D:D,'Oct. 2017'!C:C=A1))

Resources