Friday, May 10, 2013

Delete duplicate record from Table SQL

Create Table :


CREATE Table [Order]
(
ID INT Primary key NOT NULL IDENTITY(1,1),
NAME NVARCHAR(MAX),
Quantity INT,
Price INT
)

Insert duplicate record into the table:


INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('SIBASIS',1,10)
INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('SIBASIS',1,1)
INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('SIBASIS',1,2)
INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('SIBASIS',1,3)
INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('SIBASIS',1,11)

INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('Manua',1,10)
INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('Manua',1,10)
INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('Manua',1,10)

INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('Kalia',1,1)
INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('Kalia',1,1)
INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('Tiki',1,1)
INSERT INTO [ORDER](NAME,Quantity,Price) VALUES('Tiki',1,1)

Delete Duplicate record:

DELETE FROM [Order] WHERE ID NOT IN (SELECT MAX(ID) FROM [Order] GROUP BY NAME)


No comments:

Post a Comment