我有一张名字和日期表。我想删除名称重复的行,而只保留最新的行。有时,信息有多个重复行。
下面的代码可以正常工作,但是我想运行一个自动循环,当不再检测到重复项时停止该循环,或者学习一种更好/更有效的方法。
使用下面的代码,我当前的过程是:
Query1 查询2 Query3
重复直到不再删除任何重复项。
Table1:
ID Field1 Field2 Field3
3 Albert Jacobsen 12/5/2018
5 Mia Shaw 12/28/2018
6 Chris Mantle 6/14/2018
7 Albert Jacobsen 1/8/2019
8 Albert Jacobsen 11/15/2018
9 Chris Mantle 11/24/2018
Query 1:
SELECT Table1.Field1, Table1.Field2, Table1.Field3, Table1.ID INTO Table2
FROM Table1
GROUP BY Table1.Field1, Table1.Field2, Table1.Field3, Table1.ID
ORDER BY Table1.Field1 DESC , Table1.Field2 DESC , Table1.Field3 DESC;
Table2:
Field1 Field2 Field3 ID
Mia Shaw 12/28/2018 5
Chris Mantle 11/24/2018 9
Chris Mantle 6/14/2018 6
Albert Jacobsen 1/8/2019 7
Albert Jacobsen 12/5/2018 3
Albert Jacobsen 11/15/2018 8
Query 2:
SELECT Table2.Field1, Table2.Field2, Count(Table2.ID) AS CountOfID,
Min(Table2.ID) AS MinOfID INTO Temp_DeleteThese
FROM Table2
GROUP BY Table2.Field1, Table2.Field2
HAVING (((Count(Table2.ID))>1));
Table Temp_DeleteThese:
Field1 Field2 CountOfID MinOfID
Albert Jacobsen 3 3
Chris Mantle 2 6
Query 3:
DELETE DISTINCTROW Table1.*
FROM Temp_DeleteThese INNER JOIN Table1 ON Temp_DeleteThese.MinofID =
Table1.ID;
Resulting Table1:
ID Field1 Field2 Field3
5 Mia Shaw 12/28/2018
7 Albert Jacobsen 1/8/2019
8 Albert Jacobsen 11/15/2018
9 Chris Mantle 11/24/2018
如何循环代码,直到删除重复项并且仅保留最近的记录,或者这样做更有效?
答案 0 :(得分:1)
您可以使用EXISTS子查询来删除单个查询中的所有重复记录,以确保存在具有相同名称和更新日期的行:
DELETE *
FROM Table1 t
WHERE EXISTS(
SELECT 1
FROM Table1 s
WHERE s.Field1 = t.Field1
AND s.Field2 = t.Field2
AND s.Field3 > t.Field3
)
这将删除应一次性删除的所有行。我认为您不会比这更有效。