Oracle Column Values Merge

Source: Internet
Author: User

The most common way to merge column values is to write a custom function to the fact that this is the other way around.

The merge column values in SQL Server can use the for Xml Path, which in Oracle can use Wm_concat or Listagg.

Prepare the numbers:

CREATE TABLE MyTest (xType number,city nvarchar2),/insert into MyTest (xtype,city) SELECT 1,n ' Beijing ' from dual UNION allse Lect 1,n ' Shanghai ' from dual Union allselect 1,n ' Guangzhou ' from dual Union allselect 2,n ' Wuhan ' from dual Union allselect 2,n ' Hangzhou ' from Du Al UNION allselect 2,n ' Xiamen ' from dualcommit;/
Using Wm_concat:

SELECT Xtype,wmsys.wm_concat (To_char) as Xcityfrom Mytestgroup by XType

Using Listagg:

SELECT XType, Listagg (To_char, ', ') within GROUP (ORDER by XType) as Xcityfrom Mytestgroup by XType
Results:

XTYPE xcity
----------   --------------------------------------
1 Beijing, Guangzhou, Shanghai
2 Hangzhou, Wuhan, Xiamen

Note:

0, the above in the city before the addition of To_char () function, is to prevent the situation of the chaotic code;

1, Wm_concat in the official documents of Oracel not, can not guarantee the compatibility of each version;

2, Listagg is the 11g version of the new aggregation function.


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.