Re: Count

Поиск
Список
Период
Сортировка
От Bricklen Anderson
Тема Re: Count
Дата
Msg-id 479793FD.2000103@presinet.com
обсуждение исходный текст
Ответ на Count  (Bob Pawley <rjpawley@shaw.ca>)
Список pgsql-general
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

?

В списке pgsql-general по дате отправления:

Предыдущее
От: Rick Schumeyer
Дата:
Сообщение: Re: Tips for upgrading from 7.4
Следующее
От: pepone.onrez
Дата:
Сообщение: Best practices for protect applications agains Sql injection.