Unpivot data table with column groups - google-sheets

My data is in this format:
A
B
C
D
E
F
G
USER1
A-DATA1
A-DATA2
A-DATA3
B-DATA1
B-DATA2
B-DATA3
USER2
A-DATA1
A-DATA2
A-DATA3
B-DATA1
B-DATA2
B-DATA3
The desired results would look like this:
A
B
C
D
USER1
A-DATA1
A-DATA2
A-DATA3
USER1
B-DATA1
B-DATA2
B-DATA3
USER2
A-DATA1
A-DATA2
A-DATA3
USER2
B-DATA1
B-DATA2
B-DATA3
I test many things, with INDEX - ROW - COLUMN - TRANSPOSE
but I never get the desired result... If you have a great idea, that would be super helpful.

try:
=QUERY({A3:D; A3:A\ E3:G}; "where Col1 is not null order by Col1"; )

Related

Intercalate multiple columns when some of those columns must remain in the same row

In Column A I have the id of the home team, B the name of the home team, C the id of the visiting team and in D the name of the visiting team:
12345 Borac Banja Luka 98765 B36
678910 Panevezys 43214 Milsami
1112131415 Flora 7852564 SJK
1617181920 Magpies 874236551 Dila
I want to create a column of ids and another of names but keeping the sequence of who will play with whom:
12345 Borac Banja Luka
98765 B36
678910 Panevezys
43214 Milsami
1112131415 Flora
7852564 SJK
1617181920 Magpies
874236551 Dila
Currently (the model works) I'm joining the columns with a special character, using flatten and finally split:
=ARRAYFORMULA(SPLIT(FLATTEN({
FILTER(A1:A&"§§§§§"&B1:B,(A1:A<>"")*(B1:B<>"")),
FILTER(C1:C&"§§§§§"&D1:D,(C1:C<>"")*(D1:D<>""))
}),"§§§§§"))
Is there a less archaic and correct approach to working in this type of case?
Spreadsheet to tests
889
A
5687
C
532
B
8723
D
Stack up the columns using {} and SORT them by a SEQUENCE of 1,2,1,2:
=SORT({A1:B2;C1:D2},{SEQUENCE(ROWS(A1:B2));SEQUENCE(ROWS(A1:B2))},1)
889
A
5687
C
532
B
8723
D
You can also try with function QUERY, enter this formula in F1:
={QUERY((A1:B), "SELECT * WHERE A IS NOT NULL and B IS NOT NULL",1);
QUERY((C1:D), "SELECT * WHERE C IS NOT NULL and D IS NOT NULL",1)}

Google Sheets - GROUP BY and ORDER BY on different column

I got a table in this format
Name Datum
A 01.01.2019
B 17.03.2020
C 18.03.2020
C 01.04.2020
I get this table output from this query:
=QUERY(Anrufe!$1:$1000;"Select A,B where A is not null ORDER BY B ASC label A 'Name', B 'Datum'")
I am trying to change the query, so that it performs a group by. This is what it should look like
Name Datum count
A 01.01.2019 1
B 17.03.2020 1
C 01.04.2020 2
But when i add a group by and add a aggregation function to the select it still throws an error.
=QUERY(Anrufe!$1:$1000;"Select A,B, min(B) where A is not null group by A ORDER BY B ASC label A 'Name', B 'Datum'")
I want the query to choose the youngest Date, that is in the column B. But it seems that the QUERY-Parser forces me to add the column B to the group by. This results in the same problem, that i manually have to count the records with the same names.
Anyone has any idea how to solve that?
Already checked those links:
Google sheets query order by SUM
Google Sheets Query: Possible to Group / Order by without Select?
Google Sheets query - Multiple “where” conditions with “group by” and “order by”
I got this message (CANNOT_GROUP_WITHOUT_AGG) from a simple query
I think you want this:
=QUERY(Anrufe!$1:$1000,"Select A,min(B),count(A) where A is not null group by A ORDER BY min(B) ASC label A 'Name', min(B) 'Datum'")

How to FILTER with LARGE condition on similar values?

I have a table shown at FILTER_LARGE.
I would like to FILTER 3 highest values from column F by LARGE condition and 3 corresponding names from column E.
For some reason I would like to avoid using SORT function.
The issue is - how to retrieve the right values when some of cells in F have same numbers?
Maybe Query helps ?
=query(E1:F; "where F is not null order By F desc limit 3")
Alternatively (to include all ties), also try
=ArrayFormula(query(E1:F; "where F >= "&LARGE(unique(F:F); 3)&""))
You might want to add Order By desc to #JPV answer:
=ArrayFormula(query(E1:F, "select E, F where F >= "&LARGE(unique(F:F), 3)&" order by F desc"))

How to insert data from one table column to another table

I have a file that is something like below format.
test.txt
1 | ABC | A, B, C, D
I need a stored procedure that insert record in details table in row by row basis. e.g.
ID Name Type
1 ABC A
1 ABC B
1 ABC C
1 ABC D
Is it possible through stored procedure in sql. Any help will be appreciated. Thanks in advance.
You can either:
Split it in your code and then insert them
Bulk insert them in a temporary table and split them all like this:
-- SAMPLE Data
declare #data table(id int, name varchar(10), type varchar(100))
insert into #data(id, name, type) values
(1, 'ABCD', 'A, B, C, D')
, (2, 'EFG', 'E, F, G')
, (3, 'HI', 'H, I')
-- Split All Rows and Types
Select ID, Name, ltrim(rtrim(value))
From (
Select *, Cast('<x>'+Replace(d.type,',','</x><x>')+'</x>' As XML) As types
From #data d
) x
Cross Apply (
Select types.x.value('.', 'varchar(10)') as value
From x.types.nodes('x') as types(x)
) c
Output:
ID Name Type
1 ABCD A
1 ABCD B
1 ABCD C
1 ABCD D
2 EFG E
2 EFG F
2 EFG G
3 HI H
3 HI I

How to find the max count of a grouped column in Grails/GORM?

I have a table with the following columns/values:
id group_id state type_id
1 g1 NY t1
2 g1 NY t1
3 g1 PA t1
4 g2 NY t1
5 g3 CA t1
6 g4 CA t2
I would like to identify the maximum frequency of a group of group_id, state combinations given a type_id. To do this in straight SQL I would do something like the following:
SELECT MAX (COUNT (*)) AS perGroup
FROM table_name
WHERE type_id = t1
GROUP BY group_id, state
This seems to work just fine in SQL but when I attempt to translate it to a Criteria Builder in Grails/GORM it always gives me a max of 0.
I've tried just getting the count and it seems to be working properly. However; this would require me to loop in the groovy code after the db call and I would like to avoid that if I can.
This is what I have that works for the count:
Table.createCriteria().list {
eq("type.id",'t1')
projections {
sqlProjection "count(*) as theCount",['theCount'], [INTEGER]
groupProperty "group"
groupProperty "state"
}
}
How would I identify the max instead?
For example, given the data in the above scenario I would expect the following output:
t1 = 2
t2 = 1

Resources