开发者

Deleting Duplicates in Access 2003

开发者 https://www.devze.com 2022-12-08 23:38 出处:网络
I have an Access 2003 table with ~4000 records which was made from 17 different tables. Roughly half of these records are duplicates. There is no unique identifying column (id, name etc). There is an

I have an Access 2003 table with ~4000 records which was made from 17 different tables. Roughly half of these records are duplicates. There is no unique identifying column (id, name etc). There is an id column which was auto filled when the tables were combined 开发者_如何学Cmeaning that the duplicates aren't completely identical (though this column could be removed if it makes things easier).

I have used the Access Find Duplicates Query Wizard which gives me a list of the duplicated records but won't let me delete them (seriously what use is this query if I can't delete them?). I've tried converting the generated query to a remove query but that changes the number of rows that it finds. I'd alter the sql by hand but it's a bit beyond me and is 7 lines long.

Does anyone know a good way of getting rid of the duplicates?


The reason the find duplicates query won't let you delete the records is because it is basically just an aggregate query, it is counting the number of duplicates it finds and returning the cases where the count is greater than 1.

Consider that if you did make a delete query based on the find duplicates, it would delete all rows that have duplicate values, which is maybe not what you want. You want to delete all but one of the duplicates.

You should try to delete all duplicates of a record apart from one, excluding the ID column in your comparison. I suggest the simplest way to do this is to make a make-table query of all the unique values (Select Distinct Field1, Field2... from MyTable) instead for every field except for the ID field, using the results in a to create a new table of around 2000 records (if half are duplicates).

Then, create an ID column on your new table, use an update query to update this ID to the first matching ID in the original table (you could do this using DLookup, which will return the first EXPRESSION value where CRITERIA is true in DOMAIN).

The DLookup() function returns one value from a single field even if more than one record satisfies the criteria. If no record satisfies the criteria, or if the domain contains no records, DLookup() returns a Null.

Since you are identifying the first matching ID based on all the other fields, which are unique values, the unmatched IDs will belong to duplicates. You will be reversing the PK relation, identifying the first matching key given a set of unique fields. After that, you should set the ID to be PK. Of course this assumes the ID has no inherent meaning, and you don't care about keeping one particular ID for a given duplicated row over any of the IDs belonging to the other duplicated rows. This assumes you care about the data in the ID column so you want to preserve it for all remaining rows, otherwise just ignore the DLookup step and do a Select Distinct on all columns apart from the ID.


Use a select with all columns except the ID column:

SELECT DISTINCTROW Column1, Column2, Column3 
INTO MYNEWTABLE
FROM TABLE

You can simply swap the names.

This solution will give you a new table with non duplicates.


The following will preserve original IDs and do it in one step:

DELETE FROM table_with_duplicates 
WHERE table_with_duplicates.id NOT IN 
    (SELECT max(id) 
    FROM table_with_duplicates 
    GROUP BY duplicated_field_1, duplicated_field_2, ...
    )

Now you have the original table with no duplicates and preserved ids. And always remember to backup you data before trying large DELETEs.


DELETE * FROM table_with_duplicates
WHERE table_with_duplicates.ID In 
    (SELECT max(ID) 
     FROM table_with_duplicates 
     GROUP BY [duplicated_field_1] 
     HAVING Count(*)>1
    )


Actually I Found A very simple solution took a while but it all of your fields across are the same like a complete duplicate record then just make one query with every field and sort by "Group BY". Thus the duplicates will combine and you can just append this information to a new table and rename it the same as the existing table. If you have a primary key field you could just ignore it in the query and then it would still combine the data (assuming that you don't care about the data in the primary field). I don't know why no one has mentioned this solution took me 5 hr. to come up with. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消