Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id DEFAE2E3C83 for ; Wed, 16 Jan 2008 11:54:50 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 34997-03 for ; Wed, 16 Jan 2008 11:54:39 -0400 (AST) Received: from rv-out-0910.google.com (rv-out-0910.google.com [209.85.198.188]) by postgresql.org (Postfix) with ESMTP id E6A692E3C25 for ; Wed, 16 Jan 2008 11:54:44 -0400 (AST) Received: by rv-out-0910.google.com with SMTP id l15so285712rvb.43 for ; Wed, 16 Jan 2008 07:54:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:organization:to:subject:date:user-agent:cc:references:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:message-id; bh=i0IG8/dRjiFCFAP3PDa+GUwzlRB20a/F8StQiO/UzIQ=; b=CDmcpbpDd8w87k+UCJYHA8KfJFVv/TdZdg57Yl1IXcXmdXgsJ0laC5h1jZa+pG1MaW/eRz95UBueGR95PIfLVKCY9LDz8wmklxj0moSMIWYCSQHj9z14B4WJpnp9sPNsWrrAvBeETs9iaPle0u8QRPCTgZq0LEplygYhZ6yWjmg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:organization:to:subject:date:user-agent:cc:references:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:message-id; b=nDQZzLm23gcUA9xFct/O6l2c53R8RrxCh7t4CTKfP8LZOI2Fkbh0+S18jXwAHAVA6QCEUDp/yczVktVTexRw1rRGOI0ufhKmOzNm9fKvIA4hACTbxwALjgnh+nPrl33k4C6C1klIRsyDLs5nDD64K2xSYJzBnR72914nIiJHBfg= Received: by 10.141.198.9 with SMTP id a9mr641011rvq.280.1200498883154; Wed, 16 Jan 2008 07:54:43 -0800 (PST) Received: from ?192.168.1.5? ( [98.207.129.115]) by mx.google.com with ESMTPS id f42sm2655525rvb.13.2008.01.16.07.54.42 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 16 Jan 2008 07:54:42 -0800 (PST) From: Bryan Emrys Organization: Andras Hill To: "Rodrigo E. De =?utf-8?q?Le=C3=B3n?= Plicet" Subject: Re: SQL dealing with subquery Date: Wed, 16 Jan 2008 07:54:41 -0800 User-Agent: KMail/1.9.7 Cc: pgsql-sql@postgresql.org References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200801160754.41769.bryan.emrys@gmail.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/141 X-Sequence-Number: 29994 Thanks. It throws off a few extra countries where there is only one treaty,= but those are=20 few enough that I can handle them manually. I thought the solution was also going to give me insight into how to select= just the lowest=20 rate from each couple, (i.e. for each payor, who is the lowest rate payee) = but it looks like=20 I'll have find some time to think about that later (I've also got to think = about what to do in tie situations.). Again, thanks. Bryan On Tuesday 15 January 2008 12:40:13 pm Rodrigo E. De Le=C3=B3n Plicet wrote: > On Jan 15, 2008 1:04 PM, Bryan Emrys wrote: > > In other words, in the sample above, I only want to return: > > 'Canada','Ireland',0 > > 'Canada','Netherlands',5 >=20 > Try (untested): >=20 > SELECT t2.* > FROM (SELECT payor > FROM treaty_rates > WHERE payee IN ('Netherlands', 'Ireland') > GROUP BY payor > HAVING MIN (rate) !=3D MAX (rate)) t1 > JOIN > treaty_rates t2 ON t1.payor =3D t2.payor > WHERE t2.payee IN ('Netherlands', 'Ireland'); >=20