pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
left outer join on more than 2 tables?
8+ messages / 4 participants
[nested] [flat]

* left outer join on more than 2 tables?
@ 2009-06-16 20:59  Carol Cheung <cacheung@consumercontact.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Carol Cheung @ 2009-06-16 20:59 UTC (permalink / raw)
  To: pgsql-sql

Hi,
I have 3 tables

region:
id
region_name

city:
id
city_name
region_id

complaint:
id
date
city_id

I would like to find the counts of complaints by region and I would like 
all regions to be displayed, regardless of whether or not complaints 
exist for that region. Is left outer join what I'm looking for?
I'm stuck at this point:
select r.region_name, count(1) from region r left outer join city c, 
complaint k on (k.city_id = c.id and r.id = c.region_id) group by 
r.region_name
Of course this doesn't work ...
Can anyone provide their insight as to how I can achieve this?

Thanks in advance,
C



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: left outer join on more than 2 tables?
@ 2009-06-16 21:25  Richard Broersma <richard.broersma@gmail.com>
  parent: Carol Cheung <cacheung@consumercontact.com>
  0 siblings, 2 replies; 8+ messages in thread

From: Richard Broersma @ 2009-06-16 21:25 UTC (permalink / raw)
  To: Carol Cheung <cacheung@consumercontact.com>; +Cc: pgsql-sql

On Tue, Jun 16, 2009 at 1:59 PM, Carol
Cheung<cacheung@consumercontact.com> wrote:
> I would like to find the counts of complaints by region and I would like all
> regions to be displayed, regardless of whether or not complaints exist for
> that region. Is left outer join what I'm looking for?

   SELECT R.region_name, Count(*) AS RegionComplaints
     FROM Region AS R
LEFT JOIN City AS Ci
       ON R.id = C.region_id
LEFT JOIN Complaint AS Cm
       ON Ci.id = Cm.city_id
 GROUP BY R.region_name;


-- 
Regards,
Richard Broersma Jr.

Visit the Los Angeles PostgreSQL Users Group (LAPUG)
http://pugs.postgresql.org/lapug



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: left outer join on more than 2 tables?
@ 2009-06-16 21:39  Gavin Baumanis <gavinb@eclinic.com.au>
  parent: Richard Broersma <richard.broersma@gmail.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Gavin Baumanis @ 2009-06-16 21:39 UTC (permalink / raw)
  To: Richard Broersma <richard.broersma@gmail.com>; +Cc: Carol Cheung <cacheung@consumercontact.com>; pgsql-sql

At the risk of being wrong.... (I'm always ready to learn something  
new) - and seemingly I'm only too happy to be wrong!...

And... it might even be that it is exactly the same result - but I  
would have proposed;

SELECT
     R.region_name,
     Count(*) AS RegionComplaints
FROM
     Region AS R
         LEFT JOIN City AS Ci
             LEFT JOIN Complaint AS Cm ON Ci.id = Cm.city_id
       ON R.id = C.region_id
GROUP BY
     R.region_name;


Gavin.


On 17/06/2009, at 7:25 AM, Richard Broersma wrote:

> On Tue, Jun 16, 2009 at 1:59 PM, Carol
> Cheung<cacheung@consumercontact.com> wrote:
>> I would like to find the counts of complaints by region and I would  
>> like all
>> regions to be displayed, regardless of whether or not complaints  
>> exist for
>> that region. Is left outer join what I'm looking for?
>
>   SELECT R.region_name, Count(*) AS RegionComplaints
>     FROM Region AS R
> LEFT JOIN City AS Ci
>       ON R.id = C.region_id
> LEFT JOIN Complaint AS Cm
>       ON Ci.id = Cm.city_id
> GROUP BY R.region_name;
>
>
> -- 
> Regards,
> Richard Broersma Jr.
>
> Visit the Los Angeles PostgreSQL Users Group (LAPUG)
> http://pugs.postgresql.org/lapug
>
> -- 
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql

As always Please contact me if I can be of any further assistance.

Gavin "Beau" Baumanis
Senior Application Developer
PalCare P/L

657 Nicholson Street
Carlton North
Victoria, Australia, 3054

E: beau@palcare.com.au
P: +61 -3 9380 3513
M: +61 -438 545 586
W: http://www.palcare.com.au



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: left outer join on more than 2 tables?
@ 2009-06-16 21:40  Rob Sargent <robjsargent@gmail.com>
  parent: Richard Broersma <richard.broersma@gmail.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Rob Sargent @ 2009-06-16 21:40 UTC (permalink / raw)
  To: pgsql-sql

Richard Broersma wrote:
> On Tue, Jun 16, 2009 at 1:59 PM, Carol
> Cheung<cacheung@consumercontact.com> wrote:
>   
>> I would like to find the counts of complaints by region and I would like all
>> regions to be displayed, regardless of whether or not complaints exist for
>> that region. Is left outer join what I'm looking for?
>>     
>
>    SELECT R.region_name, Count(*) AS RegionComplaints
>      FROM Region AS R
> LEFT JOIN City AS Ci
>        ON R.id = C.region_id
> LEFT JOIN Complaint AS Cm
>        ON Ci.id = Cm.city_id
>  GROUP BY R.region_name;
>
>
>   
Is there a city without a reference to region?  And wouldn't you want to 
count(cm.id)?



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: left outer join on more than 2 tables?
@ 2009-06-16 22:45  Richard Broersma <richard.broersma@gmail.com>
  parent: Gavin Baumanis <gavinb@eclinic.com.au>
  0 siblings, 0 replies; 8+ messages in thread

