2017-08-16 64 views
1

我想串联一个列的值,但这些值必须格式化为另一个字符串。连接列值

这里是我的表:

C1   C2      c3     c4 
--------- ---------    ------    -------- 
ID1   28-OCT-16 11.59.00  (null)    04-OCT-16 08.48.00 
ID2   (null)     05-OCT-16 02.55.00 (null) 
ID3   (null)     10-OCT-16 04.32.00 21-OCT-16 02.25.00 
ID4   10-OCT-16 04.32.00  18-OCT-16 08.52.00 18-OCT-16 08.32.00 
ID5   10-OCT-16 04.32.00  (null)    (null) 

我已经完成格式化表格来匹配我需要的价值。

select 
    c1 T_ID, 
    case when c2 is not null then 'Plane' end PLANE, 
    case when c3 is not null then 'BUS' end BUS, 
    case when c4 is not null then 'Hotel' end HOTEL 
    from table1 
order by 1; 




T_ID  PLANE     BUS     HOTEL 
--------- ---------    ------    -------- 
ID1   Plane     (null)    Hotel 
ID2   (null)     BUS     (null) 
ID3   (null)     BUS     Hotel 
ID4   Plane     BUS     Hotel 
ID5   Plane     (null)    (null) 

而且我尝试做以下

T_ID  SERVICE   
--------- ---------  
ID1   Plane+Hotel   
ID2   BUS   
ID3   BUS+Hotel   
ID4   Plane+BUS+Hotel   
ID5   Plane 

我试过一对夫妇级联功能,但无法找到我要找的结果。

+0

“我试过几个串接函数” - 你有什么试过?什么不行? –

+0

向我们展示你在找什么结果请 –

回答

1

基本上可以做到:

select c1 T_ID, 
     substr((case when c2 is not null then '+Plane' end) || 
       (case when c3 is not null then '+BUS' end) || 
       (case when c4 is not null then '+Hotel' end) 
       2) 
from table1 
order by 1; 

这基本上通过将分离器在字符串中的每个组件的开始实现功能concat_ws()。外部substr()删除第一个字符。

+0

是的,我喜欢那个....让我试试看.. – Zombraz