Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 147BC1337B3E for ; Sun, 3 Oct 2010 15:54:54 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 74205-03 for ; Sun, 3 Oct 2010 18:54:46 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from blu0-omc2-s5.blu0.hotmail.com (blu0-omc2-s5.blu0.hotmail.com [65.55.111.80]) by mail.postgresql.org (Postfix) with ESMTP id C894B1337BB1 for ; Sun, 3 Oct 2010 15:54:45 -0300 (ADT) Received: from BLU0-SMTP93 ([65.55.111.71]) by blu0-omc2-s5.blu0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Sun, 3 Oct 2010 11:54:44 -0700 X-Originating-IP: [174.91.3.43] X-Originating-Email: [fbax@sympatico.ca] Message-ID: Received: from t60p.bax.ca ([174.91.3.43]) by BLU0-SMTP93.blu0.hotmail.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Sun, 3 Oct 2010 11:54:43 -0700 Date: Sun, 03 Oct 2010 14:54:41 -0400 From: Frank Bax User-Agent: Thunderbird 2.0.0.23 (X11/20100309) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: join returns too many results... Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 03 Oct 2010 18:54:43.0474 (UTC) FILETIME=[70F0EF20:01CB632C] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-5 required=5 tests=BAYES_00=-1.9, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201010/6 X-Sequence-Number: 34657 When I join tables; I will sometimes get multiple rows back as in this example. create table class(name varchar, p point, d int); insert into class values( 'All', point(50,50), 100 ); insert into class values( 'NE70', point(70,70), 20 ); insert into class values( 'NE75', point(75,75), 20 ); insert into class values( 'NE80', point(80,80), 20 ); insert into class values( 'Centre', point(50,50), 5 ); create table list(x varchar, p point); insert into list values('A',point(10,10)); insert into list values('B',point(75,75)); insert into list values('C',point(51,51)); select x,name,class.p<->list.p as dist, class.d as size from list left join class on list.p <@ circle(class.p,class.d); x | name | dist | size ---+--------+------------------+------ A | All | 56.5685424949238 | 100 B | All | 35.3553390593274 | 100 B | NE75 | 0 | 20 B | NE70 | 7.07106781186548 | 20 B | NE80 | 7.07106781186548 | 20 C | All | 1.4142135623731 | 100 C | Centre | 1.4142135623731 | 5 In the case where multiple rows are returned from class; I will need to add another condition which decides which row to return. 1) distance from centre 2) size of circle Whatever clause I choose to add I *must* ensure that final result set contains only one-to-one join between tables. Either of these two results is acceptable: For option 1; result C=All or C=Centre is acceptable. For option 2; result B=NE70 or B=NE75 or B=NE80 is acceptable. I am lost trying to construct SQL to accomplish this...