Unix Technical Forum

SQL dealing with subquery comparison

This is a discussion on SQL dealing with subquery comparison within the pgsql Sql forums, part of the PostgreSQL category; --> Hi all, I'm having a conceptual problem with a subquery here - any help would be appreciated. I have ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 06:00 PM
Bryan Emrys
 
Posts: n/a
Default SQL dealing with subquery comparison

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 06:00 PM
shakahshakah
 
Posts: n/a
Default Re: SQL dealing with subquery comparison

On Jan 15, 2:02 pm, bryan.em...@gmail.com ("Bryan Emrys") wrote:
> 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


I'm unclear on exactly what you're asking as it seems that several
result sets would satisfy your example given your data, but does
DISTINCT ON help you?

BEGIN ;

CREATE TABLE treaty_rates ( payor text, payee text, rate integer ) ;

INSERT INTO treaty_rates VALUES ('US','UK',5) ;
INSERT INTO treaty_rates VALUES ('US','Ireland',5) ;
INSERT INTO treaty_rates VALUES ('US','Netherlands',5) ;
INSERT INTO treaty_rates VALUES ('US','China',10) ;
INSERT INTO treaty_rates VALUES ('Canada','US',0) ;
INSERT INTO treaty_rates VALUES ('Canada','Ireland',0) ;
INSERT INTO treaty_rates VALUES ('Canada','Netherlands',5) ;
INSERT INTO treaty_rates VALUES ('Canada','UK',5) ;
INSERT INTO treaty_rates VALUES ('Belgium','Netherlands',0) ;

SELECT DISTINCT ON (tr.rate) tr.payor, tr.payee, tr.rate
FROM treaty_rates tr
WHERE tr.payee IN ('Netherlands','Ireland')
ORDER BY tr.rate, 1, 2 ;

SELECT DISTINCT ON (tr.payee,tr.rate) tr.payor, tr.payee, tr.rate
FROM treaty_rates tr
WHERE tr.payee IN ('Netherlands','Ireland')
ORDER BY tr.payee, tr.rate, 1 ;

payor | payee | rate
---------+-------------+------
Belgium | Netherlands | 0
Canada | Netherlands | 5
(2 rows)

payor | payee | rate
---------+-------------+------
Canada | Ireland | 0
US | Ireland | 5
Belgium | Netherlands | 0
Canada | Netherlands | 5
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:35 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com