listagg
1. 语法:
listagg(measure_expr, 'delimiter') within group (order by expr) [over (partition by expr)]
2. 功能说明:
measure_expr:任何基于列的表达式
delimiter:分割符
order by expr:决定被拼接的顺序
over (partition by expr): 表中所有内容按照expr进行分区处理
3. 【示例1】:
create or replace view v 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 v.population, v.nation, listagg(v.city, ',') within group (order by city) over (partition by nation) as rank from v;
结果:
【示例2】:
create or replace view v 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 v.nation, listagg(v.city, ',') within group (order by city) as rank from v group by nation;
注:listagg在这里启动汇总的作用。sum将数值结果加在一起,而listagg是把字符串连接在一起。