如何使用表中的所有列选择所有重复行的出现?

时间:2017-08-06 11:09:10

标签: sql oracle

例如:

select c1, c2
from mytable
group by c1, c2
having count(*) > 1;

每个副本只能复制一份。

3 个答案:

答案 0 :(得分:2)

使用窗口功能:

select t.*
from (select t.*, count(*) over (partition by c1, c2) as cnt
      from mytable
     ) t
where cnt > 1;

请注意,即使c1和/或c2NULL,此功能也可用。

但是,如果您只关心c1c2,那么在结果集中包含计数就足够了:

select c1, c2, count(*)
from mytable
group by c1, c2
having count(*) > 1;

Oracle中另一种有趣的方法:

select t.*
from mytable t
where exists (select 1
              from mytable t2
              where t2.c1 = t.c1 and t2.c2 = t.c2 and t2.rowid <> t.rowid
             );

但是,如果c1c2NULL,则会失败,因此第一种方法更为通用。

答案 1 :(得分:0)

使用所有c1 / c2值

添加SQL
with dup as (
select c1,c2 from mytable group by c1,c2 having count(*) > 1
)
select c1,c2 from mytable m, dup where m.c1=dup.c1 and m.c2 = c2.dup;

答案 2 :(得分:0)

试试这个: select * from mytable mt join(选择c1,c2来自mytable group by c1,c2 have count(*)&gt; 1)m on mt.c1 = m.c1 and mt.c2 = m.c2