我每周需要更新2个表。我将在文件中复制一个唯一标识符,我需要根据另一个字段的Max值删除重复项,但是,可能有最大字段的重复。
TrackID | GrossOpens (Amoung other fields...)
1 | 2
1 | 1
2 | 5
2 | 10
2 | 10
3 | 3
我能够获取我的数据以获得以下结果:
TrackID | GrossOpens
1 | 2
2 | 10
2 | 10
3 | 3
我需要具有匹配" GrossOpens"的重复项。在每组中删除。我使用以下代码来获得adove结果:
DELETE *
FROM NL_2015 AS NL
WHERE EXISTS ( SELECT * FROM NL_2015 DL WHERE DL.TrackID = NL.TrackID AND (DL.GrossOpens > NL.GrossOpens OR (DL.GrossOpens = NL.GrossOpens AND DL.TrackID > NL.TrackID)));
答案 0 :(得分:2)
使用此查询创建行号,所有重复项的行号都优于1.
WITH dupes
AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY TrackID ORDER BY grossopens DESC) row
FROM NL_2015)
DELETE FROM dupes
WHERE row > 1
编辑没有WITH
的查询:
DELETE FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY TrackID ORDER BY grossopens DESC) row
FROM NL_2015
)
WHERE
row > 1