Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 4F0812E31F5 for ; Tue, 15 Jan 2008 15:03:05 -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 14704-01 for ; Tue, 15 Jan 2008 15:02:57 -0400 (AST) Received: from rv-out-0910.google.com (rv-out-0910.google.com [209.85.198.191]) by postgresql.org (Postfix) with ESMTP id 5733D2E0077 for ; Tue, 15 Jan 2008 15:02:58 -0400 (AST) Received: by rv-out-0910.google.com with SMTP id l15so2576832rvb.43 for ; Tue, 15 Jan 2008 11:02:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type; bh=S5y9s9qIEBB6UuJO5jck/kN0pxle09v/UyolEzm4zks=; b=HZwxMAyf66IX60+Lb6kC6i5f2kmdJ0EnBLnCfPzSQ+kdZDOfPVuXQdM435jxrqmCsJDgeBYNIfixNHtsHakBdk1RnDZykQNXE1uITOztPDhRKjDVaUdt2sJTQTTa5GBeAjnQjRSU9RfUKhUy6QPdvK57gNKFAH3adMMVWXTSwsQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type; b=rnVgy6wDZFUQ4ZDPEtQUjcxwXHjFYeA/Rt45Ey1fxjudjIx/wDEpyVA4MIVHucI1HvdXL6mzTlbGOy6FKRV4VGotOqRPgKU/XsK2RowmRF5qR6+AY9oBibMJN7mI3RKq+Kml13B6zsLLviNCkMyjMwbLD7700rdBniAKl3SnZN8= Received: by 10.140.142.4 with SMTP id p4mr4825872rvd.261.1200423776217; Tue, 15 Jan 2008 11:02:56 -0800 (PST) Received: by 10.141.84.8 with HTTP; Tue, 15 Jan 2008 11:02:56 -0800 (PST) Message-ID: Date: Tue, 15 Jan 2008 11:02:56 -0800 From: "Bryan Emrys" To: pgsql-sql@postgresql.org Subject: SQL dealing with subquery comparison MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_16779_533514.1200423776210" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/132 X-Sequence-Number: 29985 ------=_Part_16779_533514.1200423776210 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi all, I'm having a conceptual problem with a subquery here - any help would be appreciated. I have a table treaty_rates with columns payor, payee, rate where payor and payee are countries. Sample set: 'US','UK',5 'US','Ireland',5 'US','Netherlands',5 'US','China',10 'Canada','US',0 'Canada','Ireland',0 'Canada','Netherlands',5 'Canada','UK,5 'Belgium','Netherlands',0 There is no certainty that the same payees exist for each payor (i.e. not all countries have treaties with other all other countries) I want to select all rows where the payee is in some array (e.g, in ('Netherlands','Ireland')) and the rate is not the same In other words, in the sample above, I only want to return: 'Canada','Ireland',0 'Canada','Netherlands',5 The initial query to limit the rows to the specified payees is obvious, but I can't get my mind around how to compare each set of rows where the payor is the same country. Running select payor, payee, rate where payee in ('Netherlands','Ireland') will return 132 rows (66 pairs). Any suggestions on how to compare the pairs would be appreciated. (Obviously I could also run into someone asking me for more than 2 payees and asking for the combination payor/payee with the lowest rate for each individual payor). Thanks, Bryan ------=_Part_16779_533514.1200423776210 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi all,
I'm having a conceptual problem with a subquery here - any help would be appreciated.

I have a table treaty_rates with columns payor, payee, rate where payor and payee are countries.
Sample set:
'US','UK',5
'US','Ireland',5
'US','Netherlands',5
'US','China',10
'Canada','US',0
'Canada','Ireland',0
'Canada','Netherlands',5
'Canada','UK,5
'Belgium','Netherlands',0

There is no certainty  that the same payees exist for each payor (i.e. not all countries have treaties with other all other countries)

I want to select all rows where the payee is in some array (e.g, in ('Netherlands','Ireland')) and the rate is not the same

In other words, in the sample above, I only want to return:
'Canada','Ireland',0
'Canada','Netherlands',5

The initial query to limit the rows to the specified payees is obvious, but I can't get my mind around how to compare each set of rows where the payor is the same country. Running select payor, payee, rate where payee in ('Netherlands','Ireland') will return 132 rows (66 pairs).

Any suggestions on how to compare the pairs would be appreciated. (Obviously I could also run into someone asking me for more than 2 payees and asking for the combination payor/payee with the lowest rate for each individual payor).

Thanks,

Bryan
------=_Part_16779_533514.1200423776210--