如何删除重复行? [英] How can I remove duplicate rows?

查看:265
本文介绍了如何删除重复行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从相当大的表(即300,000+行)中删除重复行的最佳方法是什么?



当然这些行不会是完美的重复,因为存在RowID身份字段。

  MyTable 
-----------
RowID int not null identity(1,1)主键,
Col1 varchar(20)not null,
Col2 varchar(2048)not null,
Col3 tinyint not null


解决方案

假设没有null,你 GROUP BY 唯一列,而 SELECT MIN(或MAX) RowId作为要保留的行。然后,只需删除没有行ID的所有内容:

 从MyTable 
中删除B $ b LEFT OUTER JOIN b $ b SELECT MIN(RowId)作为RowId,Col1,Col2,Col3
FROM MyTable
GROUP BY Col1,Col2,Col3
)作为KeepRows ON
MyTable.RowId = KeepRows .RowId
WHERE
KeepRows.RowId IS NULL

如果您有GUID而不是整数,您可以替换

  MIN(RowId)
pre>

  CONVERT(uniqueidentifier,MIN(CONVERT (36),MyGuidColumn)))


What is the best way to remove duplicate rows from a fairly large table (i.e. 300,000+ rows)?

The rows of course will not be perfect duplicates because of the existence of the RowID identity field.

MyTable
-----------
RowID int not null identity(1,1) primary key,
Col1 varchar(20) not null,
Col2 varchar(2048) not null,
Col3 tinyint not null

解决方案

Assuming no nulls, you GROUP BY the unique columns, and SELECT the MIN (or MAX) RowId as the row to keep. Then, just delete everything that didn't have a row id:

DELETE FROM MyTable
LEFT OUTER JOIN (
   SELECT MIN(RowId) as RowId, Col1, Col2, Col3 
   FROM MyTable 
   GROUP BY Col1, Col2, Col3
) as KeepRows ON
   MyTable.RowId = KeepRows.RowId
WHERE
   KeepRows.RowId IS NULL

In case you have a GUID instead of an integer, you can replace

MIN(RowId)

with

CONVERT(uniqueidentifier, MIN(CONVERT(char(36), MyGuidColumn)))

这篇关于如何删除重复行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