Comments on: How to delete duplicate rows with SQL, Part 2 http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/ Stay curious! Fri, 10 May 2013 18:25:19 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Deepak http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-19489 Deepak Thu, 14 Jul 2011 08:45:11 +0000 http://www.xaprb.com/blog/?p=291#comment-19489 The code

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))) > 0
order by source, target;

Does not work when the rows are not in constitutive order.

]]>
By: Lyndon http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-19397 Lyndon Tue, 07 Jun 2011 21:16:39 +0000 http://www.xaprb.com/blog/?p=291#comment-19397 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…

]]>
By: Lyndon http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-19396 Lyndon Tue, 07 Jun 2011 21:13:42 +0000 http://www.xaprb.com/blog/?p=291#comment-19396 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(*) > 1
) as good_rows on good_rows.tlogtransactionid = bad_rows.tlogtransactionid
) where id > 1;

delete from safeaudit where tlogtransactionid in (
select tlogtransactionid
from safeaudit
group by tlogtransactionid
having count(*) > 1
);

alter table safeaudit add constraint XPKSafeAudit primary key (TLOGTRANSACTIONID);

insert into safeaudit select * from safeauditcopy;

drop table safeauditcopy;

{code}

]]>
By: Itry http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-18737 Itry Fri, 01 Oct 2010 01:07:45 +0000 http://www.xaprb.com/blog/?p=291#comment-18737 If someone see, that will be fine

]]>
By: Itry http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/#comment-18736 Itry Fri, 01 Oct 2010 01:06:49 +0000 http://www.xaprb.com/blog/?p=291#comment-18736 It motivate me in a new way.
But I wonder if most of you use mySQL instead SQL server.

]]>