Re: how to remove the duplicate records from a table

From: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Yi Zhao *EXTERN*" <yi(dot)zhao(at)alibaba-inc(dot)com>, "pgsql-general" <pgsql-general(at)postgresql(dot)org>
Subject: Re: how to remove the duplicate records from a table
Date: 2008-10-07 09:48:01
Message-ID: D960CB61B694CF459DCFB4B0128514C2029660B3@exadv11.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Yi Zhao wrote:
> I have a table contains some duplicate records, and this table create
> without oids, for example:
> id | temp_id
> ----+---------
> 10 | 1
> 10 | 1
> 10 | 1
> 20 | 4
> 20 | 4
> 30 | 5
> 30 | 5
> I want get the duplicated records removed and only one is reserved, so
> the results is:
> 10 1
> 20 4
> 30 5
>
> I know create a temp table will resolve this problem, but I don't want
> this way:)
>
> can someone tell me a simple methold?

Don't know if you'd call that simple, but if the table is
called "t", you could do

DELETE FROM t t1 USING t t2
WHERE t1.id = t2.id AND t1.temp_id = t2.temp_id AND t1.ctid > t2.ctid;

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message tsp 2008-10-07 10:08:50 Re: ERROR: column "datpath" does not exist
Previous Message Markus Wanner 2008-10-07 09:08:08 Re: [Pkg-postgresql-public] Postgres major version support policy on Debian