Re: Filling null values - Mailing list pgsql-general

From Ben Carbery
Subject Re: Filling null values
Date
Msg-id CACp6DjB3GyyV_heOALBxBoAZ-674TR30kO8Ap_UQOjTJkt+nnw@mail.gmail.com
Whole thread Raw
In response to Re: Filling null values  (Sim Zacks <sim@compulab.co.il>)
List pgsql-general
Hmm, no-one seemed to mention the obvious - a pl/pgsql function, either triggered or run manually depending if you want to update on insert/update or on demand.


On 7 August 2011 16:05, Sim Zacks <sim@compulab.co.il> wrote:
On 08/05/2011 07:32 PM, jeffrey wrote:
I have a table that looks like this:

homeid    city          date     measurement      pre/post
123   san francisco  1/2/2003     1458             pre
123   san francisco  NULL          1932             post
124   los angeles    2/4/2005      938               pre
124   NULL            NULL           266               pre
124   los angeles    7/4/2006      777               post

I'd like to write a query so that I get the following result:

homeid    city          date     measurement      pre/post
123   san francisco  1/2/2003     1458             pre
123   san francisco  1/2/2003      1932            post
124   los angeles    2/4/2005      938               pre
124   los angeles    2/4/2005       266              pre
124   los angeles    7/4/2006      777               post

If a city or date is null, then it will fill from other not null
values with the same homeid.  If given the choice, it will
preferentially fill from a row where homeid AND pre/post match.  But
if that doesn't match, then it will still fill from the same homeid.

Does anyone have ideas for this?

Thanks,
Jeff

pgsql-general by date:

Previous
From: "Tomas Vondra"
Date:
Subject: Re: postgres table have a large number of relpages and occupied a big memory size
Next
From: Amitabh Kant
Date:
Subject: Re: Backup & Restore a database in PostgreSQL