从 Table 中删除 'RANK' > 1

DELETE FROM Table WHERE 'RANK' > 1

我是 RANKCTE 查询的新手。以下是我的查询:

SELECT 
    *
    ,ROW_NUMBER() OVER(PARTITION BY name, Product ORDER BY Date desc) AS 'RANK'
FROM 
    Config 
WHERE 
    Name 
    IN
    (SELECT 
        name
    FROM 
        Config
    GROUP BY 
        Name
        ,Product
        ,Amount
        ,Date 
    HAVING 
        COUNT(name) >1) 
        AND
        Product 
        IN 
        (SELECT 
            Product
        FROM 
            Config
        GROUP BY 
            Name
            ,Product
            ,Amount
            ,Date
        HAVING 
             COUNT(name) >1)

以下为样本结果,满足条件:

ID|name|Product|Amount|Date      |RANK|
---------------------------------------
1 |a   |0000   |1     |2015-01-01| 1  |
2 |a   |0000   |1     |2015-01-01| 2  |
3 |a   |1111   |0     |2015-01-01| 1  |
4 |a   |1111   |0     |2015-01-01| 2  |
5 |b   |2222   |1     |2015-01-01| 1  |
6 |b   |2222   |1     |2015-01-01| 2  |
7 |b   |3333   |0     |2015-01-01| 1  |
8 |b   |3333   |0     |2015-01-01| 2  |

DELETE 满足RANK > 1 的记录的最佳方法是什么?

您可以使用 CTE:

with todelete as (<your query here>)
delete from todelete
    where [rank] = 1;

从表面上看,您想要删除 NameProduct 的重复条目,留下最新的条目。您可以通过以下方式简化您的查询:

SELECT
    *,
    RN = ROW_NUMBER() OVER(PARTITION BY Name, Product ORDER BY [Date] DESC)
FROM Config

然后您可以将其放入 CTE 并执行 DELETE

CREATE TABLE Config(
    ID      INT,
    Name    VARCHAR(10),
    Product VARCHAR(10),
    Amount  INT,
    [Date]  DATE
)
INSERT INTO Config VALUES
(1, 'a', '0000', 1, '2015-01-01'),
(2, 'a', '0000', 1, '2015-01-01'),
(3, 'a', '1111', 0, '2015-01-01'),
(4, 'a', '1111', 0, '2015-01-01'),
(5, 'b', '2222', 1, '2015-01-01'),
(6, 'b', '2222', 1, '2015-01-01'),
(7, 'b', '3333', 0, '2015-01-01'),
(8, 'b', '3333', 0, '2015-01-01');

;WITH CTE AS(
    SELECT
        *,
        RN = ROW_NUMBER() OVER(PARTITION BY Name, Product ORDER BY [Date] DESC)
    FROM Config
)
DELETE FROM CTE WHERE RN > 1

SELECT * FROM Config

您可以根据需要使用更简单的方法 return 结果集(如果您不想为此使用 Row_nimber() 和 CTE :))

您想按名称和产品明智地获取最近的日期。

select c1.Name,c1.Product,c1.Amount,max(c1.Date) as Date from Config c1
group by c1.Name,c1.Product,c1.Amount

另一种方法是使用 CROSS APPLY,您也可以使用它来整理最近的日期

谢谢