42

在 10 列/50K 行表中检测重复项的最有效方法是什么?我正在使用 MSSQL 8.0

4

5 回答 5

61

举一个其他人描述的例子:

SELECT
    Col1, -- All of the columns you want to dedupe on
    Col2, -- which is not neccesarily all of the columns
    Col3, -- in the table
    Col4,
    Col5,
    Col6,
    Col7,
    Col8,
    Col9,
    Col10
FROM
    MyTable
GROUP BY
    Col1,
    Col2,
    Col3,
    Col4,
    Col5,
    Col6,
    Col7,
    Col8,
    Col9,
    Col10
HAVING
    COUNT(*) > 1
于 2008-11-20T20:44:07.470 回答
15

您可以group by在所有列上使用,然后count(*)>1

于 2008-11-20T20:28:13.180 回答
8

试试这个

Select * From Table
Group By [List all fields in the Table here]
Having Count(*) > 1
于 2008-11-20T20:43:19.263 回答
3

要检测,只需按照古格所说的分组即可。

select fieldA, fieldB, count(*) from table
group by fieldA, fieldB
having count(*) > 1

如果你想删除欺骗......伪......

select distinct into a temp table
truncate original table
select temp table back into original table

使用 truncate 如果您有 FK 约束,您可能会遇到问题,所以要明智地放弃约束并确保您不会孤立记录。

于 2008-11-20T20:39:04.390 回答
3

除了提供的建议之外,我会在以后努力防止重复,而不是稍后尝试找到它们。

这是通过对应该是唯一的列(或列组)使用唯一索引来完成的。请记住,除了通过您正在处理的特定应用程序之外,可以从其他位置修改数据库中的数据,因此最好在数据库级别定义表中允许和不允许的内容。

于 2008-11-20T21:48:17.700 回答