de复制访问数据库

时间:2016-12-15 18:21:15

标签: sql ms-access

我有一个非常大的表名为runners,它有大约50,00个重复实例,我想删除重复项。 经过一些实验,我得到了一个突出重复的查询。

然后我尝试将一个名为'del'的新字段设置为true,然后我可以放心地删除。我尝试了一个成功查询的连接,但是我收到一条错误消息“更新语法错误”并依赖于“ UPDATE“

我的sql看起来像这样:

`Update History A
INNER JOIN 
(
SELECT Max(History.rDate) AS lastRun, History.Runner, History.rtime, History.source, Count(History.rdate) AS occurrs, History.rDate, History.audittime
FROM History
GROUP BY History.Runner, History.rtime, History.source, History.rDate, History.audittime
HAVING (((Count(1))>1)) 
)B

SET A.del =-1 
ON (( A.rdate =B.rdate) AND (A.Runner =B.runner))
WHERE  A.audittime < B.audittime 
;`

任何想法或建议都会有很大帮助。感谢

1 个答案:

答案 0 :(得分:0)

使用相关子查询考虑直接DELETE语句。不太确定您的数据,但下面删除了 Runner Source 分组中最高的 rdate 字段(因为这两个字段似乎是唯一的标识符)除了日期时间)。

DELETE 
FROM History main
WHERE main.rDate IN 
   (SELECT Max(h.rDate) AS lastRun
    FROM History h
    WHERE h.Runner = main.Runner AND h.source = main.source           
    HAVING Count(1) > 1)

现在,如果有两个以上的重复项,您可能需要反复运行此查询,直到不进行任何更改。或者,运行以下DELETE,只保留最小 rDate

DELETE 
FROM History main
WHERE NOT main.rDate IN 
   (SELECT Min(h.rDate) AS lastRun
    FROM History h
    WHERE h.Runner = main.Runner AND h.source = main.source)
AND 
   (SELECT Count(*)
    FROM History h
    WHERE h.Runner = main.Runner AND h.source = main.source) > 1