从oracle中的字符串中删除重复的子字符串
Posted
技术标签:
【中文标题】从oracle中的字符串中删除重复的子字符串【英文标题】:Remove duplicate substring from a string in oracle 【发布时间】:2021-04-13 20:16:58 【问题描述】:我的表格中有如下字符串
2001,2452,2452,2421,2421,2495
2001,2483,2421,2421,2482
2001,2420,2421,2421,2425
2001,2420,2421,2421,2422
2001,2452,2452,2421,2421,2464
我想删除重复的数字,如 2452 和 2421,并在数据中只显示一次,如
2001,2452,2421,2495
2001,2483,2421,2482
2001,2420,2421,2425
2001,2420,2421,2422
2001,2452,2421,2464
有人做过这样的事吗?请告诉我如何解决这个问题
谢谢!
【问题讨论】:
您应该认真考虑不再像这样存储 CSV 数据。将每个 CSV 值放到单独的行中。 您的数据库中是否安装了 APEX?您可以自己编写,但可以使用apex_util.string_to_table
和 apex_util.table_to_string
和/或取决于 APEX 版本 apex_util.split
和 listagg
。假设修复数据模型不是一种选择。
【参考方案1】:
在Oracle SQL中,可以使用层次查询和listagg
,如下:
select str, listagg(str_distinct, ',') within group (order by 1) as distinct_str from
(select distinct str, regexp_substr(str,'[^,]+',1,column_value) str_distinct from cte
cross join table(
cast(multiset(
select level lvl
from dual
connect by level <= regexp_count(str, '[^,]+'))
as sys.odcivarchar2list)
) lvls)
group by str;
db<>fiddle 用于输入字符串之一。
【讨论】:
以上是关于从oracle中的字符串中删除重复的子字符串的主要内容,如果未能解决你的问题,请参考以下文章
Regexp_replace :从 Oracle 中的“+”分隔字符串中删除重复项 [重复]