7

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

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

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

提前致谢 :)

4

4 回答 4

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
于 2010-07-27T15:51:55.220 回答
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

如何删除重复行中窃取?

更新:

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

于 2010-07-27T15:40:16.337 回答
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,这应该会让你朝着正确的方向前进。

于 2010-07-27T15:44:12.243 回答
0

首先使用一个SELECT... INTO

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

放下第一张桌子。

于 2010-07-27T15:37:37.347 回答