Re: Count

From: brian <brian(at)zijn-digital(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Count
Date: 2008-01-23 19:59:22
Message-ID: 47979C9A.6040106@zijn-digital.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

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

Browse pgsql-general by date

  From Date Subject
Next Message Charles Simard 2008-01-23 20:08:09 Re: Count
Previous Message Marko Kreen 2008-01-23 19:59:09 Re: Postgresql + digital signature