仅基于SQL中的条件删除表中的重复项

时间:2012-09-20 15:14:36

标签: sql duplicates sql-delete

假设我有一个包含列的表:

CustomerNumber
Lastname
Firstname
PurchaseDate

...以及其他未在此处显示的问题中未更改任何内容的列。

在此表中,我可以为同一个客户提供多行,具有不同的购买日期(我知道,设计不佳......我只是试图解决报告问题,而不是真正尝试解决问题的根源)。

在SQL中,如何为每位客户保留一条记录,并删除其余的日期? group by似乎不适用于我的案例

3 个答案:

答案 0 :(得分:6)

;with a as
(
select row_number() over (partition by CustomerNumber, Lastname, Firstname order by PurchaseDate desc) rn
from <table>
)
delete from a where rn > 1

答案 1 :(得分:3)

这对我有用(在DB2上):

 DELETE FROM my_table 
 WHERE (CustomerNumber, Lastname, Firstname, PurchaseDate) 
 NOT IN ( 
       SELECT CustomerNumber, Lastname, Firstname, MAX(PurchaseDate) 
       FROM my_table 
       GROUP BY CustomerNumber, Lastname, FirstName 
 )

答案 2 :(得分:1)

SELECT CustomerNumber, Lastname, Firstname, MAX(PurchaseDate) LatestPurchaseDate
FROM Table
GROUP BY CustomerNumber, Lastname, Firstname

MAX将选择最高(最新)日期,并显示GROUP BY列的每个唯一组合的日期。

编辑:我误解了你想删除除最近购买日期以外的所有记录。

WITH Keep AS
(
    SELECT CustomerNumber, Lastname, Firstname, MAX(PurchaseDate) LatestPurchaseDate
    FROM Table
    GROUP BY CustomerNumber, Lastname, Firstname
)
DELETE FROM Table
WHERE NOT EXISTS
(
    SELECT *
    FROM Keep
    WHERE Table.CustomerNumber = Keep.CustomerNumber
    AND Table.Lastname = Keep.Lastname
    AND Table.Firstname = Keep.Firstname
    AND Table.PurchaseDate = Keep.LastPurchaseDate
)