Join question - Mailing list pgsql-sql

From Edward W. Rouse
Subject Join question
Date
Msg-id 04b101c8fef6$b3c87d50$1b5977f0$@com
Whole thread Raw
Responses Re: Join question
Re: Join question
List pgsql-sql
<div class="Section1"><p class="MsoNormal">I have 2 tables, both have a user column. I am currently using a left join
fromtable a to table b because I need to show all users from table a even those not having an entry in table b. The
problemis I also have to include items from table b with that have a null user. There are some other criteria as well
thatare simple where clause filters. So as an example:<p class="MsoNormal"> <p class="MsoNormal">Table a:<p
class="MsoNormal">Org|user<pclass="MsoNormal">A    | emp1<p class="MsoNormal">B    | emp1<p class="MsoNormal">B    |
emp2<pclass="MsoNormal">B    | emp3<p class="MsoNormal">C    | emp2<p class="MsoNormal"> <p class="MsoNormal">Table
b:<pclass="MsoNormal">Org|user|color<p class="MsoNormal">A   |emp1|red<p class="MsoNormal">A   |emp1|blue<p
class="MsoNormal">A  |null|pink<p class="MsoNormal">A   |null|orange<p class="MsoNormal">B   |emp1|red<p
class="MsoNormal">B  |emp3|red<p class="MsoNormal">B   |null|silver<p class="MsoNormal">C   |emp2|avacado<p
class="MsoNormal"> <pclass="MsoNormal">If I:<p class="MsoNormal"> <p class="MsoNormal">select org, user, count(total)<p
class="MsoNormal">froma left join b<p class="MsoNormal">on (a.org = b.org and a.user = b.user)<p
class="MsoNormal">wherea.org = ‘A’<p class="MsoNormal">group by a.org, a.user<p class="MsoNormal">order by a.org,
a.user<pclass="MsoNormal"> <p class="MsoNormal">I get:<p class="MsoNormal"> <p class="MsoNormal">Org|user|count<p
class="MsoNormal">A   |emp1|2<p class="MsoNormal">A    |emp2|0<p class="MsoNormal">A    |emp3|0<p class="MsoNormal"> <p
class="MsoNormal">Butwhat I need is:<p class="MsoNormal"> <p class="MsoNormal">A    |emp1|2<p class="MsoNormal">A
   |emp2|0<pclass="MsoNormal">A    |emp3|0<p class="MsoNormal">A    |null|2<p class="MsoNormal"> <p
class="MsoNormal">Thanks,<pclass="MsoNormal">Edward W. Rouse</div> 

pgsql-sql by date:

Previous
From: Emi Lu
Date:
Subject: Re: Check a column value not in Array.
Next
From: "Edward W. Rouse"
Date:
Subject: Re: Join question