Oracle之字符串去重统计

需求: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功能匹配的行为。它可以是以下的组合:

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值