Turn: Oracle's Column change function: Listagg ()

Source: Internet
Author: User

Look at the sample code first:

SQL code
  1. With temp as (
  2. Select ' China ' nation,' Guangzhou ' to dual Union All
  3. Select ' China ' nation,' Shanghai ' to dual Union All
  4. Select ' China ' Nation,' Beijing ' to dual Union All
  5. Select ' USA ' Nation,' New York ' to dual Union all
  6. Select ' USA ' Nation,' Bostom ' city from dual Union All
  7. Select ' Japan ' Nation,' Tokyo ' from dual
  8. )
  9. Select Nation,listagg (City,', ') within GROUP (order by city)
  10. From temp
  11. GROUP 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:

SQL code
  1. With temp as (
  2. Select population, ' China ' nation,' Guangzhou ' to dual Union All /c1>
  3. Select population, ' China ' nation,' Shanghai ' to dual Union all
  4. Select population, ' China ' Nation,' Beijing ' to dual Union all
  5. Select population, ' USA ' Nation,' New York ' city from dual Union all
  6. Select population, ' USA ' Nation,' Bostom ' city from dual Union all
  7. Select population, ' Japan ' Nation,' Tokyo ' city from dual
  8. )
  9. Select population,
  10. Nation
  11. City
  12. Listagg (city,', ') within GROUP [order by City ] Over (partition by Nation) rank
  13. From temp

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

Turn: Oracle's Column change function: Listagg ()

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.