<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to delete duplicate rows with SQL, Part 2</title>
	<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/</link>
	<description>Stay curious!</description>
	<pubDate>Sat, 30 Aug 2008 04:57:09 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: Diwakar</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-13677</link>
		<author>Diwakar</author>
		<pubDate>Mon, 12 Nov 2007 13:36:11 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-13677</guid>
		<description>BINGO !!!!
awesome article 

CHEERS!!
---
Diwakar</description>
		<content:encoded><![CDATA[<p>BINGO !!!!<br />
awesome article </p>
<p>CHEERS!!<br />
&#8212;<br />
Diwakar</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joseph</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-13539</link>
		<author>Joseph</author>
		<pubDate>Wed, 17 Oct 2007 10:34:21 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-13539</guid>
		<description>i want to delete the records on my table that has simllar entries in my column. e.g column A: has the following entries [aaaaaaa]
        [aaaaamobile]
        [aaaamotoraaaa]
now i want to delete only the a's</description>
		<content:encoded><![CDATA[<p>i want to delete the records on my table that has simllar entries in my column. e.g column A: has the following entries [aaaaaaa]<br />
        [aaaaamobile]<br />
        [aaaamotoraaaa]<br />
now i want to delete only the a&#8217;s</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jimro</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-4731</link>
		<author>jimro</author>
		<pubDate>Sat, 03 Mar 2007 12:26:58 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-4731</guid>
		<description>&lt;p&gt;Hi, sorry for my poor english.&lt;/p&gt;
&lt;p&gt;I forgot to format a part of my previous message in XHTML, thank you for correction.&lt;/p&gt;

&lt;p&gt;With this
&lt;code&gt;least(0, length(@type := type))) &#62; 0&lt;/code&gt;
only one row is preserved.
Isn't this the required goal? &lt;/p&gt;

&lt;p&gt;Regarding my previous code, it would be better like this:&lt;/p&gt;
&lt;pre&gt;set @num := 0, @source := '', @target := '';
delete from test
where greatest(0,
   @num := if(source = @source and target = @target, @num + 1, 0),
   least(0, length(@source := source), length(@target := target))) &#62; 0
order by source, target;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi, sorry for my poor english.</p>
<p>I forgot to format a part of my previous message in XHTML, thank you for correction.</p>
<p>With this<br />
<code>least(0, length(@type := type))) &gt; 0</code><br />
only one row is preserved.<br />
Isn&#8217;t this the required goal? </p>
<p>Regarding my previous code, it would be better like this:</p>
<pre>set @num := 0, @source := '', @target := '';
delete from test
where greatest(0,
   @num := if(source = @source and target = @target, @num + 1, 0),
   least(0, length(@source := source), length(@target := target))) &gt; 0
order by source, target;</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: jimro</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-4717</link>
		<author>jimro</author>
		<pubDate>Fri, 02 Mar 2007 22:40:20 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-4717</guid>
		<description>&lt;p&gt;Hi, thanks for these nice articles.&lt;/p&gt;

&lt;p&gt;Maybe
&lt;code&gt;least(0, length(@type := type))) &#62; 0&lt;/code&gt;
instead of
&lt;code&gt;least(0, length(@type := type))) &#62; 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I've tested with two columns (MySQL 5.0.27) and it works very well&lt;/p&gt;

&lt;pre&gt;set @num := 0, @source := '', @target := '';
delete from test
where greatest(0,
   @num := if(source = @source and target = @target, @num + 1, 0),
   least(0, length(@source := source), length(@target := target))) &#62; 0
order by source;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi, thanks for these nice articles.</p>
<p>Maybe<br />
<code>least(0, length(@type := type))) &gt; 0</code><br />
instead of<br />
<code>least(0, length(@type := type))) &gt; 1</code></p>
<p>I&#8217;ve tested with two columns (MySQL 5.0.27) and it works very well</p>
<pre>set @num := 0, @source := '', @target := '';
delete from test
where greatest(0,
   @num := if(source = @source and target = @target, @num + 1, 0),
   least(0, length(@source := source), length(@target := target))) &gt; 0
order by source;</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Collin</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-3995</link>
		<author>Collin</author>
		<pubDate>Fri, 09 Feb 2007 01:59:04 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-3995</guid>
		<description>&lt;p&gt;I have a genius method for removing duplicates..  open enterprise manager.. list all rows and then go through deleting the rows that are the same using the delete button.

