Unix Technical Forum

merge two rows where value are null

This is a discussion on merge two rows where value are null within the pgsql Admins forums, part of the PostgreSQL category; --> Hi all, sorry if OT. I have the following using case: Name, Surname , Job, Hobby, Pet, address John, ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Admins

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 08:05 AM
giuseppe.derossi@email.it
 
Posts: n/a
Default merge two rows where value are null

Hi all,
sorry if OT. I have the following using case:

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

that is I want to substitute the null valus of the LAST row with not-null
value of previous one.

how can I perform that ? in may installed vesion there aren't function as
merge and collect.

Thanks in advance

GIU

--
Email.it, the professional e-mail, gratis per te: http://www.email.it/f

Sponsor:
Entra in Mondolastminute, centinaia di offerte ti aspettano per le tue
vacanze a prezzi lastminute!
Clicca qui: http://adv.email.it/cgi-bin/foclick....851&d=20070814



---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 08:06 AM
Ben Kim
 
Posts: n/a
Default Re: merge two rows where value are null

On Tue, 14 Aug 2007, giuseppe.derossi@email.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

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:58 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com