pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Fuhr <mike@fuhr.org>
To: operationsengineer1@yahoo.com
Cc: pgsql-sql@postgresql.org
Subject: Re: Advanced Query
Date: Thu, 1 Jun 2006 18:00:52 -0600
Message-ID: <20060602000051.GA12215@winnie.fuhr.org> (raw)
In-Reply-To: <20060601230921.92601.qmail@web33305.mail.mud.yahoo.com>
References: <20060601230921.92601.qmail@web33305.mail.mud.yahoo.com>

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



view thread (13+ messages)  latest in thread

Message-ID: <20060602000051.GA12215@winnie.fuhr.org>
Permalink:  ../20060602000051.GA12215@winnie.fuhr.org/
Also on:    postgresql.org/message-id/20060602000051.GA12215@winnie.fuhr.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mike@fuhr.org, operationsengineer1@yahoo.com
  Subject: Re: Advanced Query
  In-Reply-To: <20060602000051.GA12215@winnie.fuhr.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox