Re: De-duplicating rows

From: Richard Huxton <dev(at)archonet(dot)com>
To: Christophe <xof(at)thebuild(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: De-duplicating rows
Date: 2009-07-17 07:21:01
Message-ID: 4A60265D.1060905@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Christophe wrote:
> The Subject: is somewhat imprecise, but here's what I'm trying to do.
> For some reason, my brain is locking up over it.
>
> I'm moving a 7.2 (yes) database to 8.4. >

Big leap. Allow some time for testing your application. I'm not sure if
7.2 even supported schemas, and there's been loads of tightening up the
rules for automatic type casting, unicode etc.

> Now, since this database has been production since 7.2 days, cruft has
> crept in: in particular, there are duplicate email addresses, some with
> mismatched attributes. The policy decision by the client is that the
> correct row is the one with the earliest timestamp.

Something like (untested):

CREATE TEMPORARY TABLE earliest_duplicates AS
SELECT
email AS tgt_email,
min(create_date) AS tgt_date
FROM mytable
GROUP BY email
HAVING count(*) > 1;

DELETE FROM mytable USING earliest duplicates
WHERE email=tgt_email AND create_date > tgt_date;

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Frank Bax 2009-07-17 15:04:48 Re: De-duplicating rows
Previous Message Christophe 2009-07-17 03:07:09 De-duplicating rows