【问题标题】:Remove Duplicate words/strings from Oracle table using Regular Expressions使用正则表达式从 Oracle 表中删除重复的单词/字符串
【发布时间】:2016-03-01 19:23:39
【问题描述】:

我想从Col B 中删除重复的字符串。例如:“New Cap Grp”在第二条记录中重复了五次。

Col A   Col B
-----   -----
WDSA    ALT COMPANY, III & New Group
1101    New Cap Grp & New Cap Grp & New Cap Grp & New Cap Grp & New Cap Grp 
2255    Tata Associates Inc. & Tata Associates Inc.& Towers Watson 
3355    Picard Lorens, Inc. & Tata Associates Inc. & Tata Associates Inc. 
8877    Morphy Companies, Inc. & Morphy Companies, Inc. & Tele Pvt.Ltd

我是正则表达式的新手,所以我无法弄清楚这究竟是如何实现的。如果有人知道如何处理这种情况,请帮助我。

【问题讨论】:

  • 这不用说你知道每条记录的重复值吧?
  • 是的,我知道每条记录中的重复值。

标签: sql regex oracle oracle11g


【解决方案1】:

我认为只使用正则表达式是不可能的,因为您必须更新 Col B* 值。

PL/SQL 上做起来更容易,我尝试做:

为测试数据创建表

create table test
    (
        id   number,
        text varchar2(100)
    );

插入测试数据

insert into test values (1, 'ALT COMPANY, III & New Group');
insert into test values (2, 'New Cap Grp & New Cap Grp & New Cap Grp & New Cap Grp & New Cap Grp');
insert into test values (3, 'Tata Associates Inc. & Tata Associates Inc.& Towers Watson');
insert into test values (4, 'Picard Lorens, Inc. & Tata Associates Inc. & Tata Associates Inc.');
insert into test values (5, 'Morphy Companies, Inc. & Morphy Companies, Inc. & Tele Pvt.Ltd');

PL/SQL 块:

declare
    l_new_column_value varchar2(1024) := '';
begin
    -- go on all row
    for x in (select id, text from test)
    loop
        -- work with each row, do from one row several by separation symbol '&' and take distinct value
        for concat_text in (
            select distinct trim(regexp_substr(text, '[^&]+', 1, level)) as part_value
            from
                (
                    select text
                    from test
                    where id = x.id
                )
            connect by instr(text, '&', 1, level - 1) > 0)
        loop
            -- formiration new uniq value 
            l_new_column_value := l_new_column_value || concat_text.part_value || ' & ';
        end loop;
        -- undate raw data
        update test
            set text = substr(l_new_column_value, 0, length(l_new_column_value)-3)
        where id = x.id;
        l_new_column_value := '';
    end loop;
end;

【讨论】:

  • 感谢您的回复,但我一直在寻找仅使用带有正则表达式的sql的解决方案,但现在似乎不可能。
猜你喜欢
  • 2014-06-21
  • 1970-01-01
  • 2021-01-02
  • 2018-01-26
  • 1970-01-01
  • 2022-01-27
  • 2015-04-11
  • 2015-05-22
  • 1970-01-01
相关资源
最近更新 更多