X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 04AF99FA12F for ; Thu, 1 Jun 2006 21:01:04 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 84169-03 for ; Thu, 1 Jun 2006 21:00:58 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id B92869FA040 for ; Thu, 1 Jun 2006 21:00:58 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id k5200qmj026605 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 1 Jun 2006 18:00:55 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.6/8.13.6) with ESMTP id k5200q0o012309; Thu, 1 Jun 2006 18:00:52 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.6/8.13.6/Submit) id k5200qSe012308; Thu, 1 Jun 2006 18:00:52 -0600 (MDT) (envelope-from mfuhr) Date: Thu, 1 Jun 2006 18:00:52 -0600 From: Michael Fuhr To: operationsengineer1@yahoo.com Cc: pgsql-sql@postgresql.org Subject: Re: Advanced Query Message-ID: <20060602000051.GA12215@winnie.fuhr.org> References: <20060601230921.92601.qmail@web33305.mail.mud.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060601230921.92601.qmail@web33305.mail.mud.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/32 X-Sequence-Number: 25268 On Thu, Jun 01, 2006 at 04:09:21PM -0700, operationsengineer1@yahoo.com wrote: > what i can't seem to do is to get both - a count of > the total number of t_inspect_result.inspect_pass > where the value is true and a total count, by unique > t_inspect.id. Are you looking for something like this? SELECT 1.0 * sum(CASE WHEN inspect_pass THEN 1 ELSE 0 END) / count(*) FROM ( SELECT DISTINCT ON (inspect_id) inspect_id, inspect_pass FROM t_inspect_result ORDER BY inspect_id, id ) AS s; Multiply by 100.0 instead of 1.0 if you want percent. If you have a cast from boolean to integer (built-in in 8.1, easily created in earlier versions) then you could replace the CASE expression with a cast (inspect_pass::integer). Whether to use the more explicit CASE or the more concise cast is a matter of style. -- Michael Fuhr