Groups | Blog | Home
all groups > sql server programming > february 2007 >

sql server programming : delete duplicate record in table


vipin
2/22/2007 8:11:01 PM
hi,
i want to delete all the duplicate record in table
Pls give me query
David Portas
2/22/2007 11:22:57 PM
[quoted text, click to view]

Your table should have a key to prevent duplicates. Example:

CREATE TABLE t1 (x INT NOT NULL, z INT NOT NULL /* NO KEY!! */);

INSERT INTO t1 (x,z) VALUES (1,1);
INSERT INTO t1 (x,z) VALUES (2,2);
INSERT INTO t1 (x,z) VALUES (2,2);
INSERT INTO t1 (x,z) VALUES (3,3);

WITH t AS
(SELECT x,z,ROW_NUMBER()
OVER (PARTITION BY x,z ORDER BY x,z) AS r
FROM t1)
DELETE FROM t
WHERE r>1;

SELECT x,z FROM t1;

-- Add a key
ALTER TABLE t1 ADD CONSTRAINT pk_t1 PRIMARY KEY (x,z);

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
Matt Urbanowski
2/23/2007 2:09:33 AM
On 23 Feb, 07:22, "David Portas"
[quoted text, click to view]

Take a look at:

http://msdn2.microsoft.com/en-us/library/ms130214.aspx

If you do find yourself with duplicates I don't think there is a quick
way to get rid of them. Microsoft suggest creating a temporary table
of all the duplicate values, deleting the current ones in your table
then inserting it back again.

Matt
Roy Harvey
2/23/2007 6:56:40 AM
On 22 Feb 2007 20:11:01 -0800, "vipin" <vipinpaliwal1980@gmail.com>
[quoted text, click to view]

This question comes up often, but with many different variations in
what type of duplicates the questioner is dealing with. If you post
specifics, including the table definition with all keys and indexes,
as well as examples of the data, you will get more specific advice.

Roy Harvey
Uri Dimant
2/25/2007 12:00:00 AM
vipin
http://dimantdatabasesolutions.blogspot.com/2007/02/dealing-with-duplicates.html



[quoted text, click to view]

Matt Urbanowski
2/26/2007 7:09:46 AM
[quoted text, click to view]

Out of interest, I've just found out that if you use the UNION
function, it removes any duplicate values. Does anyone know if this
would be a suitable (faster?) way of removing duplicates if you union
a table by itself?
Tibor Karaszi
2/27/2007 12:00:00 AM
[quoted text, click to view]

Yes, UNION by definition removes duplicates. If you want to keepo duplicates, you use UNION ALL.

I tried three versions on my machine, comparing DISTINCT, GROUP BY (all columns) and UNION. The two
first produced the same plan (as expected), while the UNION produced a much worse plan. You can
check execution plan for below (assuming SQL Server 2005):

SELECT DISTINCT SalesOrderID, SalesOrderDetailID, CarrierTrackingNumber, OrderQty, ProductID,
SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, rowguid, ModifiedDate
FROM Sales.SalesOrderDetail

SELECT SalesOrderID, SalesOrderDetailID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID,
UnitPrice, UnitPriceDiscount, LineTotal, rowguid, ModifiedDate
FROM Sales.SalesOrderDetail
GROUP BY SalesOrderID, SalesOrderDetailID, CarrierTrackingNumber, OrderQty, ProductID,
SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, rowguid, ModifiedDate

SELECT SalesOrderID, SalesOrderDetailID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID,
UnitPrice, UnitPriceDiscount, LineTotal, rowguid, ModifiedDate
FROM Sales.SalesOrderDetail
UNION
SELECT SalesOrderID, SalesOrderDetailID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID,
UnitPrice, UnitPriceDiscount, LineTotal, rowguid, ModifiedDate
FROM Sales.SalesOrderDetail



--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


[quoted text, click to view]

Matt Urbanowski
2/27/2007 2:42:53 AM
On 27 Feb, 07:57, "Tibor Karaszi"
[quoted text, click to view]

OK, Thanks. That's interesting to know!

Matt
AddThis Social Bookmark Button