如何删除完全重复的行

时间:2010-07-27 15:32:50

标签: sql sql-server tsql sql-server-2008 duplicate-removal

假设我的表中有重复的行,而且我的数据库设计是第3类: -

Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (1,'Cinthol','cosmetic soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (1,'Cinthol','cosmetic soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (1,'Cinthol','cosmetic soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (1,'Lux','cosmetic soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (1,'Crowning Glory','cosmetic soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (2,'Cinthol','nice soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (3,'Lux','nice soap','soap');
Insert Into tblProduct (ProductId,ProductName,Description,Category) Values (3,'Lux','nice soap','soap');

我希望每个表中只有1个实例存在于我的表中。因此应删除2nd, 3rd and last row完全相同的{{1}}。我可以为此写什么查询?可以在不创建临时表的情况下完成吗?只需一个查询?

提前致谢:)

4 个答案:

答案 0 :(得分:18)

试试这个 - 它会删除你桌子上的所有重复项:

;WITH duplicates AS
(
    SELECT 
       ProductID, ProductName, Description, Category,
       ROW_NUMBER() OVER (PARTITION BY ProductID, ProductName
                          ORDER BY ProductID) 'RowNum'
    FROM dbo.tblProduct
)
DELETE FROM duplicates
WHERE RowNum > 1
GO

SELECT * FROM dbo.tblProduct
GO

你的副本现在应该消失了:输出是:

ProductID   ProductName   DESCRIPTION        Category
   1          Cinthol         cosmetic soap      soap
   1          Lux             cosmetic soap      soap
   1          Crowning Glory  cosmetic soap      soap
   2          Cinthol         nice soap          soap
   3          Lux             nice soap          soap

答案 1 :(得分:4)

DELETE tblProduct 
FROM tblProduct 
LEFT OUTER JOIN (
   SELECT MIN(ProductId) as ProductId, ProductName, Description, Category
   FROM tblProduct 
   GROUP BY ProductName, Description, Category
) as KeepRows ON
   tblProduct.ProductId= KeepRows.ProductId
WHERE
   KeepRows.ProductId IS NULL

How can I remove duplicate rows?

被盗

<强>更新

这仅在ProductId是主键(不是主键)时才有效。你最好使用@marc_s'方法,但我会留下这个以防万一使用PK的人遇到这篇文章。

答案 2 :(得分:1)

几个星期后我不得不这样做...你使用的是什么版本的SQL Server?在SQL Server 2005及更高版本中,您可以使用Row_Number作为选择的一部分,并且只选择Row_Number为1的位置。我忘记了确切的语法,但是它有详细记录......有些内容如下:

Select t0.ProductID, 
       t0.ProductName, 
       t0.Description, 
       t0.Category
Into   tblCleanData
From   (
    Select ProductID, 
           ProductName, 
           Description, 
           Category, 
           Row_Number() Over (
               Partition By ProductID, 
                            ProductName, 
                            Description, 
                            Category
               Order By     ProductID,
                            ProductName,
                            Description,
                            Category
           ) As RowNumber
    From   MyTable
) As t0
Where t0.RowNumber = 1

查看http://msdn.microsoft.com/en-us/library/ms186734.aspx,这会让你朝着正确的方向前进。

答案 3 :(得分:0)

首先使用SELECT... INTO

SELECT DISTINCT ProductID, ProductName, Description, Category
    INTO tblProductClean
    FROM tblProduct

放下第一张桌子。