考虑以下内容:
ProductID Supplier
--------- --------
111 Microsoft
112 Microsoft
222 Apple Mac
222 Apple
223 Apple
在此示例中,重复产品222,因为供应商在所提供的数据中称为两个名称。
我有数千种产品的数据。如何删除重复的产品或选择单个结果 - 类似于SELECT TOP 1的自联接或类似的东西?
谢谢!
答案 0 :(得分: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
WITH cte
AS
(
SELECT ProductID, Supplier,
ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY Supplier) rn
FROM tableName
)
DELETE FROM cte WHERE rn > 1
答案 1 :(得分: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
答案 2 :(得分: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列(如果存在)。