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'.

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

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

              http://www.postgresql.org/docs/faq

[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