我找到了很多关于如何找到重复项的答案,包括PK-column或没有关注它:
如果您有一个名为T1的表,并且列是c1,c2和c3,那么此查询将显示重复的值。
SELECT C1, C2, C3, count(*)as DupCount
from T1
GROUP BY C1, C2, C3
HAVING COUNT(*) > 1
但更常见的要求是获取具有相等c1,c2,c3值的所有重复项的ID。
所以我需要关注什么不起作用,因为必须聚合id:
SELECT ID
from T1
GROUP BY C1, C2, C3
HAVING COUNT(*) <> 1
(所有重复项的ID必须不同,但列必须相等)
修改:
谢谢大家。我总是很惊讶人们在Stackoverflow上给出了极好的答案!
答案 0 :(得分:6)
这里有很多版本,但我想我想出了一个新版本。
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)
答案 1 :(得分:3)
;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
答案 2 :(得分:3)
获取所有重复的行:
使用此:
WITH Dups AS
(
SELECT *,
COUNT(1) OVER(PARTITION BY C1, C2, C3) AS CNT
FROM T1
)
SELECT *
FROM Dups
WHERE CNT > 1
和唯一行(即保留一行并过滤其他重复行)使用:
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 :(得分:1)
假设CTE至少有SQL 2005:
;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
答案 4 :(得分:0)
我并不完全理解你的问题,但这里有一个不同风格的解决方案:
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
答案 5 :(得分:0)
您可以将重复的C1,C2,C3组合存储到临时表中,然后将其加入以获取ID。
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