需求:listagg去重,字符串去重,去重统计
select listagg(t.city, ',') within group(order by t.city),
regexp_replace((listagg(t.city, 'A') within group(order by t.city)), '([^A]+)(A\1)+', '\1') city,
regexp_replace((listagg(t.city, '-') within group(order by t.city)), '([^-]+)(-\1)+', '\1') city,
regexp_replace((listagg(t.city, '+') within group(order by t.city)), '([^+]+)(+\1)+', '\1') city,
count(distinct t.city) cityNum
from (
select '北京' as city,'010' as code from dual
union all
select '北京' as city,'010' as code from dual
union all
select '北京' as city,'010' as code from dual
union all
select '上海' as city,'021' as code from dual
union all
select '成都' as city,'028' as code from dual
union all
select '上海' as city,'021' as code from dual
union all
select '北京' as city,'010' as code from dual
union all
select '成都' as city,'028' as code from dual
)t
执行流程(特别注意 分隔符不能用正则语法中的特殊字符,例如 + $,建议,A)
步骤一:得到排序后的字符串 上海,上海,北京,北京,北京,北京,成都,成都
步骤二:([^,]+) 不为','的字符串,分组
步骤三:(,\1)+ 分组后 上海,上海 北京,北京,北京,北京 成都,成都
步骤四:'\1' 每组的第一个子串,替换当前组。 replace('上海,上海','上海,')
Oracle的REGEXP_REPLACE函数简单用法详解
语法
Oracle数据库中的REGEXP_REPLACE函数的语法是:
REGEXP_REPLACE(source_char, pattern [, replace_string [, position [, occurrence [, match_parameter ] ] ] ] )
参数 source_char
搜索值的字符表达式。这通常是一个字符列,可以是任何数据类型CHAR,VARCHAR2,NCHAR,NVARCHAR2,CLOB或NCLOB。
正则表达式 pattern
replace_string
可选。匹配的模式将被替换replace_string字符串。如果省略replace_string参数,
将删除所有匹配的模式,并返回结果字符串。
position
可选。在字符串中的开始位置搜索。如果省略,则默认为1。
occurrence
可选。是一个非负整数默认为1,指示替换操作的发生:
如果指定0,那么所有出现将被替换字符串。
如果指定了正整数n,那么将替换第n次出现。
match_parameter
可选。它允许你修改REGEXP_REPLACE功能匹配的行为。它可以是以下的组合: