Use of the "Oracle" ***listagg WITHIN GROUP by * * * Method __oracle

Source: Internet
Author: User

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







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.