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

 

posted @ 2018-11-21 20:43  学亮编程手记  阅读(563)  评论(0编辑  收藏  举报