Tuesday, August 25, 2009

SQL SERVER 2005: Removing Duplicate records

The following post tell you how to remove duplicate records from a table not having primary key
CREATE TABLE T1
(
[Ename] char(10) NULL,
[ECity] char(10) NULL
)
GO
INSERT INTO T1 (Ename, ECity)
SELECT 'Hamed', 'Bellevue'
UNION ALL
SELECT 'Billg', 'Redmond'
UNION ALL
SELECT 'Billg', Redmond
Now that duplicate records exists...
; WITH T1CTE(Ename, ECity, Ranking) AS
(
SELECT Ename, ECity, Ranking = DENSE_RANK() OVER (PARTITION BY Ename, ECity ORDER by NEWID() ASC)
FROM T1
)
DELETE FROM T1CTE WHERE Ranking > 1
-- Duplicates from the underlying table T1 is removed when we process the Virtual table T1CTE
-- To verify run the following query
SELECT * FROM T1;

Another good reference: http://blogs.techrepublic.com.com/datacenter/?p=420

No comments:

MSDN: U.S. Local Highlights