Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 50238C7DB12 for ; Mon, 19 Mar 2012 19:15:17 -0300 (ADT) Received: from smtp110.prem.mail.ac4.yahoo.com ([76.13.13.93]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1S9krA-0001bk-47 for pgsql-sql@postgresql.org; Mon, 19 Mar 2012 22:15:16 +0000 Received: (qmail 19116 invoked from network); 19 Mar 2012 22:14:58 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:Cc:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=3zgBo5XYyjCikY++oWo0WydUh2kitCUPfTP1lkSG037ZUzrn+WliImUuq98HNekfqXQmcUC6LzmdaWAOOPmo39Rg8lqBcwTqfFoP2NqM4qrwqb1BmYCz11jm0Vkzl0jHxr9WabUl+NjSdwmYdIZpNz3c6Q5IYuCCHY2rUb6Sd1w= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1332195298; bh=fFegHyrtpkHr3vj6inDJR1cNR2ojHMsDDpX4Q/PjMAc=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:Cc:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=6KQdpwIlZyGSxhwGbBiwGrr7RhPm2voDP+7ix/zEiNH3DQwJ2T5C4va97U5zREZbp7ZIM5j1eX2HakkvUhPJ4JMV8GIRdIgECb0UBW2c4Wpt5aKXm7KprhC1qGbXjPm2N5/1tA5OQEHdf2VehaSX9LxmxinQiK3P0hWU33ukAlU= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: EecJHH8VM1mBO3t1n_3aDqG4uh2nQ3WnO19YZbbN3OUc8w0 pBRRpgPs5NL8QXOERNNnHGhnmV63ecvSYuwAGjjIOw97oz2r5n3jWMQZ8HwY Fp8.smyHV0OHJyUS3grCoUBvXM5bWFjKvEu7H5SpEeEmF1aaO.7mNp4TxUrj FRA8_Bwu75ZeSB9tbtc5oR5ENyTPZnO8Aq_8MWjl3RIDKbxx9YdRADc63G0l gR_v2FTVHs2WMz_ZNx.plKZYkbQc706USG5vmpY5oyC5uInC7gZ5gf8t_HCH jS2NAeBOS2gON8oDOnk1M7VyqOE3l_FBZ.0TBN4QJEOnW.llwTS51lS9WSf2 xceu5a.juUQJHxs0QKy.xT19m.NNbr.KokwkcDV0iFSMrBZrUTuNU3wrAZf0 j1FckT8swYSNfh9ZelvJ5eByy7h42e817PkEcYnqh80184Nn.OndzFLvOfpE 6TKNfjMcuC73ZJ6UzViQEEzQV X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp110.prem.mail.ac4.yahoo.com with SMTP; 19 Mar 2012 15:14:58 -0700 PDT From: "David Johnston" To: "'Richard Huxton'" Cc: "'Rehan Saleem'" , References: <1332186006.44650.YahooMailNeo@web121605.mail.ne1.yahoo.com> <00b101cd060b$6f87a560$4e96f020$@yahoo.com> <4F679E2B.2010207@archonet.com> In-Reply-To: <4F679E2B.2010207@archonet.com> Subject: Re: Postgresql function which compares values from both tables Date: Mon, 19 Mar 2012 18:14:53 -0400 Message-ID: <00dc01cd061d$b6381780$22a84680$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQHFL+j3y9jTbY3Yw2YNdysepRDFFwK4N0GnAjWVjryWWmxNwA== Content-Language: en-us X-Pg-Spam-Score: -2.0 (--) X-Archive-Number: 201203/51 X-Sequence-Number: 36446 > -----Original Message----- > From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql- > owner@postgresql.org] On Behalf Of Richard Huxton > Sent: Monday, March 19, 2012 4:59 PM > To: David Johnston > Cc: 'Rehan Saleem'; pgsql-sql@postgresql.org > Subject: Re: [SQL] Postgresql function which compares values from both > tables > > On 19/03/12 20:04, David Johnston wrote: > > You would need to install the "HSTORE" extension to convert the record > > into a key->value pair then perform the comparison on that. > > No, you can do this just fine with a simple join. > > BEGIN; > > CREATE TABLE ta (id int PRIMARY KEY, d date, t text); CREATE TABLE tb (id int > PRIMARY KEY, d date, t text); > > INSERT INTO ta > SELECT 100+n, '2001-01-01'::date + n, 'entry ' || n FROM > generate_series(1,19) n; > > INSERT INTO tb > SELECT 200+n, '2001-01-01'::date + n, 'entry ' || n FROM > generate_series(1,19) n; > > SELECT ta.id AS id_a, tb.id AS id_b, ta.d, ta.t FROM ta JOIN tb USING (d,t); > > ROLLBACK; > > If the fields were named differently you'd need something like: > FROM ta JOIN tb ON (ta.d1, ta.t1) = (tb.d2, tb.t2) > I think I read into the question too much. If you want to find matching rows then ( ROW(alias_1.*) = ROW(alias_2.*) ) will work (and you do not even have to specify the columns explicitly). Read this for details. Note that if any of the columns can be NULL you need to be especially careful. http://www.postgresql.org/docs/9.0/interactive/functions-comparisons.html I was thinking that you wanted to know which specific columns matched even if the row as a whole did not. Dave J.