Re: How to delete duplicate record - Mailing list pgsql-sql

From Tomasz Myrta
Subject Re: How to delete duplicate record
Date
Msg-id 20030210083647.M23706@klaster.net
Whole thread Raw
In response to How to delete duplicate record  (Abdul Wahab Dahalan <wahab@mimos.my>)
List pgsql-sql
> b2b=> select * from biztypes;
>  bizid |  biztype
> -------+-----------
>  B11   | logistics
>  B11   | logistics
>  B11   | logistics
>  B11   | logistics
>  B11   | logistics
> (5 rows)
> 
> b2b=>delete from biztypes where exists (select * from biztypes b2 where
> biztypes.bizid=b2.bizid)
> 
> DELETE 5
> b2bscm=> select * from test1;
>  bizid | biztype
> -------+---------
> (0 rows)
> 
> all the records been deleted.
> the result should be :
> 
> bizid |  biztype
> -------+-----------
>  B11   | logistics
> 
Sorry, I forgot this code works only if you have any field which is
primary key or any other unique identifier - for example oid.
It will look then:
delete from biztypes where exists (select * from biztypes b2 where
biztypes.bizid=b2.bizid and biztypes.oid<>b2.oid)

If you don't have any unique identifier for rows in your table, the only
way to delete this record is creating a copy of this table:
create table some_copy as
selectbizid,biztype
from biztypes group by bizid,biztype;
drop table biztypes;
alter table some_copy rename to biztypes;

Regards,
Tomasz Myrta




pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: How to delete duplicate record
Next
From: "Frankie Lam"
Date:
Subject: Re: plpgsql + dblink() question