Oracle列转行函数LISTAGG()

--Oracle列转行函数LISTAGG()
with tb_temp as(
  select 'China' 国家,'Wuhan' 城市 from dual union all
  select 'China' 国家,'Dongjing' 城市 from dual union all
  select 'China' 国家,'Xijing' 城市 from dual union all
  select 'Germany' 国家,'Berlin' 城市 from dual union all
  select 'USA' 国家,'NewYork' 城市 from dual union all
  select 'USA' 国家,'Washington' 城市 from dual
)
select 国家,listagg(城市,',') within group(order by 城市) as 一些城市
from tb_temp 
group by 国家

--不使用Group by语句时候,也可以使用LISTAGG函数:
with temp as(  
select 500 population, 'China' nation ,'Guangzhou' city from dual union all  
select 1500 population, 'China' nation ,'Shanghai' city from dual union all  
select 500 population, 'China' nation ,'Beijing' city from dual union all  
select 1000 population, 'USA' nation ,'New York' city from dual union all  
select 500 population, 'USA' nation ,'Bostom' city from dual union all  
select 500 population, 'Japan' nation ,'Tokyo' city from dual   
)  
select population,  
nation,  
city,  
listagg(city,',') within GROUP (order by city) over (partition by nation) rank  
from temp
原文地址:https://www.cnblogs.com/niwotaxuexiba/p/9997727.html