SQL Server – Deleting Duplicate Rows

Oh what a tangled web we weave when we don’t use unique constraints or primary keys…

Using some clever rules about deleting from a view (also applies to a common table expression).  You can delete from a view if the view only references one table.

WITH    cte(b, r)
AS (SELECT    Field1,
ROW_NUMBER() OVER (PARTITION BY Field1 ORDER BY Field1)
FROM      dbo.Table1
WHERE     Field1 IN (SELECT   Field1
FROM     dbo.Table1
GROUP BY Field1
HAVING   COUNT(*)>1))
DELETE FROM cte WHERE r > 1;

Part of the trick is to use Row_Number to make a distinction between the rows.  You can then delete anything that has a row number greater than 1 (a dupe)

Advertisements

Have an opinion? Leave an opinion!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s