SQL dealing with subquery

Поиск
Список
Период
Сортировка
Искать

SQL dealing with subquery

От:
"Bryan Emrys" <bryan.emrys@gmail.com>
Дата:
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.

Bryan

Re: SQL dealing with subquery

От:
"Rodrigo E. De León Plicet" <rdeleonp@gmail.com>
Дата:
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

Try (untested):

SELECT t2.*
FROM   (SELECT   payor       FROM     treaty_rates       WHERE    payee IN ('Netherlands', 'Ireland')       GROUP BY payor       HAVING   MIN (rate) != MAX (rate)) t1      JOIN      treaty_rates t2 ON t1.payor = t2.payor
WHERE  t2.payee IN ('Netherlands', 'Ireland');

Re: SQL dealing with subquery

От:
Bryan Emrys <bryan.emrys@gmail.com>
Дата:
Thanks. It throws off a few extra countries where there is only one treaty, but those are 
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 
rate from each couple, (i.e. for each payor, who is the lowest rate payee) but it looks like 
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ón 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
> 
> Try (untested):
> 
> SELECT t2.*
> FROM   (SELECT   payor
>         FROM     treaty_rates
>         WHERE    payee IN ('Netherlands', 'Ireland')
>         GROUP BY payor
>         HAVING   MIN (rate) != MAX (rate)) t1
>        JOIN
>        treaty_rates t2 ON t1.payor = t2.payor
> WHERE  t2.payee IN ('Netherlands', 'Ireland');
> 



Re: SQL dealing with subquery

От:
"Bryan Emrys" <bryan.emrys@gmail.com>
Дата:

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
FAQ