从表格中删除'排名' > 1

时间:2015-01-27 03:09:32

标签: sql sql-server conditional rank sql-delete

我是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的记录的最佳方法是什么?

3 个答案:

答案 0 :(得分:2)

从它的外观来看,您要删除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

答案 1 :(得分:1)

您可以使用CTE:

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

答案 2 :(得分:1)

您可以使用更简单的方法返回结果集(如果您不想使用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,你可以使用那个也可以整理,最近的日期

谢谢