An Idea can change your life.....

Saturday, February 06, 2010

Delete Duplicate Records

___________________________________________________________


CREATE TABLE dbo.car_info
(
Car_Sl_No INT NOT NULL,
CarCompany NVARCHAR (max) NULL,
CarBodyType NVARCHAR (max) NULL,
CarName NVARCHAR (max) NULL,
EngineType NVARCHAR (max) NULL
)
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)VALUES (1, 'Maruti ', 'small ', 'Maruti-800 ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (2, 'Honda ', 'sedan ', 'City ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (3, 'Maruti ', 'small ', 'Maruti-800 ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (4, 'Maruti ', 'small ', 'Waganor Duo ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (5, 'Honda ', 'sedan ', 'City ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (6, 'TATA ', 'small ', 'indica ', 'diesel')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (7, 'Mahindra ', 'SUV ', 'Scorpio ', 'diesel')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (8, 'TATA ', 'SUV ', 'Sumo ', 'diesel')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (9, 'Maruti ', 'sedan ', 'SX4 ', 'petrol')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (10, 'Maruti ', 'sedan ', 'Swift-Dzire ', 'diesel')
GO
INSERT INTO dbo.car_info (Car_Sl_No, CarCompany, CarBodyType, CarName, EngineType)
VALUES (11, 'TATA ', 'small ', 'Nano ', 'petrol')
GO
______________________________________________________________________________

Delete duplicate records based on the company name only
delete from dbo.car_info
where exists
(select Car_Sl_No from dbo.car_info s
where s.CarCompany = dbo.car_info.CarCompany and s.Car_Sl_No < dbo.car_info.Car_Sl_No)