Re: merge two rows where value are null

From: Ben Kim <bkim(at)tamu(dot)edu>
To: giuseppe(dot)derossi(at)email(dot)it
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: merge two rows where value are null
Date: 2007-08-14 13:34:02
Message-ID: Pine.GSO.4.64.0708140820250.8353@coe.tamu.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Tue, 14 Aug 2007, giuseppe(dot)derossi(at)email(dot)it wrote:

> Name, Surname , Job, Hobby, Pet, address
> John, Smith, NULL, photo, NULL, NULL
> John, Smith, student, NULL, cat, NULL

> by using name and surname as selecting key, I want :
>
> John, Smith, student, photo, cat, NULL

If you are sure there's no conflict (multiple values) for a (name,
surname) key, one simple solution might be (surely not an efficient one)

select
(select distinct Name from thetable where Name = 'name1' and Surname = 'surname1' where Name is not null),
(select distinct Surname from thetable where Name = 'name1' and Surname = 'surname1' where Surname is not null),
(select distinct Job from thetable where Name = 'name1' and Surname = 'surname1' where Job is not null),
(select distinct Hobby from thetable where Name = 'name1' and Surname = 'surname1' where Hobby is not null),
(select distinct Pet from thetable where Name = 'name1' and Surname = 'surname1' where Pet is not null),
(select distinct address from thetable where Name = 'name1' and Surname = 'surname1' where address is not null)

This will fail if you also have
John, Smith, student, NULL, dog, NULL

in addition to
> John, Smith, student, NULL, cat, NULL

You can test for offending rows by:

select
array_to_string( array( select distinct Pet from thetable where Name = 'name1' and Surname = 'surname1' where Pet is not null),',')

and so on...

Regards,

Ben K.
Developer
http://benix.tamu.edu

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Lew 2007-08-14 13:41:37 Re: Downloading PostgreSQL source code version 7.1 through CVS
Previous Message Hyatt, Gordon 2007-08-14 13:18:28 Re: plpgsql question/problem