Re: how to remove the duplicate records from a table

From: "Peter Childs" <peterachilds(at)gmail(dot)com>
To: "Yi Zhao" <yi(dot)zhao(at)alibaba-inc(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: how to remove the duplicate records from a table
Date: 2008-10-07 07:04:45
Message-ID: a2de01dd0810070004m443e02e1xf3d9b6d5778758eb@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2008/10/7 Yi Zhao <yi(dot)zhao(at)alibaba-inc(dot)com>:
> 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?
>
> any help is appreciated,
>
> thanks,
>
>

I would not say this is easier....

1. alter table t add key serial;
2. delete from table where key not in (select max(key) from table
group on id,temp_id);

The truth is this is not any less work then using a temporary table
(whole table still needs rewriting). Which method you select really
depends on why these duplicate records exist in the first place.

Regards

Peter

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tapio.Niva 2008-10-07 08:40:20 DBD::Pg 2.10.7 compile failed on RH4
Previous Message Henry Combrinck 2008-10-07 06:50:18 Re: Looping through cursor row batches