Oracle listagg,wm

您所在的位置:网站首页 项目推进记录表怎么写 Oracle listagg,wm

Oracle listagg,wm

2023-03-11 14:31| 来源: 网络整理| 查看: 265

1、准备数据表

2、根据学生名(stu_name)分组,学生名相同的,学生年龄(stu_age)用逗号拼接,使用 listagg()函数法拼接

 

 3、上图中出现了两个12,12,实现去重

 3.1 listagg() 函数 去重

【方法1】使用正则表达式去重,oracle 11g 和oracle 19c都适用

select stu_name, regexp_replace(listagg(stu_age,',') within group (order by stu_age), '([^,]+)(,\1)*(,|$)','\1\3') stu_age from student group by stu_name

【方法2】先把数据去重在使用listagg()函数,oracle 11g 和oracle 19c都适用 

select stu_name, listagg(stu_age,',') within group (order by stu_age) stu_age from (select DISTINCT stu_name,stu_age from student) group by stu_name

 【方法3】直接使用listagg( )函数 distinct方法,适用oracle 19c 不适用oracle 11g

select stu_name, listagg(DISTINCT stu_age,',') within group (order by stu_age) stu_age from student group by stu_name

 

 【方法3简化】:oracle 19c 可以不写 within group(order by xxx),如果需要对去重结果排序可以加上

select stu_name, listagg(DISTINCT stu_age,',') stu_age from student group by stu_name

 3.2 wm_concat()函数 去重 

 【方法1】直接使用wm_concat( )函数 distinct方法,适用oracle 11g 不适用oracle 19c

select stu_name, wm_concat(DISTINCT stu_age) stu_age from student group by stu_name

使用Oracle 19c 测试 wm_concat()函数会报错

 总结:尽量使用listagg函数,避免使用wm_concat,11gr2之后的版本中WM_CONCAT函数已经弃用,使用listagg函数代替wm_concat



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3