Oracle column Change function Listagg ()

Source: Internet
Author: User

This is an Oracle's column change function: Listagg ()

Look at the sample code first:

With temp as (  select ' China ' Nation, ' Guangzhou ' to dual union ALL  Select ' China ' Nation, ' Shanghai ' city F ROM Dual UNION All  Select ' China ' Nation, ' Beijing ' to dual union ALL  Select ' USA ' Nation, ' New York ' city From dual UNION ALL  Select ' USA ' Nation, ' Bostom ' to dual union ALL  Select ' Japan ' Nation, ' Tokyo ' city fro M dual) Select Nation,listagg (City, ', ') within GROUP [order by City] from Tempgroup by nation

  

This is the most basic usage:

Listagg (xxx,xxx) within GROUP (ORDER by XXX)

Using a GROUP BY statement, a field of each group is stitched together, just like an aggregate function.

Very convenient.

Also an aggregation function, there is an advanced usage:

Is over (partition by XXX)

In other words, you can also use the Listagg function when you are not practical with the GROUP BY statement:

With temp as (  Select $ population, ' China ' nation, ' Guangzhou ' City from dual union All  Select population, ' China ' nation, ' Shanghai ' to dual union ALL  Select + population, ' China ' nation, ' Beijing ' city from dual UN Ion all  Select population, ' USA ' Nation, ' New York ' to dual union ALL Select +  population, ' USA ' NAT Ion, ' Bostom ' City from dual union All  Select population, ' Japan ' Nation, ' Tokyo ' to dual ' select Populatio N,       Nation, City       ,       Listagg (city, ', ') within GROUP (order by city) over (partition by nation) Rankfrom Temp

  

Summary: Listagg () uses it as a sum () function.

Original: http://dacoolbaby.iteye.com/blog/1698957

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.