Find duplicates for multiple columns

I found a lot of answers on how to find duplicates, including a PK column or with no focus on it, like this:

If you have a table called T1 and the columns are c1, c2 and c3, then this query will show you duplicate values.

SELECT C1, C2, C3, count(*)as DupCount
 from T1
 GROUP BY C1, C2, C3
 HAVING COUNT(*) > 1

But a more general requirement would be to get the identifier of all duplicates having equal values ​​c1, c2, c3.

So, I need to do something that does not work, because the identifier must be aggregated:

SELECT ID
 from T1
 GROUP BY C1, C2, C3
 HAVING COUNT(*) <> 1

(The identifier of all duplicates should be different, but the columns should be equal)

Edit :

Thanks to everyone. I always wonder how quickly people give great answers to Stackoverflow!

+3
source share
6 answers

, , .

select *
from @T as T1
where exists (select *
              from @T as T2
              where
                T1.ID <> T2.ID and
                T1.C1 = T2.C1 and
                T1.C2 = T2.C2 and
                T1.C3 = T2.C3)
+5
;WITH CTE
     AS (SELECT ID,
                C1,
                C2,
                C3,
                COUNT(*) OVER (PARTITION BY C1, C2, C3) AS Cnt
         FROM   T1)
SELECT ID,
       C1,
       C2,
       C3
FROM   CTE
WHERE  Cnt > 1  
+3

To get all duplicate rows:

Use this:

WITH Dups AS
(
    SELECT *, 
           COUNT(1) OVER(PARTITION BY C1, C2, C3) AS CNT
      FROM T1  
)
SELECT * 
  FROM Dups
 WHERE CNT > 1

and to a unique row (i.e. keep one row and filter other duplicate rows):

WITH NoDups AS
(
    SELECT *, 
         ROW_NUMBER() OVER(PARTITION BY C1, C2, C3 ORDER BY ID) AS RN
      FROM T1  
)
SELECT * 
  FROM NoDups
WHERE RN = 1 
+3
source

Assuming at least SQL 2005 for CTE:

;with cteDuplicates as (
    select c1, c2, c3
        from t1
        group by c1, c2, c3
        having count(*) > 1
)
select id
    from t1
        inner join cteDuplicates d
            on t1.c1 = d.c1
                and t1.c2 = d.c2
                and t1.c3 = d.c3
+1
source

I do not quite understand your problem, but here is a shot in a different solution style:

select id
from t1 a
join t1 b on a.c1 = b.c2
join t1 c on b.c2 = c.c3
where a.id <> b.id and b.id <> c.id and a.id <> c.id
0
source

You can save a combination of C1, C2, C3 for duplicates in a temporary table, and then join it to get the identifiers.

select C1, C2, C3
into #duplicates
from T1
group by C1, C2, C3
having count(*) > 1

select ID
from T1 t
inner join #duplicates d
    on  t.C1 = d.C1
    and t.C2 = d.C2
    and t.C3 = d.C3
0
source

Source: https://habr.com/ru/post/1793073/


All Articles