From: | Jean-Luc Lachance <jllachan(at)nsd(dot)ca> |
---|---|
To: | Dan MacNeil <dan(at)ltc(dot)org>, pgsql-sql <pgsql-sql(at)postgresql(dot)org> |
Subject: | Re: count(boolean) |
Date: | 2002-05-20 16:07:37 |
Message-ID: | 3CE91F49.DC7B73C8@nsd.ca |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Actually, "count" should be "sum"
SELECT
question_id AS id,
sum( case when value then 1 else 0 end) as count_true,
sum( case when value then 0 else 1 end) as count_false
FROM
answers_boolean
GROUP BY
question_id;
I wrote:
>
> 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(at)postgresql(dot)org so that your
> > message can get through to the mailing list cleanly
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)
From | Date | Subject | |
---|---|---|---|
Next Message | Eckhard Hoeffner | 2002-05-20 16:10:36 | Re: Using LASTOID in one query |
Previous Message | Josh Berkus | 2002-05-20 16:03:42 | Re: A proposal for Interval Math |