OK, OK.. not so JENIUS after all..  worked for me though.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>I have a genius method for removing duplicates..  open enterprise manager.. list all rows and then go through deleting the rows that are the same using the delete button.</p>
<p>OK, OK.. not so JENIUS after all..  worked for me though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-3958</link>
		<author>Xaprb</author>
		<pubDate>Wed, 07 Feb 2007 12:51:06 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-3958</guid>
		<description>&lt;p&gt;Thanks Shantanu, I didn't know about that!&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Thanks Shantanu, I didn&#8217;t know about that!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shantanu Oak</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-3956</link>
		<author>Shantanu Oak</author>
		<pubDate>Wed, 07 Feb 2007 12:08:53 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-3956</guid>
		<description>&lt;p&gt;Why not to add unique index to drop duplicate rows? It works only in MySQL.
alter ignore table dupTest add unique index (a,b)&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Why not to add unique index to drop duplicate rows? It works only in MySQL.<br />
alter ignore table dupTest add unique index (a,b)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-3932</link>
		<author>Xaprb</author>
		<pubDate>Tue, 06 Feb 2007 15:17:58 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-3932</guid>
		<description>&lt;p&gt;Bill Minton: bravo!  Thanks for writing this in.  The &lt;code&gt;SET ROWCOUNT&lt;/code&gt; is exactly the type of platform-specific trick I was talking about.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Bill Minton: bravo!  Thanks for writing this in.  The <code>SET ROWCOUNT</code> is exactly the type of platform-specific trick I was talking about.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill Minton</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-3931</link>
		<author>Bill Minton</author>
		<pubDate>Tue, 06 Feb 2007 15:11:41 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-3931</guid>
		<description>&lt;p&gt;SQL Server Version:&lt;/p&gt;

&lt;pre&gt;set nocount on
create table #tbFruit (Fruit varchar(20))
insert into #tbFruit (Fruit) values ('apple')
insert into #tbFruit (Fruit) values ('apple')
insert into #tbFruit (Fruit) values ('apple')
insert into #tbFruit (Fruit) values ('apple')
insert into #tbFruit (Fruit) values ('orange')
insert into #tbFruit (Fruit) values ('orange')
insert into #tbFruit (Fruit) values ('orange')

create table #tbFruitsWithDupes (DupeID int identity, Fruit varchar(20), DupeCount int)

insert into #tbFruitsWithDupes (Fruit, DupeCount)
	select Fruit, count(*) DupeCount from #tbFruit
	group by Fruit having count(*) &#62; 1

declare @x int, @max int, @Fruit varchar(20), @DupeCount int

select @x = 1, @max	= max(DupeID) from #tbFruitsWithDupes

while (@x &#60;= @max)
	begin
		select @Fruit = Fruit, @DupeCount = DupeCount - 1
			from #tbFruitsWithDupes Where DupeID = @x

		set rowcount @DupeCount

		delete from #tbFruit where Fruit = @Fruit

		select @x = @x + 1
	end

select * from #tbFruit

drop table #tbFruit
drop table #tbFruitsWithDupes&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>SQL Server Version:</p>
<pre>set nocount on
create table #tbFruit (Fruit varchar(20))
insert into #tbFruit (Fruit) values ('apple')
insert into #tbFruit (Fruit) values ('apple')
insert into #tbFruit (Fruit) values ('apple')
insert into #tbFruit (Fruit) values ('apple')
insert into #tbFruit (Fruit) values ('orange')
insert into #tbFruit (Fruit) values ('orange')
insert into #tbFruit (Fruit) values ('orange')

create table #tbFruitsWithDupes (DupeID int identity, Fruit varchar(20), DupeCount int)

insert into #tbFruitsWithDupes (Fruit, DupeCount)
	select Fruit, count(*) DupeCount from #tbFruit
	group by Fruit having count(*) &gt; 1

declare @x int, @max int, @Fruit varchar(20), @DupeCount int

select @x = 1, @max	= max(DupeID) from #tbFruitsWithDupes

while (@x &lt;= @max)
	begin
		select @Fruit = Fruit, @DupeCount = DupeCount - 1
			from #tbFruitsWithDupes Where DupeID = @x

		set rowcount @DupeCount

		delete from #tbFruit where Fruit = @Fruit

		select @x = @x + 1
	end

select * from #tbFruit

drop table #tbFruit
drop table #tbFruitsWithDupes</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
