the SQL for creating tables and preparing table data is as follows:
CREATE TABLE aaa (
Age number,--ages
Name1 VARCHAR2 (20)--Name
)
INSERT into AAA VALUES (23, ' John ');
INSERT into AAA VALUES (24, ' Dick ');
INSERT into AAA VALUES (23, ' American ');
INSERT into AAA VALUES (24, ' Alyssa ');
INSERT into AAA VALUES (23, ' Zhang Great ');
INSERT into AAA VALUES (27, ' silently ');
INSERT into AAA VALUES (29, ' Carina Lau ');
INSERT into AAA VALUES (23, ' Zhang Bo-ji ');
INSERT into AAA VALUES (32, ' TSE Front ');
INSERT into AAA VALUES (32, ' Chen Zhizhong ');
INSERT into AAA VALUES (33, ' Gillian ');
INSERT into AAA VALUES (33, ' life ');
SELECT * FROM Triple-A-by-age ASC;
23 sheets of three
23 American
23 Pieces of great
23 Zhang Bo-ji
24 Dick
24 Alyssa
27 silently
29 Carina Lau
32 TSE Front
32 Chen Zhizhong
33 Life
33 Ah Jiao
Desired table Results
23 American-#-Zhang Bo-ji-#-John-#-Zhang Great
24 Alyssa-#-Dick
27 silently
29 Carina Lau
32 Chen Zhizhong-#-TSE Front
33 Jiao-#-Life
SQL is as follows:
SELECT
Age,listagg (CPD. Name1, '-#-') WITHIN GROUP (order by name1 ASC)
From AAA CPD GROUP by age;
Explain:
The first parameter of the LISTAGG is the column name to be counted,
The second parameter '-#-' is a connection fit,
How to sort the columns that are counted with the order in group brackets: positive sequence or reverse order
Final result of the table
23 American-#-Zhang Bo-ji-#-John-#-Zhang Great
24 Alyssa-#-Dick
27 silently
29 Carina Lau
32 Chen Zhizhong-#-TSE Front
33 Jiao-#-Life