4

考虑以下:

ProductID  Supplier
---------  --------
111        Microsoft
112        Microsoft
222        Apple Mac
222        Apple
223        Apple

在此示例中,产品 222 被重复,因为供应商在所提供的数据中被称为两个名称。

我有成千上万种产品的此类数据。如何删除重复的产品或选择单个结果 - 例如使用 SELECT TOP 1 进行自我加入或类似的东西?

谢谢!

4

3 回答 3

4
DELETE  a
FROM    tableName a
        LEFT JOIN
        (
            SELECT  Supplier, MIN(ProductID) min_ID
            FROM    tableName
            GROUP   BY Supplier
        ) b ON  a.supplier = b.supplier AND
                a.ProductID = b.min_ID
WHERE   b.Supplier IS NULL

或者如果你想删除productID比 onbe 产品更多的产品

WITH cte 
AS
(
    SELECT  ProductID, Supplier,
            ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY Supplier) rn
    FROM    tableName
)
DELETE FROM cte WHERE rn > 1
于 2013-02-15T00:57:58.243 回答
4

我认为您想要执行以下操作:

select t.*
from (select t.*,
             row_number() over (partition by product_id order by (select NULL)) as seqnum
      from t
     ) t
where seqnum = 1

这将为每个产品选择任意行。

要删除除一个以外的所有行,您可以使用相同的想法:

with todelete (
      (select t.*,
               row_number() over (partition by product_id order by (select NULL)) as seqnum
        from t
      )
delete from to_delete where seqnum > 1
于 2013-02-15T01:02:15.267 回答
1
;WITH Products_CTE AS
    (
    SELECT ProductID, Supplier,
        ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY <some value>) as rn
    FROM PRODUCTS
    )

SELECT *
FROM Products_CTE
WHERE rn = 1

some value将是决定您保留哪个供应商版本的关键。如果您想要供应商的第一个实例,您可以使用 DateAdded 列(如果存在)。

于 2013-02-15T01:01:14.987 回答