Results 1 to 2 of 2
  1. #1
    NotSoAccess is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2013
    Posts
    1

    SQL code to delete duplicate records (but to keep one copy)


    I have a table that has 1335 records in, and columns names, Title, First Name, Surname, Address, Address2, Town/City, County, Postcode.

    I want to know how to write a SQL code, which will locate all the duplicates that match the First Name, Surname and Postcode and delete them, but keeping 1 copy of the duplicate.

    I have created a query to locate the duplicate records, and I have put the SQL code there. I tried changing this, however it would delete all the records, so I am not to sure what to try...

    SELECT SurnameSearch.Title, SurnameSearch.[First Name], SurnameSearch.Surname, SurnameSearch.Address, SurnameSearch.Address2, SurnameSearch.[Town/City], SurnameSearch.County, SurnameSearch.Postcode
    FROM SurnameSearch
    WHERE (((SurnameSearch.[First Name]) In (SELECT [First Name] FROM [SurnameSearch] As Tmp GROUP BY [First Name],[Surname],[Postcode] HAVING Count(*)>1 And [Surname] = [SurnameSearch].[Surname] And [Postcode] = [SurnameSearch].[Postcode])))
    ORDER BY SurnameSearch.[First Name], SurnameSearch.Surname, SurnameSearch.Postcode;

  2. #2
    qa12dx is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    india
    Posts
    112
    i don't know if this is the best way to do it, but how i do it is;

    - if ur table doesn't have a autonumber field, add one and also add a temp - yes/no field
    - use ur sql above in a update query, adding max([autonumber field]), and update the temp field with -1.
    - now have a delete query delete where temp field = -1
    - if u ve more than 1 dup of a record run the two queries again in the same order

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 07-11-2012, 10:13 PM
  2. Replies: 2
    Last Post: 04-13-2012, 12:53 AM
  3. Delete Duplicate Records Based on Criteria
    By chadd in forum Queries
    Replies: 2
    Last Post: 02-07-2012, 04:24 PM
  4. Replies: 1
    Last Post: 05-21-2010, 02:22 PM
  5. Delete duplicate records
    By Zukster in forum Queries
    Replies: 1
    Last Post: 08-26-2009, 03:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums