Re: Count

From: Charles Simard <tech(at)denarius(dot)ca>
To: 'Bob Pawley' <rjpawley(at)shaw(dot)ca>, 'PostgreSQL' <pgsql-general(at)postgresql(dot)org>
Subject: Re: Count
Date: 2008-01-23 20:08:09
Message-ID: E6AE5AD88D55B543A0A97FF0B93AA9E93C3307@TRADE200.DENARIUS.CA
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> -----Original Message-----
> From: pgsql-general-owner(at)postgresql(dot)org
> [mailto:pgsql-general-owner(at)postgresql(dot)org]On Behalf Of Bob Pawley
> Sent: 23 janvier 2008 13:51
> To: PostgreSQL
> Subject: [GENERAL] Count
>
>
> I have a table with four columns that will either be null or hold the
value
> 'true'.
>
> I want to obtain the count of these columns, within a particular row, that
> have 'true' as a value (0 to 4).
>
> I have attempted the Select count method but it seems that I need
something
> more.
>
> If anyone has any thoughts it would be much appreciated.
>
> Bob
>

Or something like this ?

create table test (
id_test serial,
c1 boolean,
c2 boolean,
c3 boolean,
c4 boolean
);

insert into test (c1,c2,c3,c4) values ( true, null, null, true),( true,
true, null, true),( null, null, null, null);

select id_test, (case when c1 is null then 0 else 1 end)+(case when c2 is
null then 0 else 1 end)+(case when c3 is null then 0 else 1 end)+(case when
c4 is null then 0 else 1 end) as total from test;

id_test | total
---------+-------
1 | 2
2 | 3
3 | 0

Regards,

Charles Simard

In response to

  • Count at 2008-01-23 18:50:48 from Bob Pawley

Browse pgsql-general by date

  From Date Subject
Next Message Roberto Scattini 2008-01-23 20:18:42 Re: pg_xlog and standby
Previous Message brian 2008-01-23 19:59:22 Re: Count