Re: Advanced Query - Mailing list pgsql-sql

From Michael Fuhr
Subject Re: Advanced Query
Date
Msg-id 20060602000051.GA12215@winnie.fuhr.org
Whole thread Raw
In response to Advanced Query  (<operationsengineer1@yahoo.com>)
Responses Re: Advanced Query
List pgsql-sql
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


pgsql-sql by date:

Previous
From: postgres@jal.org
Date:
Subject: Re: Table design question
Next
From:
Date:
Subject: Re: Advanced Query