diff --git a/dev/workflow/TK_Cust/smart_ccc_custom/客户标签关系表/cust_label_rela.sql b/dev/workflow/TK_Cust/smart_ccc_custom/客户标签关系表/cust_label_rela.sql index ac7ce1e..a9c6e09 100644 --- a/dev/workflow/TK_Cust/smart_ccc_custom/客户标签关系表/cust_label_rela.sql +++ b/dev/workflow/TK_Cust/smart_ccc_custom/客户标签关系表/cust_label_rela.sql @@ -12,7 +12,7 @@ CREATE TABLE IF NOT EXISTS p30_common.cust_label_rela ( , Etl_Tx_Dt timestamp(0) , Src_Sysname varchar(50) , Src_Table varchar(50) - ,primary key( cust_id,cust_label_id ) + ,primary key( cust_id,cust_label_id,cust_external_id ) ); COMMENT ON COLUMN p30_common.cust_label_rela.cust_id IS '客户编号'; diff --git a/dev/workflow/TK_Cust/smart_ccc_custom/客户标签关系表/cust_label_rela_agi.sql b/dev/workflow/TK_Cust/smart_ccc_custom/客户标签关系表/cust_label_rela_agi.sql index 2e74722..7710913 100644 --- a/dev/workflow/TK_Cust/smart_ccc_custom/客户标签关系表/cust_label_rela_agi.sql +++ b/dev/workflow/TK_Cust/smart_ccc_custom/客户标签关系表/cust_label_rela_agi.sql @@ -4,7 +4,7 @@ /*Brilliance stems from wisdoms. */ /*************Head Section**************************************************************************/ /*Script Use: Periodically load data to :cust_label_rela(客户标签关系表) */ -/*Create Date:2024-08-23 14:38:21 */ +/*Create Date:2024-08-23 14:56:54 */ /*SDM Developed By: dev */ /*SDM Developed Date: 2024-07-11 */ /*SDM Checked By: dev */ @@ -13,6 +13,8 @@ /*Script Checked By: dev */ /*Source table 1: :PDMDB.t01_ccc_cust_info */ /*Source table 2: :PDMDB.t01_ccc_cust_label */ +/*Source table 3: :PDMDB.(select id, trim(unnest(string_to_array(replace(external_id,';',','),','))) external_id from p20_pdm.t01_ccc_cust_info tcci +where length(trim(label_ids))>0)*/ /*Job Type: Inbound transform (Tier 1 to Tier 2) */ /*Target Table:cust_label_rela */ /*ETL Job Name:cust_label_rela */ @@ -60,7 +62,7 @@ SELECT coalesce(p0.id,'') /*cust_id*/ ,coalesce(p0.label_id,'') /*cust_label_id*/ ,coalesce(p1.label_name,'') /*cust_label_name*/ - ,coalesce(p0.external_id,'') /*cust_external_id*/ + ,coalesce(p2.external_id,'') /*cust_external_id*/ ,0 /*Etl_Batch_No*/ ,TO_DATE(:TXDATE,'YYYYMMDD') /*Etl_First_Dt*/ ,:ETLJOB /*Etl_Job*/ @@ -69,11 +71,13 @@ SELECT ,Substr('t01_ccc_cust_info',1,3) /*Src_Sysname*/ ,'t01_ccc_cust_info' /*Src_Table*/ -FROM (select * from (select id,trim(unnest(string_to_array(replace(replace(label_ids,'[',''),']',''),','))) label_id, trim(unnest(string_to_array(replace(external_id,';',','),''))) external_id from p20_pdm.t01_ccc_cust_info tcci -where length(trim(external_id))>0)p -where label_id is not null and external_id is not null) p0 +FROM (select id,trim(unnest(string_to_array(replace(replace(label_ids,'[',''),']',''),','))) label_id from p20_pdm.t01_ccc_cust_info tcci +where length(trim(external_id))>0) p0 LEFT JOIN :PDMDB.t01_ccc_cust_label p1 ON p0.label_id=p1.label_id +LEFT JOIN ((select id, trim(unnest(string_to_array(replace(external_id,';',','),','))) external_id from p20_pdm.t01_ccc_cust_info tcci +where length(trim(label_ids))>0)) p2 + ON p2.id=p0.id ; @@ -83,9 +87,9 @@ LEFT JOIN :PDMDB.t01_ccc_cust_label p1 /*将不同数据插入到临时表 */ ;INSERT INTO cust_label_rela_agi_INS ( cust_label_name /*客户标签名称*/ - ,cust_external_id /*客户外部企业ID*/ ,cust_id /*客户编号*/ ,cust_label_id /*客户标签编号*/ + ,cust_external_id /*客户外部企业ID*/ ,Etl_Batch_No /*作业批次号*/ ,Etl_First_Dt /*最初入库时间*/ ,Etl_Job /*作业名称*/ @@ -97,9 +101,9 @@ LEFT JOIN :PDMDB.t01_ccc_cust_label p1 ) SELECT P1.cust_label_name /*客户标签名称*/ - ,P1.cust_external_id /*客户外部企业ID*/ ,P1.cust_id /*客户编号*/ ,P1.cust_label_id /*客户标签编号*/ + ,P1.cust_external_id /*客户外部企业ID*/ ,P1.Etl_Batch_No /*作业批次号*/ ,P1.Etl_First_Dt /*最初入库时间*/ ,P1.Etl_Job /*作业名称*/ @@ -111,22 +115,22 @@ LEFT JOIN :PDMDB.t01_ccc_cust_label p1 FROM cust_label_rela_agi_CUR_I P1 LEFT JOIN :COMMDB.cust_label_rela P2 ON P1.cust_label_name = P2.cust_label_name - AND P1.cust_external_id = P2.cust_external_id AND P1.cust_id = P2.cust_id AND P1.cust_label_id = P2.cust_label_id + AND P1.cust_external_id = P2.cust_external_id WHERE P2.cust_label_name IS NULL - OR P2.cust_external_id IS NULL OR P2.cust_id IS NULL OR P2.cust_label_id IS NULL + OR P2.cust_external_id IS NULL ; /*将新增数据插入到目标表 */ ;INSERT INTO :COMMDB.cust_label_rela ( cust_label_name /*客户标签名称*/ - ,cust_external_id /*客户外部企业ID*/ ,cust_id /*客户编号*/ ,cust_label_id /*客户标签编号*/ + ,cust_external_id /*客户外部企业ID*/ ,Etl_Batch_No /*作业批次号*/ ,Etl_First_Dt /*最初入库时间*/ ,Etl_Job /*作业名称*/ @@ -138,9 +142,9 @@ WHERE P2.cust_label_name IS NULL ) SELECT P1.cust_label_name /*客户标签名称*/ - ,P1.cust_external_id /*客户外部企业ID*/ ,P1.cust_id /*客户编号*/ ,P1.cust_label_id /*客户标签编号*/ + ,P1.cust_external_id /*客户外部企业ID*/ ,P1.Etl_Batch_No /*作业批次号*/ ,P1.Etl_First_Dt /*最初入库时间*/ ,P1.Etl_Job /*作业名称*/ @@ -150,12 +154,12 @@ SELECT ,P1.Src_Table /*来源表*/ FROM cust_label_rela_agi_INS P1 -ON CONFLICT ( cust_id,cust_label_id) +ON CONFLICT ( cust_id,cust_label_id,cust_external_id) DO UPDATE SET cust_id=excluded.cust_id ,cust_label_id=excluded.cust_label_id - ,cust_label_name=excluded.cust_label_name ,cust_external_id=excluded.cust_external_id + ,cust_label_name=excluded.cust_label_name ,Etl_Batch_No=excluded.Etl_Batch_No ,Etl_First_Dt=excluded.Etl_First_Dt ,Etl_Job=excluded.Etl_Job