从自我加入中删除

DELETE FROM SELF JOIN

我正在尝试删除日期较早的重复记录。我刚刚上传了一些日期较晚的复制品,只想保留它们。

以下是我的代码

DELETE FROM 
    Config 
WHERE
(
SELECT 
    * 
FROM 
    Config one
INNER JOIN 
    Config two
ON 
    one.name = two.name 
    and 
    one.product = two.product
WHERE 
    (one.name LIKE '4%' OR one.Name LIKE '7%' OR one.Name LIKE '9%') 
    AND 
    (one.date < two.date)
)

我得到这个真是太棒了。

编辑:添加 Table 架构

ConfigID GUID UNIQUE IDENTIFIER
name     varchar(100),
Product  varchar(50),
Amount   smallint,
Date     datetime

我认为应该这样做:

delete one
FROM 
    Config one
    INNER JOIN 
        Config two
    ON 
        one.name = two.name 
        and one.product = two.product
    WHERE 
        (one.name LIKE '4%' OR one.Name LIKE '7%' OR one.Name LIKE '9%') 
        AND (one.date < two.date)

试试这个:

;WITH CTE AS(
    SELECT 
        *, Rn = ROW_NUMBER() OVER(PARTITION BY Product, Name ORDER BY [Date] DESC)
    FROM Config
    WHERE
        name  LIKE '4%'
        OR name  LIKE '7%'
        OR name  LIKE '9%'
)
DELETE FROM CTE WHERE Rn > 1