Oracle重复行检测

时间:2013-07-24 18:45:03

标签: sql oracle

在Oracle中显示重复行的理想方法是什么?这里的诀窍是我正在寻找所有行而不仅仅是倍数(重复)。例如,在下面的数据集中:

RSN fname   lname   emailaddress
1   John    Smith   j.smith@system.com
2   John    Smith   j.smith@system.com
3   John    Smith   j.smith@system.com
4   Kevin   Walker  k.walker@system.com
5   James   Kirk    j.kirk@system.com
6   James   Kirk    j.kirk@system.com
7   Kevin   James   k.james@system.com
8   Mike    Jones   m.jones@system.com

我希望返回以下内容:

1   John    Smith   j.smith@system.com
2   John    Smith   j.smith@system.com
3   John    Smith   j.smith@system.com
5   James   Kirk    j.kirk@system.com
6   James   Kirk    j.kirk@system.com

任何帮助?

2 个答案:

答案 0 :(得分:1)

这是一种方式:

SELECT RSN, fname, lname, emailaddress
  FROM whatever_your_table_is_named t1
 WHERE ( SELECT COUNT(1)
           FROM whatever_your_table_is_named t2
          WHERE t2.fname = t1.fname
            AND t2.lname = t1.lname
            AND t2.emailaddress = t1.emailaddress
            AND ROWNUM < 3
       ) > 1
;

这是另一个:

SELECT t1.RSN, t1.fname, t1.lname, t1.emailaddress
  FROM whatever_your_table_is_named t1
  JOIN ( SELECT fname, lname, emailaddress
           FROM whatever_your_table_is_named
          GROUP
             BY fname, lname, emailaddress
         HAVING COUNT(1) > 1
       ) t2
    ON t1.fname = t2.fname
   AND t1.lname = t2.lname
   AND t1.emailaddress = t2.emailaddress
;

(免责声明:我没有测试其中任何一种。)

答案 1 :(得分:0)

此子查询将从表中获取重复项。

    select *
    from (some table)
    where rsn IN (select rsn
                  from (some table) 
                   where fname IN 
                   (select fname
                    from (select fname, count(fname) as dup 
           from (some table)) where dup > 1)