我尝试进行一次查询,删除Access 2010表中的所有重复行。我的问题是查询正在删除所有重复记录(甚至是原始记录)。
所以,让我说我有1条原始记录和2条重复记录。它不删除2个重复项,而是删除所有3个。
这是我做的:
DELETE *
FROM Table
WHERE [field1] IN (SELECT [field1] FROM [table] As Tmp
GROUP BY [field1],[field2],[field3],[field4],[field5] HAVING Count(*)>1);
主键字段为(ID)
答案 0 :(得分:3)
访问永远不会允许您在DELETE
查询中GROUP BY
。所以你需要一种根本不同的方法。
创建一个查询,该查询仅为每个重复的字段组返回一行:
SELECT [field1], [field2], [field3], [field4], [field5], Min([ID]) AS keep_id
FROM [table]
GROUP BY [field1], [field2], [field3], [field4], [field5];
将其另存为 qryKeepIDs ,然后使用DELETE
查询:
DELETE FROM [Table]
WHERE [Table].ID Not In (SELECT keep_id FROM qryKeepIDs);
答案 1 :(得分:1)
以下代码将生成与@HansUp提到的相同的查询。如果您想检查许多不同的表以查找重复项,这应该会带来优势。除了删除" all"你甚至有机会删除一些重复项。
提出改进意见会很棒:
Public Sub CleanUpDuplicates(strTable As String, strQuery, strID As String, strField1 As String, _
Optional strField2 As String, _
Optional strField3 As String, _
Optional strField4 As String, _
Optional strField5 As String)
strQueryDUP = strQuery & "_DUP"
strQueryCleanUp_Single = strQuery & "_CleanUp_Single"
strQueryCleanUp_All = strQuery & "_CleanUp_All"
'SELECT [field1], [field2], [field3], [field4], [field5], Min([ID]) AS keyID
strSQLDUP = "SELECT [" & strField1
If strField2 <> "" Then strSQLDUP = strSQLDUP + "], [" & strField2
If strField3 <> "" Then strSQLDUP = strSQLDUP + "], [" & strField3
If strField4 <> "" Then strSQLDUP = strSQLDUP + "], [" & strField4
If strField5 <> "" Then strSQLDUP = strSQLDUP + "], [" & strField5
strSQLDUP = strSQLDUP & "], Min(" & strID & ") AS keyID "
'FROM [Table]
strSQLDUP = strSQLDUP & "FROM " & strTable & " "
'GROUP BY [field1], [field2], [field3], [field4], [field5];
strSQLDUP = strSQLDUP & "GROUP BY [" & strField1
If strField2 <> "" Then strSQLDUP = strSQLDUP + "], [" & strField2
If strField3 <> "" Then strSQLDUP = strSQLDUP + "], [" & strField3
If strField4 <> "" Then strSQLDUP = strSQLDUP + "], [" & strField4
If strField5 <> "" Then strSQLDUP = strSQLDUP + "], [" & strField5
strSQLDUP = strSQLDUP & "];"
'FROM [Table]
strSQLCleanUp = "FROM " & strTable & " "
'WHERE [Table].ID Not In (SELECT keep_id FROM [Query]);
strSQLCleanUp = strSQLCleanUp & "WHERE " & strTable & "." & strID & " Not In (SELECT keyID FROM "
strSQLCleanUp = strSQLCleanUp & strQueryDUP & ");"
'SELECT [Table].*
strSQLCleanUp_Single = "SELECT " & strTable & ".* " & strSQLCleanUp
'DELETE [Table].*
strSQLCleanUp_All = "DELETE " & strTable & ".* " & strSQLCleanUp
'###### Create Queries ##########
Dim db As DAO.Database
Set db = CurrentDb
Dim qdfDUP As DAO.QueryDef
Dim qdfCleanUp_Single As DAO.QueryDef
Dim qdfCleanUp_All As DAO.QueryDef
On Error Resume Next
DoCmd.DeleteObject acQuery, strQueryDUP
Set qdfDUP = db.CreateQueryDef(strQueryDUP, strSQLDUP)
DoCmd.DeleteObject acQuery, strQueryCleanUp_Single
Set qdfCleanUp_Single = db.CreateQueryDef(strQueryCleanUp_Single, strSQLCleanUp_Single)
DoCmd.DeleteObject acQuery, strQueryCleanUp_All
Set qdfCleanUp_All = db.CreateQueryDef(strQueryCleanUp_All, strSQLCleanUp_All)
On Error GoTo 0
DoCmd.OpenQuery strQueryCleanUp_Single, acViewNormal
response = MsgBox("Do you like to clean-up all duplicates?", vbOKCancel + vbCritical, "Attention")
If response = vbOK Then
DoCmd.Close acQuery, strQueryCleanUp_Single
DoCmd.OpenQuery strQueryCleanUp_All, acViewNormal
End If
End Sub
答案 2 :(得分:0)
我们可以使用此查询删除重复项并保留唯一的查询,例如。找到3个重复项,它保留1并删除其他2个。
Delete * From myTable Where UniqueID Not IN (select UniqueID From myTable Where UniqueID IN (select Min(UniqueID) From myTable Group by [Duplicates]))
谢谢。