Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: Count


  • From: brian <brian(at)zijn-digital(dot)com>
  • To: pgsql-general(at)postgresql(dot)org
  • Subject: Re: Count
  • Date: Wed, 23 Jan 2008 14:59:22 -0500
  • Message-id: <47979C9A(dot)6040106(at)zijn-digital(dot)com>

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


  • References:

Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group