<?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/"
		>
<channel>
	<title>Comments on: How to delete duplicate rows with SQL, Part 2</title>
	<atom:link href="http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/</link>
	<description>Stay curious!</description>
	<lastBuildDate>Thu, 09 Feb 2012 09:56:43 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Deepak</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-19489</link>
		<dc:creator>Deepak</dc:creator>
		<pubDate>Thu, 14 Jul 2011 08:45:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=291#comment-19489</guid>
		<description>The code 

set @num := 0, @source := &#039;&#039;, @target := &#039;&#039;;
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;


Does not work when the rows are not in constitutive order.</description>
		<content:encoded><![CDATA[<p>The code </p>
<p>set @num := 0, @source := &#8221;, @target := &#8221;;<br />
delete from test<br />
where greatest(0,<br />
   @num := if(source = @source and target = @target, @num + 1, 0),<br />
   least(0, length(@source := source), length(@target := target))) &gt; 0<br />
order by source, target;</p>
<p>Does not work when the rows are not in constitutive order.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lyndon</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-19397</link>
		<dc:creator>Lyndon</dc:creator>
		<pubDate>Tue, 07 Jun 2011 21:16:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=291#comment-19397</guid>
		<description>I forgot to mention, the code was for DB2 and makes use of the OLAP function rownumber() to give me the ability to determine the difference between one record and another...</description>
		<content:encoded><![CDATA[<p>I forgot to mention, the code was for DB2 and makes use of the OLAP function rownumber() to give me the ability to determine the difference between one record and another&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lyndon</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-19396</link>
		<dc:creator>Lyndon</dc:creator>
		<pubDate>Tue, 07 Jun 2011 21:13:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=291#comment-19396</guid>
		<description>Thank you for the article, it was of considerable help.  I had a table with many records but no primary key and I had to create a primary key on it.  The problem was there were duplicates.  

Using the inner join query from your other article I was able to create a temporary table where I inserted the unique records found from all duplicates.  Then I was able to delete all the duplicates from the original table, add the constraint, and then add the deleted records back in from the temporary table.

Here is my code for those who might need this help later.

{code}
connect to TE_DFLT;

set current schema EPOS;

create table safeauditcopy (sitedirectoryid bigint not null,
				safeid vargraphic(32) not null,
				updatedatetime timestamp not null,
				debitamount decimal(19) not null,
				creditamount decimal(19) not null,
				aftertranssafeamount decimal(19) not null,
				businessdate timestamp not null,
				tlogtransactionid character(22) not null constraint XPKSafeAudit PRIMARY KEY
);

insert into safeauditcopy
select sitedirectoryid,safeid,updatedatetime,debitamount,creditamount,aftertranssafeamount,businessdate,tlogtransactionid 
from ( 
	select bad_rows.*, rownumber() over(partition by bad_rows.tlogtransactionid) as id
	from safeaudit as bad_rows
	inner join (
		select tlogtransactionid
		from safeaudit
		group by tlogtransactionid
		having count(*) &gt; 1
	) as good_rows on good_rows.tlogtransactionid = bad_rows.tlogtransactionid
)  where id &gt; 1;

delete from safeaudit where tlogtransactionid in ( 
	select tlogtransactionid
	from safeaudit
	group by tlogtransactionid
	having count(*) &gt; 1	
);

alter table safeaudit add constraint XPKSafeAudit primary key (TLOGTRANSACTIONID);

insert into safeaudit select * from safeauditcopy;

drop table safeauditcopy;

{code}</description>
		<content:encoded><![CDATA[<p>Thank you for the article, it was of considerable help.  I had a table with many records but no primary key and I had to create a primary key on it.  The problem was there were duplicates.  </p>
<p>Using the inner join query from your other article I was able to create a temporary table where I inserted the unique records found from all duplicates.  Then I was able to delete all the duplicates from the original table, add the constraint, and then add the deleted records back in from the temporary table.</p>
<p>Here is my code for those who might need this help later.</p>
<p>{code}<br />
connect to TE_DFLT;</p>
<p>set current schema EPOS;</p>
<p>create table safeauditcopy (sitedirectoryid bigint not null,<br />
				safeid vargraphic(32) not null,<br />
				updatedatetime timestamp not null,<br />
				debitamount decimal(19) not null,<br />
				creditamount decimal(19) not null,<br />
				aftertranssafeamount decimal(19) not null,<br />
				businessdate timestamp not null,<br />
				tlogtransactionid character(22) not null constraint XPKSafeAudit PRIMARY KEY<br />
);</p>
<p>insert into safeauditcopy<br />
select sitedirectoryid,safeid,updatedatetime,debitamount,creditamount,aftertranssafeamount,businessdate,tlogtransactionid<br />
from (<br />
	select bad_rows.*, rownumber() over(partition by bad_rows.tlogtransactionid) as id<br />
	from safeaudit as bad_rows<br />
	inner join (<br />
		select tlogtransactionid<br />
		from safeaudit<br />
		group by tlogtransactionid<br />
		having count(*) &gt; 1<br />
	) as good_rows on good_rows.tlogtransactionid = bad_rows.tlogtransactionid<br />
)  where id &gt; 1;</p>
<p>delete from safeaudit where tlogtransactionid in (<br />
	select tlogtransactionid<br />
	from safeaudit<br />
	group by tlogtransactionid<br />
	having count(*) &gt; 1<br />
);</p>
<p>alter table safeaudit add constraint XPKSafeAudit primary key (TLOGTRANSACTIONID);</p>
<p>insert into safeaudit select * from safeauditcopy;</p>
<p>drop table safeauditcopy;</p>
<p>{code}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Itry</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-18737</link>
		<dc:creator>Itry</dc:creator>
		<pubDate>Fri, 01 Oct 2010 01:07:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=291#comment-18737</guid>
		<description>If someone see, that will be fine</description>
		<content:encoded><![CDATA[<p>If someone see, that will be fine</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Itry</title>
		<link>http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-18736</link>
		<dc:creator>Itry</dc:creator>
		<pubDate>Fri, 01 Oct 2010 01:06:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=291#comment-18736</guid>
		<description>It motivate me in a new way.
But I wonder if most of you use mySQL instead SQL server.</description>
		<content:encoded><![CDATA[<p>It motivate me in a new way.<br />
But I wonder if most of you use mySQL instead SQL server.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

