首页 > 解决方案 > 用逗号分隔列,忽略空值

问题描述

我有下表:

一种 C D
A1 空值 C1 空值 E1
A2 B2 C2 空值 空值
空值 空值 C3 空值 E3

我想要以下输出(用逗号分隔,如果任何值为空,则不要添加逗号):

F
A1、C1、E1
A2、B2、C2
C3、E3

标签: sqloracle

解决方案


基本上,你想要concat_ws()——Oracle 不支持。相反,您可以使用:

select trim(',' from
             (case when A is not null then ',' || A end ||
              case when B is not null then ',' || B end ||
              case when C is not null then ',' || C end ||
              case when D is not null then ',' || D end ||
              case when E is not null then ',' || E end
             )
           )
              

推荐阅读