Re: Counting boolean values (how many true, how many false) - Mailing list pgsql-general

From Richard Broersma
Subject Re: Counting boolean values (how many true, how many false)
Date
Msg-id AANLkTin2HC0rvF1vCQk3BxNw0wWbPVAjYSEmMAF-nAqM@mail.gmail.com
Whole thread Raw
In response to Re: Counting boolean values (how many true, how many false)  (Alexander Farber <alexander.farber@gmail.com>)
List pgsql-general
On Tue, Nov 16, 2010 at 11:32 AM, Alexander Farber
<alexander.farber@gmail.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

pgsql-general by date:

Previous
From: Alexander Farber
Date:
Subject: Re: Counting boolean values (how many true, how many false)
Next
From: Peter Eisentraut
Date:
Subject: Re: ipv4 data type does not allow to use % assubnet mask delimiter