Oracle listagg() 中的子字符串去重 regexp_replace

本文深入解析Oracle正则表达式的使用技巧,重点介绍regexp_replace()和listagg()函数,展示如何进行字符串替换和子串去重,适用于数据处理和文本分析场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 概述

listagg 子串去重
方法:regexp_replace()
必须 排序(相邻去重)
字符串长度不能超过 4000
varchar2 的最大长度

2 语法

2.1 regexp_replace():字符串替换

-- 将 '源字符串' 中 符合 '正则表达式' 的字符串替换为 '新字符串'
regexp_replace('源字符串', '正则表达式', '新字符串')

-- 去除 子串 中的数字(数字 替换为 空)
select regexp_replace('abc123', '[0-9]', '') -- abc
  from dual;

2.2 listagg():先排序

regexp_replace(listagg('列1', ',') within group(order by '列2'),
               '([^,]+)(,\1)*(,|$)',
               '\1\3')

-- 相邻相同字符串去重,故一定要 先排序!
select regexp_replace('1,1,3,5,5', '([^,]+)(,\1)*(,|$)', '\1\3') 
  from dual; 
-- 结果: 1,3,5

参数解释:源字符串 = '1,1,3,5,5’

-- ([^,]+)(,\1)*(,|$) 由 3 个括号组成
([^,]+): 1个或多个非 ',' 的数据,如: 1 1 3 5 5
(,\1)* : 括号1 ', 隔开 2个重复数据',如: (1,1),(3),(5,5)   
(,|$)  : 末尾添加 ',''$',如: (1,1,)(3,)(5,5,)

with t_str as (
  select '1,1,3,5,5' str from dual
)
select t.str, -- 1,1,3,5,5
       regexp_replace(t.str, '([^,]+)', 'x') a, -- x,x,x,x,x
       regexp_replace(t.str, '([^,]+)(,\1)*', 'x') b, -- x,x,x
       regexp_replace(t.str, '([^,]+)(,\1)*(,|$)', 'x') c -- xxx
  from t_str t;


\n: 匹配第 n 个 () 中的引用('重复一次''(\d)\1': 匹配两个连续的数字,如:11aa 中的 11
'(\d)(a)\1': 匹配第一个是数字,第二个是a,第三个引用第一个数字,如:1a1
'(\d)(a)\2': 如:1aa
'(\d)(a)\1\2': 如:1a1a

select regexp_replace('11aa', '(\d)\1', 'x') a, -- xaa
       regexp_replace('1a1a', '(\d)(a)\1', 'x') b, -- xa
       regexp_replace('1aa1', '(\d)(a)\2', 'x') c, -- x1
       regexp_replace('1a1aa', '(\d)(a)\1\2', 'x') d -- xa
  from dual;

3 扩展

3.1 Oracle 正则表达式详解:regexp_xx

select decode(f.fab,'0','FAB10','') fab, h.lastname, c.field42 dept, f.liushuihao, (select listagg(d.huali_liaohao,'</br>') within group (order by d.id) from formtable_main_1233_dt1 d where d.mainid = f.id) as huali_liaohao, (select listagg(d.wuliao_pinming,'</br>') within group (order by d.id) from formtable_main_1233_dt1 d where d.mainid = f.id) as wuliao_pinming, (select listagg(d.supplier,'</br>') within group (order by d.id) from formtable_main_1233_dt1 d where d.mainid = f.id) as supplier, (select listagg(d.supplier_batch,'</br>') within group (order by d.id) from formtable_main_1233_dt1 d where d.mainid = f.id) as supplier_batch, (select listagg(d.outlier_quantity,'</br>') within group (order by d.id) from formtable_main_1233_dt1 d where d.mainid = f.id) as outlier_quantity, f.des, f.sqe_date, case when f.d3_enclose is not null then (select i.docsubject from docdetail i where i.id = (REGEXP_REPLACE(f.d3_enclose, '\,[^,]*$', ''))) else '' end as d3_enclose, f.d3_date, case when f.d4_enclose is not null then (select i.docsubject from docdetail i where i.id = (REGEXP_REPLACE(f.d4_enclose, '\,[^,]*$', ''))) else '' end as d4_enclose, f.d4_date, case when f.d8_report is not null then (select i.docsubject from docdetail i where i.id = (REGEXP_REPLACE(f.d8_report, '\,[^,]*$', ''))) else '' end as d8_report, f.d8_date, case when f.zz_8d is not null then (select i.docsubject from docdetail i where i.id = (REGEXP_REPLACE(f.zz_8d, '\,[^,]*$', ''))) else '' end as zz_8d, f.bg_date, f.cycle_time, decode(f.if_delay,0,'超时',1,'未超时','') if_delay from formtable_main_1233 f left join hrmresource h on f.applicant = h.id left join cus_fielddata c on c.id = h.id and c.scope = 'HrmCustomFieldByInfoType' 帮我优化一下sql
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼丸丶粗面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值