| From: | Bill Moseley <moseley(at)hank(dot)org> |
|---|---|
| To: | pgsql-general(at)postgresql(dot)org |
| Subject: | Re: SQL Help: Multiple LEFT OUTER JOINs |
| Date: | 2005-11-22 00:26:39 |
| Message-ID: | 20051122002639.GA32241@hank.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-general |
On Mon, Nov 21, 2005 at 02:06:35PM -0800, Bill Moseley wrote:
> The now working query (thanks to you!) is:
No that doesn't work. It's dropping the people that have never
been assigned a class to teach (i.e. don't have a row in the
"instructors" link table).
> FROM class INNER JOIN instructors ON class.id = instructors.class
> LEFT OUTER JOIN person ON person.id = instructors.person,
> person_role
I really seem to need the multiple left outer join. This works:
SELECT person.id AS id, last_name,
person_role.role AS role,
count(instructors.class),
sum (CASE WHEN class_time > now() THEN 1 ELSE 0 END) as future_class_count,
sum (CASE WHEN class_time <= now() THEN 1 ELSE 0 END) as past_class_count
FROM person LEFT OUTER JOIN instructors ON (person.id = instructors.person)
LEFT OUTER JOIN class ON (instructors.class = class.id),
person_role
WHERE person_role.person = person.id
-- AND person_role.role = 2
GROUP BY person.id, last_name, person_role.role;
I'm not clear how to move that "person_role.person = person.id" into
the FROM statement. Does it matter?
--
Bill Moseley
moseley(at)hank(dot)org
| From | Date | Subject | |
|---|---|---|---|
| Next Message | John McCawley | 2005-11-22 00:49:08 | Re: SQL Help: Multiple LEFT OUTER JOINs |
| Previous Message | Guy Rouillier | 2005-11-22 00:25:55 | Re: Group By? |