Oracle: find duplicate rows in select query

徘徊边缘 提交于 2019-12-11 12:08:13

问题


My SQL query returns results with 4 columns "A", "B", "C", "D".

Suppose the results are:

A    B    C    D
1    1    1    1
1    1    1    2
2    2    2    1

Is it possible to get the count of duplicate rows with columns "A", "B", "C" in each row.

e.g. the expected result is:

A    B    C    D    cnt
1    1    1    1    2
1    1    1    2    2
2    2    2    1    1

I tried using count(*) over. But it returns me the total number of rows returned by the query. Another information is that in example I have mentioned only 3 columns based on which I need to check the count. But my actual query has such 8 columns. And number of rows in database are huge. So I think group by will not be a feasible option here. Any hint is appreciable. Thanks.


回答1:


Maybe too late, but probably the count over as analytic function (aka window function) within oracle helps you. When I understand your request correctly, this should solve your problem :

create table sne_test(a number(1)
                 ,b number(1)
                 ,c number(1)
                 ,d number(1)
                 ,e number(1)
                 ,f number(1));

insert into sne_test values(1,1,1,1,1,1);
insert into sne_test values(1,1,2,1,1,1);
insert into sne_test values(1,1,2,4,1,1);
insert into sne_test values(1,1,2,5,1,1);
insert into sne_test values(1,2,1,1,3,1);
insert into sne_test values(1,2,1,2,1,2);
insert into sne_test values(2,1,1,1,1,1);

commit;

 SELECT a,b,c,d,e,f, 
       count(*) over (PARTITION BY a,b,c)
  FROM sne_test;

 A  B  C  D  E  F AMOUNT
-- -- -- -- -- -- ------
 1  1  1  1  1  1      1
 1  1  2  4  1  1      3
 1  1  2  1  1  1      3
 1  1  2  5  1  1      3
 1  2  1  1  3  1      2
 1  2  1  2  1  2      2
 2  1  1  1  1  1      1



回答2:


To find duplicates you must group the data based on key column

select 
  count(*)
  ,empno 
from 
  emp
group by
  empno
having
  count(*) > 1;

This allows you to aggregate by empno even when multiple records exist for each category (more than one).




回答3:


You have to use a subquery where you get the count of rows, grouped by A, B and C. And then you join this subquery again with your table (or with your query), like this:

select your_table.A, your_table.B, your_table.C, your_table.D, cnt
from
  your_table inner join
  (SELECT A, B, C, count(*) as cnt
   FROM your_table
   GROUP BY A, B, C) t
  on t.A = your_table.A
     and t.B = your_table.B
     and t.C = your_table.C


来源:https://stackoverflow.com/questions/13776123/oracle-find-duplicate-rows-in-select-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!