Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Let's say I've got 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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
639 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...