Re: Count

Поиск
Список
Период
Сортировка
От brian
Тема Re: Count
Дата
Msg-id 47979C9A.6040106@zijn-digital.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'.

Any reason why the NULL values aren't instead FALSE? NULL != FALSE.

> I want to obtain the count of these columns, within a particular row,
> that have 'true' as a value (0 to 4).

SELECT
     (CASE WHEN t.col1 = TRUE THEN 1 ELSE 0 END)
   + (CASE WHEN t.col2 = TRUE THEN 1 ELSE 0 END)
   + (CASE WHEN t.col3 = TRUE THEN 1 ELSE 0 END)
   + (CASE WHEN t.col4 = TRUE THEN 1 ELSE 0 END)
AS true_count FROM your_table AS t;

I'm sure there's a more clever way than that, though.

> I have attempted the Select count method but it seems that I need
> something more.

That would only be useful if you were counting across rows (aggregating).

b

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

Предыдущее
От: "Marko Kreen"
Дата:
Сообщение: Re: Postgresql + digital signature
Следующее
От: Charles Simard
Дата:
Сообщение: Re: Count