Search Postgresql Archives

Re: Count

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

 



Bob Pawley wrote:
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

Something like this?

create table t (id int, w bool, x bool, y bool, z bool);
insert into t values
(1,null,null,'t','t'),
(1,null,'t','t',null),
(2,'t',null,'t',null),
(2,'t',null,'t',null),
(3,null,'t','t','t'),
(4,'t','t','t','t');

select id,
       sum(case when w is null then 0 else 1 end) as w,
       sum(case when x is null then 0 else 1 end) as x,
       sum(case when y is null then 0 else 1 end) as y,
       sum(case when z is null then 0 else 1 end) as z
from t
group by id
order by id;

 id | w | x | y | z
----+---+---+---+---
  1 | 0 | 1 | 2 | 1
  2 | 2 | 0 | 2 | 0
  3 | 0 | 1 | 1 | 1
  4 | 1 | 1 | 1 | 1

?

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

[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