Search Postgresql Archives

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Nov 16, 2010 at 11:32 AM, Alexander Farber
<alexander.farber@xxxxxxxxx> 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

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux