您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

listagg函数的用法

2024/2/25 3:35:45发布35次查看
listagg函数的用法
这是一个oracle的列转行函数:listagg()
先看示例代码:
sql代码 
with temp as( select 'china' nation ,'guangzhou' city from dual union all select 'china' nation ,'shanghai' city from dual union all select 'china' nation ,'beijing' city from dual union all select 'usa' nation ,'new york' city from dual union all select 'usa' nation ,'bostom' city from dual union all select 'japan' nation ,'tokyo' city from dual ) select nation,listagg(city,',') within group (order by city) from temp group by nation
这是最基础的用法:
listagg(xxx,xxx) within group( order by xxx)
用法就像聚合函数一样,通过group by语句,把每个group的一个字段,拼接起来。
非常方便。
同样是聚合函数,还有一个高级用法:
就是over(partition by xxx)
也就是说,在你不实用group by语句时候,也可以使用listagg函数:
sql代码
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
总结:listagg()把它当作sum()函数来使用就可以了。
以上就是listagg函数的用法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product