Thursday, 25 October 2012

deleting duplicates.

CREATE TABLE DUPTest (A int not null,
B int not null,
C int not null,
ID int not null identity) on [Primary]
GO
INSERT INTO DUPTest (A,B,C) VALUES (1,1,1)
INSERT INTO DUPTest (A,B,C) VALUES (1,1,1)
INSERT INTO DUPTest (A,B,C) VALUES (1,1,1)

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

INSERT INTO DUPTest (A,B,C) VALUES (4,5,6)
GO
Select * from DUPTest
GO











Delete from DUPTest where ID <
(Select Max(id) from DUPTest t where DUPTest.A = t.A and
DUPTest.B = t.B and
DUPTest.C = t.C)
GO
Select * from DUPTest
GO










No comments:

Post a Comment