pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jean-Luc Lachance <jllachan@nsd.ca>
To: Dan MacNeil <dan@ltc.org>
Cc: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Re: count(boolean)
Date: Mon, 20 May 2002 11:08:47 -0400
Message-ID: <3CE9117F.183B58EE@nsd.ca> (raw)
References: <JGEPJNMCKODMDHGOBKDNAELLCOAA.joel@joelburton.com>
	<001d01c1ff3f$d7d86470$b11a9318@prometheus>

Dan,

Try:

SELECT
  question_id AS id, 
  count( case when value then 1 else 0 end) as count_true,
  count( case when value then 0 else 1 end) as count_false
FROM
  answers_boolean
GROUP BY
  question_id;



jll

Dan MacNeil wrote:
> 
> I have a table answers_boolean:
> 
>  question_id   | integer   | not null
>  evaluation_id | integer   | not null
>  value             | boolean |
> 
> I'd like output in the form:
> 
> question_id,  count_true, count_false
> 
> ....where count_true is the number of questions answered "true"
> 
> SELECT
>     question_id AS id, value AS val , count(value) AS cnt
> FROM
>      answers_boolean
> GROUP BY
>      question_id,value;
> 
> gives me:
>  id | val | cnt
> ----+-----+-----
>   2 | f   |   3
>   2 | t   |   3
>   3 | f   |   2
>   3 | t   |   4
> 
> ...which is sorta what I want.
> 
> I've looked through the docs & archive but haven't found the answer.
> 
> TIA
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly



view thread (4+ messages)  latest in thread

Message-ID: <3CE9117F.183B58EE@nsd.ca>
Permalink:  ../3CE9117F.183B58EE@nsd.ca/
Also on:    postgresql.org/message-id/3CE9117F.183B58EE@nsd.ca

 · 

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: jllachan@nsd.ca, dan@ltc.org
  Subject: Re: count(boolean)
  In-Reply-To: <3CE9117F.183B58EE@nsd.ca>

* 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