Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Removal and delete of duplicate records in my table

Removal and delete of duplicate records in my table

Scheduled Pinned Locked Moved Database
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goldsoft
    wrote on last edited by
    #1

    hi i have this table: id | Name | Age ================== 1 | AAA | 22 1 | AAA | 22 2 | BBB | 33 2 | BBB | 33 2 | BBB | 33 3 | CCC | 44 4 | DDD | 55 i need to delete from this table all the duplicate records and leave only one record. the table will looks like this: id | Name | Age ================== 1 | AAA | 22 2 | BBB | 33 3 | CCC | 44 4 | DDD | 55 i work with sqlCE for Mobile thanks

    M R 2 Replies Last reply
    0
    • G goldsoft

      hi i have this table: id | Name | Age ================== 1 | AAA | 22 1 | AAA | 22 2 | BBB | 33 2 | BBB | 33 2 | BBB | 33 3 | CCC | 44 4 | DDD | 55 i need to delete from this table all the duplicate records and leave only one record. the table will looks like this: id | Name | Age ================== 1 | AAA | 22 2 | BBB | 33 3 | CCC | 44 4 | DDD | 55 i work with sqlCE for Mobile thanks

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      This is very much a solved problem, the minimum of research [^] would get you plenty of examples

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • G goldsoft

        hi i have this table: id | Name | Age ================== 1 | AAA | 22 1 | AAA | 22 2 | BBB | 33 2 | BBB | 33 2 | BBB | 33 3 | CCC | 44 4 | DDD | 55 i need to delete from this table all the duplicate records and leave only one record. the table will looks like this: id | Name | Age ================== 1 | AAA | 22 2 | BBB | 33 3 | CCC | 44 4 | DDD | 55 i work with sqlCE for Mobile thanks

        R Offline
        R Offline
        RNA Team
        wrote on last edited by
        #3

        Try this

        CREATE TABLE tblTest (ID INT, NAME VARCHAR(20),AGE INT)
        INSERT INTO tblTest VALUES(1,'AAA',22),(1,'AAA',22),(2,'BBB',33),(2,'BBB',33),(2,'BBB',33),(3,'CCC',44),(4,'DDD',55)

        --create an alternate table to store the duplicate records
        CREATE TABLE tblTestDuplicate (ID INT, NAME VARCHAR(20),AGE INT)

        -- insert those duplicate records to this new table
        INSERT INTO tblTestDuplicate
        SELECT *
        FROM tblTest
        GROUP BY ID,NAME,AGE
        HAVING COUNT(ID) > 1

        --delete the duplicate records from the original table
        DELETE FROM
        tblTest
        WHERE ID IN (SELECT ID FROM tblTestDuplicate)

        --insert all the records into the original table
        INSERT INTO tblTest
        SELECT *
        FROM tblTestDuplicate

        --Project the new records
        SELECT *
        FROM tblTest
        ORDER BY ID

        --clean up
        DROP TABLE tblTestDuplicate
        DROP TABLE tblTest

        /*

        ID NAME AGE
        1 AAA 22
        2 BBB 33
        3 CCC 44
        4 DDD 55

        */ Hope this helps

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups