How to remove duplicate rows and keep one in an Access database?

有些话、适合烂在心里 提交于 2021-02-05 11:23:43

问题


I need to remove duplicate rows in my Access database, does anyone have generic query to do this? As I have this problem with multiple tables


回答1:


There are two things you need to do,

  1. Determine what the criteria are for a unique record - what is the list of columns where two, or more, records would be considered duplicates, e.g. JobbID and HisGuid
  2. Decide what you want to do with the duplicate records - do you want to hard delete them, or set the IsDeleted flag that you have on the table

Once you've determined the criteria that you want to use for uniqueness you then need to pick 1 record from each group of duplicates to retain. A query along the lines of:

SELECT  MAX(ID)
FROM    MyTable
GROUP
BY      JobbID, HisGuid

Will give you (and I've assumed that the ID column is an auto-increment/identity column that is unique across all records in the table) the highest value for each group of records where JobbID and HisGuid are both the same. You could use MIN(ID) if you want, it's up to you - you just need to pick ONE record from each group to keep.

Assuming that you want to set the IsDeleted flag on the records you don't want to keep, you can then incorporate this into an update query:

UPDATE  MyTable
SET     IsDeleted = 1
WHERE   ID NOT IN
(
    SELECT  MAX(ID)
    FROM    MyTable
    GROUP
    BY  JobbID, HisGuid
)

This takes the result of the query that retrieves the highest IDs and uses it to say set IsDeleted to 1 for all the records where the ID isn't the highest ID for each group of records where JobbID and HisGuid are the same.

The only part I can't help you with is running these queries in Access as I don't have it installed on the PC I'm using right now and my memory is a bit rusty regarding how/where to run arbitrary queries.



来源:https://stackoverflow.com/questions/52774206/how-to-remove-duplicate-rows-and-keep-one-in-an-access-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!