Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 0C12B475F2B for ; Mon, 20 May 2002 11:05:25 -0400 (EDT) Received: from beamish.nsd.ca (unknown [205.150.156.194]) by postgresql.org (Postfix) with ESMTP id DEC464759A4 for ; Mon, 20 May 2002 11:01:11 -0400 (EDT) Received: (from smap@localhost) by beamish.nsd.ca (8.9.3/8.9.3) id LAA03682; Mon, 20 May 2002 11:01:15 -0400 X-Authentication-Warning: beamish.nsd.ca: smap set sender to using -f Received: from reddog.nsd.ca(192.168.101.30) by beamish.nsd.ca via smap (V2.1/2.1+anti-relay+anti-spam) id xma003680; Mon, 20 May 02 11:01:12 -0400 Received: from nsd.ca (jllachan-linux.nsd.ca [192.168.101.148]) by reddog.nsd.ca (8.8.7/8.8.7) with ESMTP id LAA27555; Mon, 20 May 2002 11:01:12 -0400 Message-ID: <3CE9117F.183B58EE@nsd.ca> Date: Mon, 20 May 2002 11:08:47 -0400 From: Jean-Luc Lachance X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.4.9-31 i686) X-Accept-Language: en MIME-Version: 1.0 To: Dan MacNeil Cc: pgsql-sql Subject: Re: count(boolean) References: <001d01c1ff3f$d7d86470$b11a9318@prometheus> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Archive-Number: 200205/203 X-Sequence-Number: 7838 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