sql(db2)

时间:2017-10-12 16:10:40

标签: sql db2 carriage-return coalesce nullif

我正在尝试使用coalesce和ifnull将多个数据点合并到一个单元格中,并在组合数据点之间使用回车符。如果我删除第二个coalesce语句它可以工作,但到目前为止没有运气结合两组以上的数据。任何想法将不胜感激。我在DB2中使用SQL。

基本上我想从SQL语句得到以下结果:

鲍勃,LCE

弗雷德,LBD

Julie,LNJ

select 
nullif(b.column03,'') || ', ' || nullif(b.column04,'')
|| coalesce(chr(10) || nullif(b.column05,'') || ', ' || 
nullif(b.column06,''),'')
|| coalesce(chr(10) || nullif(b.column07,'') || ', ' || 
nullif(b.column08,''),'')
as "Presenter/Agency"
from  [ETL redacted] b
order by 1,2,3,4

1 个答案:

答案 0 :(得分:0)

case when coalesce(column3,column04) > '' then
   column03  || ', ' || b.column04 
else '' end ||
case when coalesce(column05,column06) > '' then 
   case when coalesce(column03,column04) > '' then chr(10) else '' end ||
   column05  || ', ' || b.column06 
else '' end ||
case when coalesce(column07,column08) > '' then 
   case when coalesce(column03,column04,column05,column06) > '' then chr(10) else '' end ||
   column07  || ', ' || b.column08 
else '' end