X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id E10D0D1B173 for ; Tue, 22 Jun 2004 14:19:45 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 75464-01 for ; Tue, 22 Jun 2004 17:19:43 +0000 (GMT) Received: from anchor-post-30.mail.demon.net (anchor-post-30.mail.demon.net [194.217.242.88]) by svr1.postgresql.org (Postfix) with ESMTP id 32613D1B169 for ; Tue, 22 Jun 2004 14:19:41 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-30.mail.demon.net with esmtp (Exim 3.35 #1) id 1Bcovm-000NbP-0U; Tue, 22 Jun 2004 18:19:34 +0100 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 761D216DFB; Tue, 22 Jun 2004 18:16:18 +0100 (BST) Message-ID: <40D86960.4070008@archonet.com> Date: Tue, 22 Jun 2004 18:16:16 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 0.7 (X11/20040615) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Matthew Nuzum Cc: pgsql-sql@postgresql.org Subject: Re: matching rows differing only by fkey,pkey References: <200406221634.i5MGYcnb009716@ms-smtp-02.tampabay.rr.com> In-Reply-To: <200406221634.i5MGYcnb009716@ms-smtp-02.tampabay.rr.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/207 X-Sequence-Number: 18152 Matthew Nuzum wrote: > When the relationships are one to one or one to many this process is easy, > however sometimes there's a many to many relationship. > > It seems that a helpful tool would be a query that can return just the pkey > of the original record copied from and the pkey of the newly created record. > > For example, if the b table looked like this after a copy of 3 rows: > bid | aid | field1 | field2 | field3 > 1 | 22 | abc | 123 | abc123 > 2 | 22 | xyz | 456 | xyz456 > 3 | 22 | pdq | 789 | pdq789 > 4 | 23 | abc | 123 | abc123 > 5 | 23 | xyz | 456 | xyz456 > 6 | 23 | pdq | 789 | pdq789 > > I'd like to get this: > oldbid | newbid > 1 | 4 > 2 | 5 > 3 | 6 SELECT one.bid AS oldbid, two.bid AS newbid FROM b one, b two WHERE one.field1=two.field1 AND ... AND two.bid > one.bid ; Of course, if there are 3+ rows with duplicate field1/2/3 then this won't work. -- Richard Huxton Archonet Ltd