【问题标题】:identify duplicates as well as the matched unique record in oracle在 oracle 中识别重复项以及匹配的唯一记录
【发布时间】:2013-02-21 23:07:19
【问题描述】:

您好,我正在运行以下查询来识别重复记录。

SELECT *
          FROM unique2 P WHERE EXISTS(SELECT 1 FROM unique2 C 
                                    WHERE ( (C.surname) =  (P.surname)) 
                                      AND ( (C.postcode) =  (P.postcode)) 
                                      AND ((( (C.forename) IS NULL OR  (P.forename) IS NULL) 
                                      AND  (C.initials) =  (P.initials)) 
                                        OR  (C.forename) =  (P.forename))
                                      AND ( (C.sex) =  (P.sex) 
                                        OR  (C.title) =  (P.title)) 
                                      AND (( (C.address1))=( (P.address1)) 
                                        OR ( (C.address1))=( (P.address2)) 
                                        OR ( (C.address2))=( (P.address1))
                                        OR  instr(C.address1_notrim, P.address1_notrim) > 0 
                                        OR  instr(P.address1_notrim, C.address1_notrim) > 0)
                                      AND C.rowid < P.rowid);

但是通过这个查询,我无法识别与重复记录匹配的唯一记录 ID。有没有办法识别 重复项以及与这些重复项匹配的唯一记录 ID(我的表具有唯一键)?

【问题讨论】:

    标签: oracle duplicates


    【解决方案1】:
    select id
    from promolog
    where surname, postcode, dob in (
      select surname, postcode,dob
      from (
        select surname, postcode, dob, count(1)
        from promolog
        group by surname,postcode,dob
        having count(1) > 1
      )
    )
    

    【讨论】:

    • 您好,感谢您的回复。但我还有一些其他规则来识别重复项,例如:出生日期和 b。邮政编码和 c。姓和 d。地址 i。邮寄地址 1 = 邮寄地址 1或 mailed_address1 = mailed_address2 iii。或 mailed_address2 = mailed_address1 iv。或记录 2 的 mailed_address1 中记录 1 的 mailed_address1。或记录 1 的 mailed_address1 中记录 2 的 mailed_address1
    • @subash 只需添加/更改您需要在此查询中比较的任何字段(我使用了您原始帖子中的 3 个字段:姓氏、邮政编码、出生日期)。
    • @subash:如果你更新你的问题,它会更好。你遇到了什么问题。
    • @subash 就像我说的那样,使用它作为模板,添加或修改您认为应该是唯一的字段组合
    【解决方案2】:

    您也可以使用分析函数来做到这一点:

    select id, num_of_ids, first_id, surname, postcode, dob
    from (
        select id,
            count(*) over (partition by surname, postcode, dob) as num_of_ids,
            first_value(id)
                over (partition by surname, postcode, dob order by id) as first_id,
            surname,
            postcode,
            dob
        from promolog
    )
    where num_of_ids > 1;
    

    根据你的更新,我认为你可以做一个自加入,你可以把它变得像你喜欢的那样复杂:

    select dup.*, master.id as duplicate_of
    from promolog dup
    join promolog master
    on master.surname = dup.surname
    and master.postcode = dup.postcode
    and master.dob = dup.dob
    ... and <address checks etc. > ...
    and master.rowid < dup.rowid;
    

    但也许我仍然缺少一些东西。顾名思义,exists是用于测试匹配记录的存在;如果您想从匹配的记录中检索任何数据,那么您需要在某个时候加入它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 2020-08-01
      • 1970-01-01
      • 2012-07-01
      • 2022-11-30
      相关资源
      最近更新 更多