我有一个如下所示的表格结构
FileId FileName CreatedBy CreationDate
------- ---------- ----------- ------------
File1_1 File1 User1 2014/09/12 17:10:49
File1_2 File1 User1 2014/09/11 17:45:49
File2_1 File2 User2 2014/05/11 17:45:22
File2_3 File2 User1 2014/08/11 17:47:22
我想编写一个查询,它将获取名称中包含重复条目的文件,并删除所有条目但是具有最新日期的条目。
例如,在当前示例中,它将删除标识为File1_2
和File2_1
答案 0 :(得分:2)
从SELECT
查询开始,该查询标识要删除的行。
SELECT y.CreatedBy, y.FileId, y.FileName, y.CreationDate
FROM YourTable AS y
WHERE
y.CreationDate <
DMax(
"CreationDate",
"YourTable",
"FileName='" & y.FileName & "'"
);
验证查询是否标识了正确的行后,将其转换为DELETE
查询。
DELETE
FROM YourTable AS y
WHERE
y.CreationDate <
DMax(
"CreationDate",
"YourTable",
"FileName='" & y.FileName & "'"
);
答案 1 :(得分:0)
DELETE TableA
FROM TableA A
INNER JOIN (
SELECT MAX(creationDate) as dt, FileId FROM TableA
GROUP by FileId
HAVING count(*) > 1
) T
where A.FileId = T.FileId
and A.dt < T.dt