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

前端 未结 1 1090
北荒
北荒 2021-01-27 06:35

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条回答
  • 2021-01-27 07:12

    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.

    0 讨论(0)
提交回复
热议问题