From: Richard Broersma @ 2009-06-16 22:45 UTC (permalink / raw)
  To: Gavin Baumanis <gavinb@eclinic.com.au>; +Cc: Carol Cheung <cacheung@consumercontact.com>; pgsql-sql

On Tue, Jun 16, 2009 at 2:39 PM, Gavin Baumanis<gavinb@eclinic.com.au> wrote:
> At the risk of being wrong.... (I'm always ready to learn something new) -
> and seemingly I'm only too happy to be wrong!...
>
> And... it might even be that it is exactly the same result - but I would
> have proposed;
>
> SELECT
>    R.region_name,
>    Count(*) AS RegionComplaints
> FROM
>    Region AS R
>        LEFT JOIN City AS Ci
>            LEFT JOIN Complaint AS Cm ON Ci.id = Cm.city_id
>      ON R.id = C.region_id
> GROUP BY
>    R.region_name;

Yup, it produces the same result.


-- 
Regards,
Richard Broersma Jr.

Visit the Los Angeles PostgreSQL Users Group (LAPUG)
http://pugs.postgresql.org/lapug



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: left outer join on more than 2 tables?
@ 2009-06-16 22:50  Richard Broersma <richard.broersma@gmail.com>
  parent: Rob Sargent <robjsargent@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Richard Broersma @ 2009-06-16 22:50 UTC (permalink / raw)
  To: Rob Sargent <robjsargent@gmail.com>; +Cc: pgsql-sql

On Tue, Jun 16, 2009 at 2:40 PM, Rob Sargent<robjsargent@gmail.com> wrote:

> Is there a city without a reference to region?

I don't know, but the OP wanted to know complaints by region.


>  And wouldn't you want to count(cm.id)?

Count(cm.id) and Count(*) produce the same result.  But I like
Count(*) more since it helps to correctly express the idea that we are
counting rows per group and not cm.id(s) per group.



-- 
Regards,
Richard Broersma Jr.

Visit the Los Angeles PostgreSQL Users Group (LAPUG)
http://pugs.postgresql.org/lapug



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: left outer join on more than 2 tables?
@ 2009-06-16 23:12  Rob Sargent <robjsargent@gmail.com>
  parent: Richard Broersma <richard.broersma@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Rob Sargent @ 2009-06-16 23:12 UTC (permalink / raw)
  To: Richard Broersma <richard.broersma@gmail.com>; pgsql-sql

Richard Broersma wrote:
> On Tue, Jun 16, 2009 at 2:40 PM, Rob Sargent<robjsargent@gmail.com> wrote:
>
>   
>> Is there a city without a reference to region?
>>     
>
> I don't know, but the OP wanted to know complaints by region.
>
>   
I didn't try this, but with regionless cities, you may need a full join
if you want a complete accounting of all complaints, some being logged
to the null region.


>
>
>   
>>  And wouldn't you want to count(cm.id)?
>>     
>
> Count(cm.id) and Count(*) produce the same result.  But I like
> Count(*) more since it helps to correctly express the idea that we are
> counting rows per group and not cm.id(s) per group.
>
>
>
>   

"Same result" is not true.  I loaded tables. 

Using count(*) you get count=1 for regions without complaints.  Using 
count(complaint.id) you get count = 0.  (The deference amount to 
counting the left hand side (region) vs the right hand side (complaint) 
which I believe is what OP is after).





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: left outer join on more than 2 tables?
@ 2009-06-17 13:07  Carol Cheung <cacheung@consumercontact.com>
  parent: Rob Sargent <robjsargent@gmail.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Carol Cheung @ 2009-06-17 13:07 UTC (permalink / raw)
  To: pgsql-sql


On 16/06/2009 19:12, Rob Sargent wrote the following:
> Richard Broersma wrote:
>> On Tue, Jun 16, 2009 at 2:40 PM, Rob Sargent<robjsargent@gmail.com> 
>> wrote:
>>
>>  
>>> Is there a city without a reference to region?
>>>     
>>
>> I don't know, but the OP wanted to know complaints by region.
>>
>>   
> I didn't try this, but with regionless cities, you may need a full join
> if you want a complete accounting of all complaints, some being logged
> to the null region.
> 
> 
>>
>>
>>  
>>>  And wouldn't you want to count(cm.id)?
>>>     
>>
>> Count(cm.id) and Count(*) produce the same result.  But I like
>> Count(*) more since it helps to correctly express the idea that we are
>> counting rows per group and not cm.id(s) per group.
>>
>>
>>
>>   
> 
> "Same result" is not true.  I loaded tables.
> Using count(*) you get count=1 for regions without complaints.  Using 
> count(complaint.id) you get count = 0.  (The deference amount to 
> counting the left hand side (region) vs the right hand side (complaint) 
> which I believe is what OP is after).
> 

Thanks everyone for your help. Your solutions worked. Much appreciated.
- Carol



^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2009-06-17 13:07 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-06-16 20:59 left outer join on more than 2 tables? Carol Cheung <cacheung@consumercontact.com>
2009-06-16 21:25 ` Richard Broersma <richard.broersma@gmail.com>
2009-06-16 21:39   ` Gavin Baumanis <gavinb@eclinic.com.au>
2009-06-16 22:45     ` Richard Broersma <richard.broersma@gmail.com>
2009-06-16 21:40   ` Rob Sargent <robjsargent@gmail.com>
2009-06-16 22:50     ` Richard Broersma <richard.broersma@gmail.com>
2009-06-16 23:12       ` Rob Sargent <robjsargent@gmail.com>
2009-06-17 13:07         ` Carol Cheung <cacheung@consumercontact.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox