<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Delete Duplicate Records &#8211; Rows</title>
	<atom:link href="http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:54:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Aditya</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-57498</link>
		<dc:creator>Aditya</dc:creator>
		<pubDate>Tue, 10 Nov 2009 15:39:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-57498</guid>
		<description>Pinal rather than adding a ID col and dropping it I think WHILE loop (which you already suggested) will be better opeion and even better than that is use ROW_NUMBER() function and simply delete the records which has row_number count &gt; 1

select  row_number() over (partition by col order by col from tbl) and just delete where row_number() is greater than one and if one can set rowcount 1 also so if 2 records are exactly identical only 1 will be deleted....

thanks</description>
		<content:encoded><![CDATA[<p>Pinal rather than adding a ID col and dropping it I think WHILE loop (which you already suggested) will be better opeion and even better than that is use ROW_NUMBER() function and simply delete the records which has row_number count &gt; 1</p>
<p>select  row_number() over (partition by col order by col from tbl) and just delete where row_number() is greater than one and if one can set rowcount 1 also so if 2 records are exactly identical only 1 will be deleted&#8230;.</p>
<p>thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amit Dhall</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-57478</link>
		<dc:creator>Amit Dhall</dc:creator>
		<pubDate>Tue, 10 Nov 2009 08:30:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-57478</guid>
		<description>delete from tablename where NOT IN ( Select distinct * from tablename)</description>
		<content:encoded><![CDATA[<p>delete from tablename where NOT IN ( Select distinct * from tablename)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-57344</link>
		<dc:creator>Joe</dc:creator>
		<pubDate>Thu, 05 Nov 2009 06:43:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-57344</guid>
		<description>Hi Sir
i use Microsoft access 2003
how to make the same record  not to calculate the total or can i count the same record and devided by the count so that i can get original sum account
because duplicate record sum it all up 
which i only want to have only one record</description>
		<content:encoded><![CDATA[<p>Hi Sir<br />
i use Microsoft access 2003<br />
how to make the same record  not to calculate the total or can i count the same record and devided by the count so that i can get original sum account<br />
because duplicate record sum it all up<br />
which i only want to have only one record</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Great Man</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-56315</link>
		<dc:creator>Great Man</dc:creator>
		<pubDate>Thu, 01 Oct 2009 10:58:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-56315</guid>
		<description>Very nice and I am proud of you. I am very excited.</description>
		<content:encoded><![CDATA[<p>Very nice and I am proud of you. I am very excited.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avineet</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-55279</link>
		<dc:creator>Avineet</dc:creator>
		<pubDate>Thu, 27 Aug 2009 01:02:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-55279</guid>
		<description>HI Manoj, you can do so by setting row count 1 and put the delete statement, this way you will delete only 1 record of the 10 duplicate records. Avineet</description>
		<content:encoded><![CDATA[<p>HI Manoj, you can do so by setting row count 1 and put the delete statement, this way you will delete only 1 record of the 10 duplicate records. Avineet</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Manoj</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-55163</link>
		<dc:creator>Manoj</dc:creator>
		<pubDate>Mon, 24 Aug 2009 17:37:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-55163</guid>
		<description>I want to delete the duplicate record ( i.e i have 10 duplicate record , i want to delete a particular record leaving all the remaining 9 records means , how can i doit , plz reply me )</description>
		<content:encoded><![CDATA[<p>I want to delete the duplicate record ( i.e i have 10 duplicate record , i want to delete a particular record leaving all the remaining 9 records means , how can i doit , plz reply me )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ryan</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-54920</link>
		<dc:creator>ryan</dc:creator>
		<pubDate>Mon, 17 Aug 2009 14:04:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-54920</guid>
		<description>Pinal,

Thanks a ton, I spent a day experimenting with how to do this before I came across your solution, I added a key to my table and this worked like a charm.</description>
		<content:encoded><![CDATA[<p>Pinal,</p>
<p>Thanks a ton, I spent a day experimenting with how to do this before I came across your solution, I added a key to my table and this worked like a charm.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rich</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-54695</link>
		<dc:creator>Rich</dc:creator>
		<pubDate>Mon, 10 Aug 2009 16:12:25 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-54695</guid>
		<description>You could use CTE to delete the duplicates if you have SQL 2005 or above.

Create the duplicate table: -----------------------------------
IF OBJECT_ID(&#039;SalesHistory&#039;) IS NOT NULLDROP TABLE SalesHistoryCREATE TABLE [dbo].[SalesHistory]  (        [Product] [varchar](10) NULL,        [SaleDate] [datetime] NULL,        [SalePrice] [money] NULL  )  GOINSERT INTO SalesHistory(Product, SaleDate, SalePrice)SELECT &#039;Computer&#039;,&#039;1919-03-18 00:00:00.000&#039;,1008.00UNION ALLSELECT &#039;BigScreen&#039;,&#039;1927-03-18 00:00:00.000&#039;,91.00UNION ALLSELECT &#039;PoolTable&#039;,&#039;1927-04-01 00:00:00.000&#039;,139.00UNION ALLSELECT &#039;Computer&#039;,&#039;1919-03-18 00:00:00.000&#039;,1008.00UNION ALLSELECT &#039;BigScreen&#039;,&#039;1927-03-25 00:00:00.000&#039;,92.00UNION ALLSELECT &#039;PoolTable&#039;,&#039;1927-03-25 00:00:00.000&#039;,108.00UNION ALLSELECT &#039;Computer&#039;,&#039;1919-04-01 00:00:00.000&#039;,150.00UNION ALLSELECT &#039;BigScreen&#039;,&#039;1927-04-01 00:00:00.000&#039;,	123.00UNION ALLSELECT &#039;PoolTable&#039;,&#039;1927-04-01 00:00:00.000&#039;,	139.00UNION ALLSELECT &#039;Computer&#039;,&#039;1919-04-08 00:00:00.000&#039;,	168.00
=======================================

Remove the duplicates ---------------------------------------

;WITH SalesCTE(Product, SaleDate, SalePrice, Ranking)AS(SELECT	Product, SaleDate, SalePrice,Ranking = DENSE_RANK() OVER(PARTITION BY Product, SaleDate, SalePrice ORDER BY NEWID() ASC)FROM SalesHistory)DELETE FROM SalesCTEWHERE Ranking &gt; 1</description>
		<content:encoded><![CDATA[<p>You could use CTE to delete the duplicates if you have SQL 2005 or above.</p>
<p>Create the duplicate table: &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
IF OBJECT_ID(&#8216;SalesHistory&#8217;) IS NOT NULLDROP TABLE SalesHistoryCREATE TABLE [dbo].[SalesHistory]  (        [Product] [varchar](10) NULL,        [SaleDate] [datetime] NULL,        [SalePrice] [money] NULL  )  GOINSERT INTO SalesHistory(Product, SaleDate, SalePrice)SELECT &#8216;Computer&#8217;,'1919-03-18 00:00:00.000&#8242;,1008.00UNION ALLSELECT &#8216;BigScreen&#8217;,'1927-03-18 00:00:00.000&#8242;,91.00UNION ALLSELECT &#8216;PoolTable&#8217;,'1927-04-01 00:00:00.000&#8242;,139.00UNION ALLSELECT &#8216;Computer&#8217;,'1919-03-18 00:00:00.000&#8242;,1008.00UNION ALLSELECT &#8216;BigScreen&#8217;,'1927-03-25 00:00:00.000&#8242;,92.00UNION ALLSELECT &#8216;PoolTable&#8217;,'1927-03-25 00:00:00.000&#8242;,108.00UNION ALLSELECT &#8216;Computer&#8217;,'1919-04-01 00:00:00.000&#8242;,150.00UNION ALLSELECT &#8216;BigScreen&#8217;,'1927-04-01 00:00:00.000&#8242;,	123.00UNION ALLSELECT &#8216;PoolTable&#8217;,'1927-04-01 00:00:00.000&#8242;,	139.00UNION ALLSELECT &#8216;Computer&#8217;,'1919-04-08 00:00:00.000&#8242;,	168.00<br />
=======================================</p>
<p>Remove the duplicates &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>;WITH SalesCTE(Product, SaleDate, SalePrice, Ranking)AS(SELECT	Product, SaleDate, SalePrice,Ranking = DENSE_RANK() OVER(PARTITION BY Product, SaleDate, SalePrice ORDER BY NEWID() ASC)FROM SalesHistory)DELETE FROM SalesCTEWHERE Ranking &gt; 1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zoltan</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-54689</link>
		<dc:creator>Zoltan</dc:creator>
		<pubDate>Mon, 10 Aug 2009 13:09:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-54689</guid>
		<description>Brian,

Thank you for your help! Worked like a charm!

Thanks!</description>
		<content:encoded><![CDATA[<p>Brian,</p>
<p>Thank you for your help! Worked like a charm!</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Tkatch</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-54594</link>
		<dc:creator>Brian Tkatch</dc:creator>
		<pubDate>Fri, 07 Aug 2009 17:29:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-54594</guid>
		<description>@Zoltan

 Remove &quot;calownerid&quot; from the GROUP BY clause.</description>
		<content:encoded><![CDATA[<p>@Zoltan</p>
<p> Remove &#8220;calownerid&#8221; from the GROUP BY clause.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zoltan</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-54588</link>
		<dc:creator>Zoltan</dc:creator>
		<pubDate>Fri, 07 Aug 2009 14:24:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-54588</guid>
		<description>Hello Pinal,

Could you please help me? ..When I ran my script it removed both of the records. Could you please advice me why?
I do not have index in that table.

Thank you!

drop table dupcalref;
create temporary table dupcalref 
SELECT MAX(calownerid) as id FROM calref
group by calownerid, calenderid having count(calownerid)&gt;1;

alter table dupcalref add index(id);

delete FROM calref where calownerid in 
(select id from dupcalref);</description>
		<content:encoded><![CDATA[<p>Hello Pinal,</p>
<p>Could you please help me? ..When I ran my script it removed both of the records. Could you please advice me why?<br />
I do not have index in that table.</p>
<p>Thank you!</p>
<p>drop table dupcalref;<br />
create temporary table dupcalref<br />
SELECT MAX(calownerid) as id FROM calref<br />
group by calownerid, calenderid having count(calownerid)&gt;1;</p>
<p>alter table dupcalref add index(id);</p>
<p>delete FROM calref where calownerid in<br />
(select id from dupcalref);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: siddique ahmad</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-54416</link>
		<dc:creator>siddique ahmad</dc:creator>
		<pubDate>Tue, 04 Aug 2009 11:52:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-54416</guid>
		<description>Thanks a lot this really help me in removing the duplicate invoice added due to my code</description>
		<content:encoded><![CDATA[<p>Thanks a lot this really help me in removing the duplicate invoice added due to my code</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ARNAB SARKAR</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-54378</link>
		<dc:creator>ARNAB SARKAR</dc:creator>
		<pubDate>Mon, 03 Aug 2009 05:59:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-54378</guid>
		<description>TABLE- MASTER,W_CODE IS COLUMN NAME.

DELETE FROM MASTER WHERE W_ID IN(SELECT MAX(W_ID) FROM MASTER WHERE W_CODE IN(SELECT W_CODE FROM MASTER GROUP BY W_CODE HAVING COUNT(W_CODE)&gt;1))</description>
		<content:encoded><![CDATA[<p>TABLE- MASTER,W_CODE IS COLUMN NAME.</p>
<p>DELETE FROM MASTER WHERE W_ID IN(SELECT MAX(W_ID) FROM MASTER WHERE W_CODE IN(SELECT W_CODE FROM MASTER GROUP BY W_CODE HAVING COUNT(W_CODE)&gt;1))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neto</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-54182</link>
		<dc:creator>Neto</dc:creator>
		<pubDate>Wed, 29 Jul 2009 04:54:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-54182</guid>
		<description>Thanks for your help man!!!

It helps me a lot!!!

Greats from Lima, Perú.</description>
		<content:encoded><![CDATA[<p>Thanks for your help man!!!</p>
<p>It helps me a lot!!!</p>
<p>Greats from Lima, Perú.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aasim abdullah</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-53655</link>
		<dc:creator>aasim abdullah</dc:creator>
		<pubDate>Mon, 13 Jul 2009 06:26:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-53655</guid>
		<description>excellent post, Senthilnathan comments really helped me to delete duplicate rows with most appropriate way.</description>
		<content:encoded><![CDATA[<p>excellent post, Senthilnathan comments really helped me to delete duplicate rows with most appropriate way.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lucky Ahuja</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-53448</link>
		<dc:creator>Lucky Ahuja</dc:creator>
		<pubDate>Fri, 03 Jul 2009 09:56:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-53448</guid>
		<description>Hey Guys thats all fine . U can store the values in the Temp table remove the duplicate records and empty the original table . Then move all the unique records to the original table . But its a lengthy process suppose i am having 2 million records in a table and from that table if i want to ren=move the duplicate records then it would really affect the performance . Sorry Since i am not having so much of exp and if i had hurted someone .  Pinal Sir do let me know if i am wrong</description>
		<content:encoded><![CDATA[<p>Hey Guys thats all fine . U can store the values in the Temp table remove the duplicate records and empty the original table . Then move all the unique records to the original table . But its a lengthy process suppose i am having 2 million records in a table and from that table if i want to ren=move the duplicate records then it would really affect the performance . Sorry Since i am not having so much of exp and if i had hurted someone .  Pinal Sir do let me know if i am wrong</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: amit</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-53192</link>
		<dc:creator>amit</dc:creator>
		<pubDate>Tue, 23 Jun 2009 09:09:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-53192</guid>
		<description>Hello sir,

You r genious, I love your articles.

thanks</description>
		<content:encoded><![CDATA[<p>Hello sir,</p>
<p>You r genious, I love your articles.</p>
<p>thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: amit jain</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-53038</link>
		<dc:creator>amit jain</dc:creator>
		<pubDate>Tue, 16 Jun 2009 10:39:09 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-53038</guid>
		<description>Hello Pinal,

Is there any procedure to restore deleted records from particular table in database?

Thank you
Amit Jain</description>
		<content:encoded><![CDATA[<p>Hello Pinal,</p>
<p>Is there any procedure to restore deleted records from particular table in database?</p>
<p>Thank you<br />
Amit Jain</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gpshiburaj</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-52867</link>
		<dc:creator>gpshiburaj</dc:creator>
		<pubDate>Tue, 09 Jun 2009 09:55:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-52867</guid>
		<description>Thanks Brian, 
I was thinking that we would have to use a cursor for this, using row_number is easier.</description>
		<content:encoded><![CDATA[<p>Thanks Brian,<br />
I was thinking that we would have to use a cursor for this, using row_number is easier.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Tkatch</title>
		<link>http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/#comment-52836</link>
		<dc:creator>Brian Tkatch</dc:creator>
		<pubDate>Mon, 08 Jun 2009 16:15:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/03/01/delete-duplicate-records/#comment-52836</guid>
		<description>@gpshiburaj

ROW_NUMBER() does that:


WITH
	Data(field01)
AS
	(
	 SELECT	&#039;AA&#039; UNION ALL
	 SELECT	&#039;AB&#039; UNION ALL
	 SELECT	&#039;AC&#039; UNION ALL
	 SELECT	&#039;AB&#039; UNION ALL
	 SELECT &#039;AA&#039; UNION ALL
	 SELECT &#039;AD&#039;
	)
SELECT
		field01,
		ROW_NUMBER() OVER(PARTITION BY field01 ORDER BY field01)
FROM
		Data;</description>
		<content:encoded><![CDATA[<p>@gpshiburaj</p>
<p>ROW_NUMBER() does that:</p>
<p>WITH<br />
	Data(field01)<br />
AS<br />
	(<br />
	 SELECT	&#8216;AA&#8217; UNION ALL<br />
	 SELECT	&#8216;AB&#8217; UNION ALL<br />
	 SELECT	&#8216;AC&#8217; UNION ALL<br />
	 SELECT	&#8216;AB&#8217; UNION ALL<br />
	 SELECT &#8216;AA&#8217; UNION ALL<br />
	 SELECT &#8216;AD&#8217;<br />
	)<br />
SELECT<br />
		field01,<br />
		ROW_NUMBER() OVER(PARTITION BY field01 ORDER BY field01)<br />
FROM<br />
		Data;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
