pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: kpi6288@gmail.com
To: pgsql-general@postgresql.org
Subject: Use left hand column for null values
Date: Fri, 19 Jan 2018 16:46:21 +0100
Message-ID: <006801d3913c$a7b7c420$f7274c60$@gmail.com> (raw)

I'm trying to fill up columns containing NULL with the most recent NOT NULL
value from left to right. 

Example: 

Select 2, 1, null, null, 3 

Should be converted into 

2, 1, 1, 1, 3

 

The following query works but I wonder if there is an easier way for tables
with 50 or more columns: 

 

with a (c1, c2, c3, c4, c5) as (

values(2, 1, null::int, null::int, 3)

)

select 

c1,

coalesce (c2, c1) as c2, 

coalesce (c3, c2, c1) as c3,

coalesce (c4, c3, c2, c1) as c4,

coalesce (c5, c4, c3, c2, c1) as c5

from a

 

Thanks

Klaus

 

 

view thread (2+ messages)  latest in thread

Message-ID: <006801d3913c$a7b7c420$f7274c60$@gmail.com>
Permalink:  ../006801d3913c$a7b7c420$f7274c60$@gmail.com/
Also on:    postgresql.org/message-id/006801d3913c$a7b7c420$f7274c60$@gmail.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: kpi6288@gmail.com
  Subject: Re: Use left hand column for null values
  In-Reply-To: <006801d3913c$a7b7c420$f7274c60$@gmail.com>
  References: <006801d3913c$a7b7c420$f7274c60$@gmail.com>

* 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