Re: Deleting all but one row of a list of non-uniques

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Zak McGregor <zak(at)mighty(dot)co(dot)za>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Deleting all but one row of a list of non-uniques
Date: 2004-06-23 00:41:38
Message-ID: 20040623004137.GA30207@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Jun 23, 2004 at 02:17:50AM +0200, Zak McGregor wrote:
> Hi all
>
>
> I have a table, for simplicity's sake containing one field, called unid.

<snip>

> (I've marked the rows with duplicated unids with the * to the right)
>
> I'd like to delete rows in such a way that one (and any one) row for each unid
> remains, and all other duplicates are removed. Does anyone have any ideas that
> may help me here please?

The usual scheme I use is something like: (not perfect SQL)

delete from table where exists (select from table where this.id <
that.id).

Hope this helps,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Scott Ribe 2004-06-23 01:02:28 Re: Connection gets into state where all queries fail
Previous Message Zak McGregor 2004-06-23 00:17:50 Deleting all but one row of a list of non-uniques