Friday, 6 February 2015

Delete Duplicate Record from a table


Delete Duplicate Record from a table


  1. Declare a temporary table  @table.
  2. Create two fields 'id' as int identity and 'data' as varchar.
  3. Insert four records with values 'not duplicate row', 'duplicate row' and 'duplicate row'.
  4. Create a while loop with a condition 1=1 and break the loop if zero row count found means no duplicate record exists in the table now.
  5. In the query we have used 'GROUP BY' and 'COUNT' aggregate function to calculate the duplicate records.
  6. 'GROUP BY' is used for grouping data values having count more than 1.
  7. Selecting MAX(id) means getting id of higher number. 
  8.  When while loop will run first time it will group three duplicate records with values 'duplicate row' and get its max id like here max id will be 4 and delete the fourth record.
  9. In WHILE loop it calculate the Row count by '@@Rowcount' but here row count is not equal to zero than again while loop will run and delete the third record and again row count not equal to zero.
  10. While loop run again but not able to get any duplicate row. This time row count will be 0 and it will break the loop and come out.


DECLARE @table TABLE(
      id INT IDENTITY(1, 1)
    , data VARCHAR(20)
    )

INSERT INTO @table VALUES ('not duplicate row')
INSERT INTO @table VALUES ('duplicate row')
INSERT INTO @table VALUES ('duplicate row')
INSERT INTO @table VALUES ('duplicate row')

SELECT  * FROM @table

WHILE 1 = 1
   BEGIN
      DELETE   FROM @table
      WHERE    id IN (SELECT   MAX(id)
                        FROM     @table
                        GROUP BY data
                        HAVING   COUNT(*) > 1)

      IF @@Rowcount = 0
         BREAK ;

   END

SELECT  * FROM @table
  

No comments:

Post a Comment