Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete Duplicates in SQL 2000, SQL 2005, SQL 2008 or higher


Delete Duplicates in SQL 2000, SQL 2005, SQL 2008 or higher

Author
Message
BINU PRASAD.M
BINU PRASAD.M
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 43
-- SQL 2005, SQL 2008 and Higher
WITH XT(i, notes, RowNumber) AS
(
   SELECT i, notes, ROW_NUMBER() OVER (PARTITION BY i, notes ORDER BY i, notes) AS RowNumber
   FROM dbo.temp
)
DELETE FROM XT WHERE RowNumber > 1;
GO

-- SQL 2000 Script
DECLARE @DupCount INT
SET @DupCount = 0
SELECT @DupCount = COUNT(*)
   FROM dbo.temp
   GROUP BY i, notes
   HAVING COUNT(*) > 1

WHILE @DupCount > 0
BEGIN
   SET ROWCOUNT 1

   DELETE dbo.temp
   FROM dbo.temp t1
   JOIN (SELECT i, notes
      FROM dbo.temp
      GROUP BY i, notes
      HAVING COUNT(*) > 1
    ) t2
   ON t1.i = t2.i
   AND t1.notes = t2.notes

   SET ROWCOUNT 0

   SELECT @DupCount = COUNT(*)
   FROM (SELECT *
       FROM dbo.temp
        GROUP BY i, notes
       HAVING COUNT(*) > 1
      Wink tp
END
GO


SELECT * FROM dbo.temp

/*
-- Sample Create Table/Insert Scripts for above queries
--DROP TABLE dbo.temp
CREATE TABLE dbo.temp(i int, notes varchar(100))
GO

INSERT INTO dbo.temp VALUES (1, 'Notes1')
INSERT INTO dbo.temp VALUES (1, 'Notes1')
INSERT INTO dbo.temp VALUES (1, 'Notes1')
INSERT INTO dbo.temp VALUES (2, 'Notes1')
INSERT INTO dbo.temp VALUES (3, 'Notes1')
INSERT INTO dbo.temp VALUES (1, 'Notes2')
INSERT INTO dbo.temp VALUES (2, 'Notes2')
INSERT INTO dbo.temp VALUES (2, 'Notes2')
INSERT INTO dbo.temp VALUES (2, 'Notes3')
INSERT INTO dbo.temp VALUES (4, 'Notes4')
INSERT INTO dbo.temp VALUES (4, 'Notes4')
GO
*/

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search