Re: SQL for Deleting all duplicate entries

From: "Merlin Moncure" <mmoncure(at)gmail(dot)com>
To: Håkan Jacobsson <hakan(dot)jacobsson99(at)bredband(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL for Deleting all duplicate entries
Date: 2007-09-09 13:42:05
Message-ID: b42b73150709090642x6427e8fdv902a22703aceefc@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 9/9/07, Håkan Jacobsson <hakan(dot)jacobsson99(at)bredband(dot)net> wrote:
> Merlin,
>
> Its just about three columns - not any column. Two columns are
> varchars and the third is
> a date. The date column value is NULL for the rows for which
> I want to delete the duplicates.

getting ready to go on vacation :). The idea is you want to write a
query that pulls out the data you want to keep. If you have a table
with 6 fields, f1 though f6 and you only want one record with
identical values of f1, f2, f3, you might do:

begin;
create temp table scratch as
select f1, f2, f3, max(f4), max(f5), max(f6) from foo group by f1, f2, f3;

truncate foo;

insert into foo select * from scratch;
commit;

You can replace max() with any suitable aggregate you deem gets you
the best data out of the record. If you are feeling really clever,
you can write a custom aggregate for the record type (it's easier than
you think!)

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-09-09 14:54:27 Re: Checking is TSearch2 query is valid
Previous Message Håkan Jacobsson 2007-09-09 12:28:51 Re: SQL for Deleting all duplicate entries