Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 484E8632ED3 for ; Fri, 17 Jul 2009 04:21:50 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 57815-01-8 for ; Fri, 17 Jul 2009 04:21:31 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from relay.pcl-ipout02.plus.net (relay.pcl-ipout02.plus.net [212.159.7.100]) by mail.postgresql.org (Postfix) with ESMTP id 2BBCE634A2D for ; Fri, 17 Jul 2009 04:21:06 -0300 (ADT) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEAHjDX0rUnw4R/2dsb2JhbADQVIQNBQ Received: from pih-relay04.plus.net ([212.159.14.17]) by relay.pcl-ipout02.plus.net with ESMTP; 17 Jul 2009 08:21:05 +0100 Received: from [84.51.143.99] (helo=server3.office.archonet.com) by pih-relay04.plus.net with esmtp (Exim) id 1MRhke-0004Tf-TB; Fri, 17 Jul 2009 08:21:05 +0100 Received: from dell36.office.archonet.com (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 91923274059; Fri, 17 Jul 2009 08:21:01 +0100 (BST) Message-ID: <4A60265D.1060905@archonet.com> Date: Fri, 17 Jul 2009 08:21:01 +0100 From: Richard Huxton User-Agent: Thunderbird 2.0.0.21 (X11/20090320) MIME-Version: 1.0 To: Christophe CC: pgsql-sql@postgresql.org Subject: Re: De-duplicating rows References: <87E8E647-4CAD-4491-B687-FA763E0EBE7E@thebuild.com> In-Reply-To: <87E8E647-4CAD-4491-B687-FA763E0EBE7E@thebuild.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Plusnet-Relay: 8494cead66ae91a714884cebeda57aec X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200907/76 X-Sequence-Number: 32817 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