pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Use left hand column for null values
2+ messages / 2 participants
[nested] [flat]

* Use left hand column for null values
@ 2018-01-19 15:46 kpi6288@gmail.com
  2018-01-19 15:59 ` Re: Use left hand column for null values David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: kpi6288@gmail.com @ 2018-01-19 15:46 UTC (permalink / raw)
  To: pgsql-general

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

 

 

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Use left hand column for null values
  2018-01-19 15:46 Use left hand column for null values kpi6288@gmail.com
@ 2018-01-19 15:59 ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: David G. Johnston @ 2018-01-19 15:59 UTC (permalink / raw)
  To: kpi6288@gmail.com; +Cc: pgsql-general

On Fri, Jan 19, 2018 at 8:46 AM, <kpi6288@gmail.com> wrote:

> 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
>
>
>
​My $0.02​

​Unless you have some need to generalize I'd say ​just brute-force it and
be done.

You could maybe play with arrays to get something that looks different but
I don't think it would be much shorter to code or easier to understand.

It is structured enough that you could probably build the query as a string
and then "EXECUTE" it but you'd probably spend more time working that out
than just coding it once.

If you don't have to return 50 columns but could return text (or an array)
it might be worth some R&D to find something similar.

One trick I use when dealing with structured but tedious queries is to
write them in a spreadsheet.  I between auto-fill and formulas I can
usually speed up the input of the structured data compared to typing it out
line-by-line manually.

David J.

^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2018-01-19 15:59 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-01-19 15:46 Use left hand column for null values kpi6288@gmail.com
2018-01-19 15:59 ` David G. Johnston <david.g.johnston@gmail.com>

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