Received: from megazone.bigpanda.com ([63.150.15.178]) by postgresql.org (8.11.3/8.11.4) with ESMTP id fB5M5Wm06854 for ; Wed, 5 Dec 2001 17:05:32 -0500 (EST) (envelope-from sszabo@megazone23.bigpanda.com) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 9E805D5EB; Wed, 5 Dec 2001 14:05:33 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 922D25BEA; Wed, 5 Dec 2001 14:05:33 -0800 (PST) Date: Wed, 5 Dec 2001 14:05:33 -0800 (PST) From: Stephan Szabo To: Roberto Mello Cc: Subject: Re: Getting matching and non-matching results (long) In-Reply-To: <20011205145435.A9169@cc.usu.edu> Message-ID: <20011205135740.J18043-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Archive-Number: 200112/62 X-Sequence-Number: 5885 On Wed, 5 Dec 2001, Roberto Mello wrote: > On Wed, Dec 05, 2001 at 01:07:20PM -0800, Stephan Szabo wrote: > > > > > > SELECT COUNT(incident_id), drug_name, grade_name > > > FROM sds_offenders o, sds_drugs d, sds_drug_offenses do, sds_grades g > > > WHERE o.drug_p = 't' > > > AND o.offender_id = do.offender_id > > > AND d.drug_id = do.drug_id > > > GROUP BY drug_name, grade_name, d.sort_key > > > ORDER BY d.sort_key > > > > I think you need a > > g.gradeid=o.gradeid > > in the where clause as well to constrain g to > > the grade for which the offender belonged, right? > > Yes, I figured this mistake minutes after sending the message to the list. > The problem is that with g.grade_id = o.grade_id there it gives me _only_ > the grades that have incidents in them, instead of _all_ the grades with > 0's for those without incidents. Right, then you will want an outer join, probably something like: select count(incident_id), drug_name, grade_name from ((sds_offenders o inner join sds_drug_offenses dro using (offender_id)) inner join sds_drugs d using (drug_id)) right join sds_grades using (grade_id) where o.drug_p='t' group by drug_name, grade_name, d.sort_key order by d.sort_key;