Find duplicates for several columns exclusive ID-column

*爱你&永不变心* 提交于 2019-12-05 16:24:54

There is a lot of versions suggested here but I think I came up with a new one.

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)
;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  

To get all the rows that are duplicates:

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 unique row (i.e. retain one row and filter the other duplicate rows) use this:

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 

Assuming at least SQL 2005 for the 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

I don't totally understand your problem, but here is a shot at a different style of solution:

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

You can store the C1, C2, C3 combination for duplicates into a temp table and then join it to get the IDs.

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