2017-07-12 47 views
-1

我在下面的查詢中根據特定條件解析列中的名稱。我想擴充此查詢以包含adwords_final表「Key」中實際列的附加列。在執行現有查詢的同時插入列

insert into mk_kw 
with a(v) as (
      select "Campaign" 
      from adwords_final 
      ), 
pre as (
     select split_part(v, '{', 1) a,split_part(v, '{', 2) b 
     from a 
     ) 
select split_part(a, '-', 1) ca, 
     split_part(a, '-', 2) cb, 
     split_part(split_part(a, '-', 3), '|', 1) cc, 
     split_part(split_part(a, '-', 3), 
     split_part(split_part(a, '-', 3), '|', 1) || '|', 2) cd, 
     '{' || b ce 
from pre; 

電流輸入表如下所示,「運動」是在行動當前列

Campaign       |Key | 
-----------------------------------+--------+ 
ES-SRC-IOT|Connectivity|SIM|USA|{e}|ES-SRC-IOT|Connectivity|SIM|USA|{e}2017-5 

這樣做的電流輸出如下所示

ca | cb | cc  |    cd    | ce 
----+-----+-----------+----------------------------+------ 
ES | SRC | IOT  | Connectivity | SIM | USA | {e} 

我會喜歡在此輸出中添加「Key」列作爲第一列。

是否有可能添加這個或我是否必須完全取消該查詢?

乾杯。

回答

0
TRUNCATE mk_kw; 
INSERT INTO mk_kw 
with a(v,g) as (
select "Campaign","Key" 
from adwords_final 
), pre as (
select split_part(v, '{', 1) a,split_part(v, '{', 2) b,g||'' "Key" 
from a 
) 
select 
"Key", 
split_part(a, '-', 1) ca, 
split_part(a, '-', 2) cb, 
split_part(split_part(a, '-', 3), '|', 1) cc, 
split_part(split_part(a, '-', 3), 
split_part(split_part(a, '-', 3), '|', 1) || '|', 2) cd, 
'{' || b ce 

from pre;