Re: SQL dealing with subquery

From: "Bryan Emrys" <bryan(dot)emrys(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL dealing with subquery
Date: 2008-01-16 17:34:53
Message-ID: eee4647d0801160934n3efe1e28h5a7c1e1cb5edf1ad@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Following up my treaty rate thoughts, if I'm trying to get the lowest treaty
payee (and rate) from a specific list of payees for every possible payor
country, the following seems to work, but is it right? I'm specifically
wondering about the group by clauses. (Or if there is a better way.) [table
treaties has columns payor,payee,rate and in this sample, I'm just trying to
find which country payee between Ireland and the Netherlands has the lowest
rate from each individual payor country.]

select a.payor,a.payee,a.rate
from treaties a,

(select payor,min(rentr) from treaties
where payee in ('Ireland','Netherlands') group by payor
) b

where a.payor=b.payor
and a.rate = b.min
and payee in ('Ireland','Netherlands')
group by a.payor, a.payee, a.rate

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Franklin Haut 2008-01-16 18:07:32 Re: Update PK Violation
Previous Message Scott Marlowe 2008-01-16 16:15:48 Re: Update PK Violation