Pages

Monday, January 3, 2011

Delete duplicate rows from the table

Create table ##Test (a int not null, b int not null, c int not null, id int not null identity) on [Primary]
GO
INSERT INTO ##Test (A,B,C) VALUES (1,1,1)
INSERT INTO ##Test (A,B,C) VALUES (1,1,1)
INSERT INTO ##Test (A,B,C) VALUES (1,1,1)

INSERT INTO ##Test (A,B,C) VALUES (1,2,3)
INSERT INTO ##Test (A,B,C) VALUES (1,2,3)
INSERT INTO ##Test (A,B,C) VALUES (1,2,3)

INSERT INTO ##Test (A,B,C) VALUES (4,5,6)
GO
Select * from ##Test
GO
Delete from ##Test where id <
(Select Max(id) from ##Test t where ##Test.a = t.a and
##Test.b = t.b and
##Test.c = t.c)
GO
Select * from ##Test
drop table ##Test
GO

No comments:

Post a Comment