开发者

Merging two rows to one while replacing null values

开发者 https://www.devze.com 2023-01-08 02:15 出处:网络
Let\'s say I\'ve got开发者_Go百科 the following database table Name | Nickname | ID ----------------------

Let's say I've got开发者_Go百科 the following database table

Name | Nickname | ID
----------------------
Joe    Joey       14
Joe    null       14

Now I want to do a select statement that merges these two columns to one while replacing the null values. The result should look like this:

Joe, Joey, 14

Which sql statement manages this (if it's even possible)?


Simplest solution:

SQL> select * from t69
  2  /

NAME       NICKNAME           ID
---------- ---------- ----------
Joe        Joey               14
Joe                           14
Michael                       15
           Mick               15
           Mickey             15

SQL> select max(name) as name
  2         , max(nickname) as nickname
  3         , id
  4  from t69
  5  group by id
  6  /

NAME       NICKNAME           ID
---------- ---------- ----------
Joe        Joey               14
Michael    Mickey             15

SQL>

If you have 11gR2 you could use the new-fangled LISTAGG() function but otherwise it is simple enough to wrap the above statement in a SELECT which concatenates the NAME and NICKNAME columns.


AFAIK, the question is not clear.so i am making some assumptions over here. your output has the first and 3rd columns for both the rows as same. Only the 2nd field is different.

so u can simply write a select quest

select one.name,two.nick_name,one.id from 
(select name,id from your_tb group by name,id) one,
your_tb two 
where two.nickname is not NULL 
and two.name=one.name 
and two.id=one.id;

may be we can tune this but i am not good in tuning sql squeries,but this is the way i suppose u need.

0

精彩评论

暂无评论...
验证码 换一张
取 消