Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

how to concatenate multiple columns in oracle sql into single column only for matching condition

i have below 4 columns

      empid | name  | dept     | ph_no
      ---------------------------------
      123   | null  |  null    | null
      124   | mike  |  science | null
      125   | null  |  physics | 789
      126   | null  |  null    | 463
      127   | john  |  null    | null

and i need to merge all 4 columns into single columns only for null values. And i need something like below--

    empid
 ------------
  123 is missing name,dept,ph_no
  124 is missing ph_no
  125 is missing name
  126 is missing name,dept
  127 is missing dept,ph_no
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This can be done with case expressions.

select empid,empid||' is missing '|| 
trim(',' from 
     (case when name is null then 'name,' else '' end||
      case when dept is null then 'dept,' else '' end||
      case when ph_no is null then 'ph_no' else '' end
     )
     ) 
from tbl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...