Re: Counting boolean values (how many true, how many false)

From: Richard Broersma <richard(dot)broersma(at)gmail(dot)com>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Counting boolean values (how many true, how many false)
Date: 2010-11-16 19:49:01
Message-ID: AANLkTin2HC0rvF1vCQk3BxNw0wWbPVAjYSEmMAF-nAqM@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Nov 16, 2010 at 11:32 AM, Alexander Farber
<alexander(dot)farber(at)gmail(dot)com> wrote:
> sum(case when good then 1 else 0 end) as good,
> sum(case when not good then 1 else 0 end) as "not good",
> sum(case when fair then 1 else 0 end) as fair,
> sum(case when not fair then 1 else 0 end) as "not fair",
> sum(case when nice then 1 else 0 end) as nice,
> sum(case when not nice then 1 else 0 end) as "not nice"
> from public.pref_rep;

Here is one slightly more compact.
# select
COUNT(NULLIF( good, FALSE)) as good,
COUNT(NULLIF( good, TRUE)) as "not good",
COUNT(NULLIF( fair, FALSE)) as fair,
COUNT(NULLIF( fair, TRUE)) as "not fair",
COUNT(NULLIF( nice, FALSE)) as nice,
COUNT(NULLIF( nice, TRUE)) as "not nice",
from public.pref_rep;

--
Regards,
Richard Broersma Jr.

Visit the Los Angeles PostgreSQL Users Group (LAPUG)
http://pugs.postgresql.org/lapug

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Hu, William 2010-11-16 20:09:31 How to select a postgresql table inside Oracle
Previous Message Alexander Farber 2010-11-16 19:32:33 Re: Counting boolean values (how many true, how many false)