pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: gvim <gvimrc@gmail.com>
Cc: pgsql sql <pgsql-sql@postgresql.org>
Subject: Re: Find NOT NULLs in a group of 20 columns
Date: Mon, 10 Jan 2011 21:45:50 -0500
Message-ID: <5647.1294713950@sss.pgh.pa.us> (raw)
In-Reply-To: <4D2BBCF4.8070905@gmail.com>
References: <4D2BBCF4.8070905@gmail.com>

gvim <gvimrc@gmail.com> writes:
> I'm struggling to find anything other than a complex stored proc to do this:
> Out of a group of 20 columns return the rows in which:

> 1. Only 1 of the 20 columns IS NOT NULL
> 2. More than 1 of the 20 columns IS NOT NULL

> Any ideas?

Cast to int and add up?

SELECT ... WHERE
 ((col1 is not null)::int +
  (col2 is not null)::int + ...
  (col20 is not null)::int) = 1     -- or > 1

			regards, tom lane



view thread (2+ messages)

Message-ID: <5647.1294713950@sss.pgh.pa.us>
Permalink:  ../5647.1294713950@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/5647.1294713950@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, gvimrc@gmail.com
  Subject: Re: Find NOT NULLs in a group of 20 columns
  In-Reply-To: <5647.1294713950@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox