-- Databricks notebook source
---------------------------------------------在配置表中提取所需字段去重，并对编码类数据补位、公司名称简化、产品重新归属公司------------------------------------
insert overwrite table tmp.tmp_ext_xiehe_pack_property_tmp 
----code 补码
---重新归属公司
select   
     distinct 
	 --20250428 chenwu iqivacode扩到12位,brandcode 扩到9位
	 if( IQVIA_PACK_CODE REGEXP '^[0-9]',right(concat('000000000000',IQVIA_PACK_CODE),12),IQVIA_PACK_CODE) as IQVIA_PACK_CODE --right(concat('0000000', IQVIA_PACK_CODE),7)    
	,right(concat('000000000', IQVIA_PROD_CODE),9) as IQVIA_PROD_CODE --right(concat('00000', IQVIA_PROD_CODE),5)
	,a.pack_des      
	,a.APP1_COD
	,a.APP1_DES
	,a.APP1_DES_C
	,a.APP2_COD
	,a.APP2_DES
	,a.APP2_DES_C
	,a.APP3_COD
	,a.APP3_DES   
	,a.APP3_DES_C   
	,a.ATC1_COD   
	,a.ATC1_DES
	,a.ATC1_DES_C   
	,a.ATC2_COD  
	,a.ATC2_DES   
	,a.ATC2_DES_C
	,a.ATC3_COD
	,a.ATC3_DES
	,a.ATC3_DES_C
	,a.ATC4_COD
	,a.ATC4_DES
	,a.ATC4_DES_C
	,a.BIO_DESC
	,right(concat('000000', a.CMPS_COD),6)                                   AS CMPS_COD
	,a.CMPS_DES
	,a.CMPS_DES_C
  ,COALESCE(c.corp_cod,b.corp_cod,a.corp_cod)                                                     as corp_cod
	,COALESCE(c.corp_des,b.corp_des,case when a.CORP_DES like '% GROUP%' then replace(a.CORP_DES,' GROUP','') else a.CORP_DES end)  as CORP_DES
  ,COALESCE(d.corp_des_c,a.corp_des_c )                                                           as corp_des_c
	,a.EDL_DESC
	,a.ETH_OTC_DESC
	,a.GENE_ORIG_DESC
	,a.GQCE_DESC
	,COALESCE(c.manu_cod,b.manu_cod,a.manu_cod)  manu_cod 
  ,COALESCE(c.manu_des,b.manu_des,a.manu_des)  manu_des 
  ,COALESCE(e.manu_des_c,a.manu_des_c)         manu_des_c
	,case when a.MNFL_COD = 'L' then 'L' else 'M' end MNFL_COD           --chpa逻辑
 	,case when a.MNFL_DES='Local' then 'LOCAL' else 'MNC' end MNFL_DES   --chpa逻辑
	-- ,a.MNFL_COD
	-- ,a.MNFL_DES
	,a.NRDL_DESC
	,a.STGH_DES
	,a.PACK_LCH
	,a.PAED_DESC
	,a.PROD_DES
	,a.PROD_DES_C  
	,a.TCM_DESC
	,CASE WHEN a.VBP_DESC IN ('VBP-IN','VBP-N/A') THEN 'VBP' ELSE 'Non VBP' END VBP_DESC    --chpa逻辑
	-- ,a.VBP_DESC
	,a.Unit
	,a.Counting_Unit
	,a.Dosage_Unit
from dwd.dwd_gnd_xiehe_pack_info a 
left join (select right(concat('000000000',prod_cod ),9) prod_cod,corp_cod
                 ,case when CORP_DES like '% GROUP%' then replace(CORP_DES,' GROUP','') else CORP_DES end corp_des,right(concat('000000',manu_cod ),6) manu_cod,manu_des 
           from dwd.dwd_gnd_tbl_corp_change 
           where pack_cod is  null  
) b  on right(concat('000000000', IQVIA_PROD_CODE),9)= b.prod_cod 
--right(concat('00000',A.IQVIA_PROD_CODE ),5)= b.prod_cod
left join (select if( pack_cod REGEXP '^[0-9]',right(concat('000000000000',pack_cod),12),pack_cod) pack_cod,corp_cod
				   ,case when CORP_DES like '% GROUP%' then replace(CORP_DES,' GROUP','') else CORP_DES end corp_des,right(concat('000000',manu_cod ),6) manu_cod,manu_des 
           from dwd.dwd_gnd_tbl_corp_change 
           where pack_cod is  not null  
) c  on if( IQVIA_PACK_CODE REGEXP '^[0-9]',right(concat('000000000000',IQVIA_PACK_CODE),12),IQVIA_PACK_CODE) = c.pack_cod
--right(concat('0000000', IQVIA_PACK_CODE),7)  = c.pack_cod
left join (select distinct corp_cod,corp_des_c  from dwd.dwd_gnd_xiehe_pack_info) d  on coalesce(C.corp_cod,B.corp_cod ) = d.corp_cod 
Left join (select distinct right(concat('000000',manu_cod ),6) manu_cod,manu_des,manu_des_c  from dwd.dwd_gnd_xiehe_pack_info) e  On coalesce(C.manu_cod,B.manu_cod )  = e.manu_cod 

;

--------关联tblmarket 数据
-----MARKET
--第一步：先处理 Extend_Market 和 NOT_IN_FLAG 都为空的数据，代表有明确定义得市场
insert overwrite table tmp.tmp_ext_xiehe_pack_property 
select  distinct
 t2.market,t1.*,
t2.bu,CASE WHEN t2.extend_market_ratio IS NULL THEN '1' ELSE t2.extend_market_ratio END AS Market_Ratio
from tmp.tmp_ext_xiehe_pack_property_tmp t1
left join (select * from dwd.dwd_gnd_ims_tblmarket WHERE Extend_Market IS NULL AND ( NOT_IN_FLAG IS NULL or  NOT_IN_FLAG = '1' )) t2
on  t1.ATC1_COD = case when t2.ATC1_Code is null then t1.ATC1_COD else t2.ATC1_Code end
and t1.ATC2_COD = case when t2.ATC2_Code is null then t1.ATC2_COD else t2.ATC2_Code end
and t1.ATC3_COD = case when t2.ATC3_Code is null then t1.ATC3_COD else t2.ATC3_Code end
and t1.ATC4_COD = case when t2.ATC4_Code is null then t1.ATC4_COD else t2.ATC4_Code end
and t1.APP1_COD = case when t2.NFC1_Code is null then t1.APP1_COD else t2.NFC1_Code end
and t1.APP2_COD = case when t2.NFC2_Code is null then t1.APP2_COD else t2.NFC2_Code end
and t1.APP3_COD = case when t2.NFC3_Code is null then t1.APP3_COD else t2.NFC3_Code end
and t1.CORP_COD = case when t2.corporation_code is null then t1.CORP_COD else t2.corporation_code end
and t1.MANU_COD = case when t2.Manufacturer_Code is null then t1.MANU_COD else t2.Manufacturer_Code end
and right(concat('000000000', coalesce(t1.IQVIA_PROD_CODE,'')),9)  = case when  t2.Product_Code  is null then right(concat('000000000', coalesce(t1.IQVIA_PROD_CODE,'')),9)  else right(concat('000000000', coalesce(t2.Product_Code,'')),9) end
-- and t1.IQVIA_PACK_CODE = case when t2.Pack_Code is null then t1.IQVIA_PACK_CODE else t2.Pack_Code end
and nvl(t1.STGH_DES,'') = case when t2.Strength is null then nvl(t1.STGH_DES,'') else nvl(t2.Strength,'') end
and right(concat('000000', coalesce(t1.CMPS_COD,'')),6)   = case when t2.Molecule_Code is null then right(concat('000000', coalesce(t1.CMPS_COD,'')),6)  else right(concat('000000', coalesce( t2.Molecule_Code ,'')),6) end
and if( coalesce(t1.IQVIA_PACK_CODE,'') REGEXP '^[0-9]',right(concat('000000000000', coalesce(t1.IQVIA_PACK_CODE,'')),12),coalesce(t1.IQVIA_PACK_CODE,'')) 
= case when t2.pack_code is null 
then if( coalesce(t1.IQVIA_PACK_CODE,'') REGEXP '^[0-9]',right(concat('000000000000', coalesce(t1.IQVIA_PACK_CODE,'')),12),coalesce(t1.IQVIA_PACK_CODE,''))
else if( coalesce(t2.pack_code,'') REGEXP '^[0-9]',right(concat('000000000000', coalesce(t2.pack_code,'')),12),coalesce(t2.pack_code,'')) end
where t2.market is not null
;

--第二步：
--处理 Extend_Market 为空，并且 NOT_IN_FLAG 有值的数据，代表反选定义市场的规则
insert overwrite table tmp.tmp_ext_xiehe_pack_property_del 
select distinct t2.market,t1.*,t2.bu,CASE WHEN t2.extend_market_ratio IS NULL THEN '1' ELSE t2.extend_market_ratio END AS Market_Ratio
	from tmp.tmp_ext_xiehe_pack_property_tmp  t1
	left join (select * from dwd.dwd_gnd_ims_tblmarket WHERE Extend_Market IS NULL AND  NOT_IN_FLAG = '0' ) t2
	on  t1.ATC1_COD = case when t2.ATC1_Code is null then t1.ATC1_COD else t2.ATC1_Code end
	and t1.ATC2_COD = case when t2.ATC2_Code is null then t1.ATC2_COD else t2.ATC2_Code end
	and t1.ATC3_COD = case when t2.ATC3_Code is null then t1.ATC3_COD else t2.ATC3_Code end
	and t1.ATC4_COD = case when t2.ATC4_Code is null then t1.ATC4_COD else t2.ATC4_Code end
	and t1.APP1_COD = case when t2.NFC1_Code is null then t1.APP1_COD else t2.NFC1_Code end
	and t1.APP2_COD = case when t2.NFC2_Code is null then t1.APP2_COD else t2.NFC2_Code end
	and t1.APP3_COD = case when t2.NFC3_Code is null then t1.APP3_COD else t2.NFC3_Code end
	and t1.CORP_COD = case when t2.corporation_code is null then t1.CORP_COD else t2.corporation_code end
	and t1.MANU_COD = case when t2.Manufacturer_Code is null then t1.MANU_COD else t2.Manufacturer_Code end
	and right(concat('000000000', coalesce(t1.IQVIA_PROD_CODE,'')),9)  = case when  t2.Product_Code  is null then right(concat('000000000', coalesce(t1.IQVIA_PROD_CODE,'')),9)  else right(concat('000000000', coalesce(t2.Product_Code,'')),9) end
	and nvl(t1.STGH_DES,'') = case when t2.Strength is null then nvl(t1.STGH_DES,'') else nvl(t2.Strength,'') end
	and right(concat('000000', coalesce(t1.CMPS_COD,'')),6)   = case when t2.Molecule_Code is null then right(concat('000000', coalesce(t1.CMPS_COD,'')),6)  else right(concat('000000', coalesce( t2.Molecule_Code ,'')),6) end
	and if( coalesce(t1.IQVIA_PACK_CODE,'') REGEXP '^[0-9]',right(concat('000000000000', coalesce(t1.IQVIA_PACK_CODE,'')),12),coalesce(t1.IQVIA_PACK_CODE,'')) 
	= case when t2.pack_code is null 
	then if( coalesce(t1.IQVIA_PACK_CODE,'') REGEXP '^[0-9]',right(concat('000000000000', coalesce(t1.IQVIA_PACK_CODE,'')),12),coalesce(t1.IQVIA_PACK_CODE,''))
	else if( coalesce(t2.pack_code,'') REGEXP '^[0-9]',right(concat('000000000000', coalesce(t2.pack_code,'')),12),coalesce(t2.pack_code,'')) end
	where t2.market is not null
 ;

--！！！注意此段是反选的规则，故执行的是从已经定义好的TempMKT中删除数据
--需要看TempMKT的字段结构，没有值的字段，在Value里面放个空值
MERGE INTO tmp.tmp_ext_xiehe_pack_property AS t1
USING tmp.tmp_ext_xiehe_pack_property_del AS t2
	 on  t1.ATC1_COD = t2.ATC1_COD
     and t1.ATC2_COD = t2.ATC2_COD
     and t1.ATC3_COD = t2.ATC3_COD
     and t1.ATC4_COD = t2.ATC4_COD
     and t1.APP1_COD = t2.APP1_COD
     and t1.APP2_COD = t2.APP2_COD
     and t1.APP3_COD = t2.APP3_COD
     and t1.CORP_COD = t2.CORP_COD
     and t1.MANU_COD = t2.MANU_COD
     and t1.IQVIA_PROD_CODE = t2.IQVIA_PROD_CODE
     and t1.IQVIA_PACK_CODE = t2.IQVIA_PACK_CODE
     and t1.STGH_DES = t2.STGH_DES
     and t1.CMPS_COD = t2.CMPS_COD
     and t1.market = t2.market
WHEN MATCHED THEN DELETE 
;


---第三步：
--处理 Extend_Market 有值得数据，用已有市场，定义新的市场，带上系数
insert into  tmp.tmp_ext_xiehe_pack_property
select distinct 
	t2.Market,
	t1.iqvia_pack_code ,
	t1.iqvia_prod_code
	,t1.pack_des      
	,t1.APP1_COD
	,t1.APP1_DES
	,t1.APP1_DES_C
	,t1.APP2_COD
	,t1.APP2_DES
	,t1.APP2_DES_C
	,t1.APP3_COD
	,t1.APP3_DES   
	,t1.APP3_DES_C   
	,t1.ATC1_COD   
	,t1.ATC1_DES
	,t1.ATC1_DES_C   
	,t1.ATC2_COD  
	,t1.ATC2_DES   
	,t1.ATC2_DES_C
	,t1.ATC3_COD
	,t1.ATC3_DES
	,t1.ATC3_DES_C
	,t1.ATC4_COD
	,t1.ATC4_DES
	,t1.ATC4_DES_C
	,t1.BIO_DESC
	,t1.CMPS_COD
	,t1.CMPS_DES
	,t1.CMPS_DES_C
  ,t1.corp_cod
	,t1.CORP_DES
	,t1.corp_des_c
	,t1.EDL_DESC
	,t1.ETH_OTC_DESC
	,t1.GENE_ORIG_DESC
	,t1.GQCE_DESC
	,t1.manu_cod 
  ,t1.manu_des 
  ,t1.manu_des_c
	,t1.MNFL_COD           
 	,t1.MNFL_DES    
	,t1.NRDL_DESC
	,t1.STGH_DES
	,t1.PACK_LCH
	,t1.PAED_DESC
	,t1.PROD_DES
	,t1.PROD_DES_C  
	,t1.TCM_DESC
	,t1.VBP_DESC   
	,t1.Unit
	,t1.Counting_Unit
	,t1.Dosage_Unit
  ,t2.BU
  ,case when t2.Extend_Market_Ratio is null then '1' end AS Market_Ratio   --市场的系数若没有维护，默认为1
from tmp.tmp_ext_xiehe_pack_property t1
left join (select Market, BU,Extend_Market,Extend_Market_Ratio from dwd.dwd_gnd_ims_tblmarket  WHERE Extend_Market IS NOT NULL) t2 on t1.Market=t2.Extend_Market
where t2.Market is not null
;

-- COMMAND ----------

--补上未匹配到Market的pack
insert into tmp.tmp_ext_xiehe_pack_property
(
Market,
IQVIA_PACK_CODE,
IQVIA_PROD_CODE,
pack_des,
APP1_COD,
APP1_DES,
APP1_DES_C,
APP2_COD,
APP2_DES,
APP2_DES_C,
APP3_COD,
APP3_DES,
APP3_DES_C,
ATC1_COD,
ATC1_DES,
ATC1_DES_C,
ATC2_COD,
ATC2_DES,
ATC2_DES_C,
ATC3_COD,
ATC3_DES,
ATC3_DES_C,
ATC4_COD,
ATC4_DES,
ATC4_DES_C,
BIO_DESC,
CMPS_COD,
CMPS_DES,
CMPS_DES_C,
corp_cod,
CORP_DES,
corp_des_c,
EDL_DESC,
ETH_OTC_DESC,
GENE_ORIG_DESC,
GQCE_DESC,
manu_cod,
manu_des,
manu_des_c,
MNFL_COD,
MNFL_DES,
NRDL_DESC,
STGH_DES,
PACK_LCH,
PAED_DESC,
PROD_DES,
PROD_DES_C,
TCM_DESC,
VBP_DESC,
Unit,
Counting_Unit,
Dosage_Unit,
bu,
Market_Ratio
)
select distinct
'Non Market' as Market,
t1.IQVIA_PACK_CODE,
t1.IQVIA_PROD_CODE,
t1.pack_des,
t1.APP1_COD,
t1.APP1_DES,
t1.APP1_DES_C,
t1.APP2_COD,
t1.APP2_DES,
t1.APP2_DES_C,
t1.APP3_COD,
t1.APP3_DES,
t1.APP3_DES_C,
t1.ATC1_COD,
t1.ATC1_DES,
t1.ATC1_DES_C,
t1.ATC2_COD,
t1.ATC2_DES,
t1.ATC2_DES_C,
t1.ATC3_COD,
t1.ATC3_DES,
t1.ATC3_DES_C,
t1.ATC4_COD,
t1.ATC4_DES,
t1.ATC4_DES_C,
t1.BIO_DESC,
t1.CMPS_COD,
t1.CMPS_DES,
t1.CMPS_DES_C,
t1.corp_cod,
t1.CORP_DES,
t1.corp_des_c,
t1.EDL_DESC,
t1.ETH_OTC_DESC,
t1.GENE_ORIG_DESC,
t1.GQCE_DESC,
t1.manu_cod,
t1.manu_des,
t1.manu_des_c,
t1.MNFL_COD,
t1.MNFL_DES,
t1.NRDL_DESC,
t1.STGH_DES,
t1.PACK_LCH,
t1.PAED_DESC,
t1.PROD_DES,
t1.PROD_DES_C,
t1.TCM_DESC,
t1.VBP_DESC,
t1.Unit,
t1.Counting_Unit,
t1.Dosage_Unit,
'' as bu,
'1' as Market_Ratio
from tmp.tmp_ext_xiehe_pack_property_tmp t1
left join tmp.tmp_ext_xiehe_pack_property t2
on t1.IQVIA_PACK_CODE = t2.IQVIA_PACK_CODE
where t2.IQVIA_PACK_CODE is null

-- COMMAND ----------

----------------------------------------------------补齐维度信息---------------------------
------对属于az 的产品打上 is az 标识 
insert overwrite table  dws.dws_ext_xiehe_pack_property 
select 
    concat(t1.Market,'_',T1.IQVIA_PACK_CODE) as MARKET_PACK_KEY 
    ,t1.market
    ,t1.IQVIA_PACK_CODE
    ,t1.IQVIA_PROD_CODE
    ,t1.pack_des
    ,t1.APP1_COD
    ,t1.APP1_DES
    ,t1.APP1_DES_C
    ,t1.APP2_COD
    ,t1.APP2_DES
    ,t1.APP2_DES_C
    ,t1.APP3_COD
    ,t1.APP3_DES
    ,t1.APP3_DES_C
    ,t1.ATC1_COD
    ,t1.ATC1_DES
    ,t1.ATC1_DES_C
    ,t1.ATC2_COD
    ,t1.ATC2_DES
    ,t1.ATC2_DES_C
    ,t1.ATC3_COD
    ,t1.ATC3_DES
    ,t1.ATC3_DES_C
    ,t1.ATC4_COD
    ,t1.ATC4_DES
    ,t1.ATC4_DES_C
    ,t1.BIO_DESC
    ,t1.CMPS_COD
    ,t1.CMPS_DES
    ,t1.CMPS_DES_C
    ,t1.corp_cod
    ,t1.CORP_DES
    ,t1.corp_des_c
    ,t1.EDL_DESC
    ,t1.ETH_OTC_DESC
    ,t1.GENE_ORIG_DESC
    ,t1.GQCE_DESC
    ,t1.manu_cod
    ,t1.manu_des
    ,t1.manu_des_c
    ,t1.MNFL_COD
    ,t1.MNFL_DES
    ,t1.NRDL_DESC
    ,t1.STGH_DES
    ,t1.PACK_LCH
    ,t1.PAED_DESC
    ,t1.PROD_DES
    ,t1.PROD_DES_C
    ,t1.TCM_DESC
    ,t1.VBP_DESC
    ,t1.Unit
    ,t1.Counting_Unit
    ,t1.Dosage_Unit
    ,t1.bu
    ,t1.Market_Ratio
    ,NRDL_ENTRY_DATE
    ,case when ta.prod_code is null then 'N' ELSE 'Y' END IS_AZ
    ,null as AZ_MAIN
    ,BRANDTYPE.brand_type   AS BRANDTYPE
from  tmp.tmp_ext_xiehe_pack_property  t1
left join (select distinct  Pack_Code,NRDL_ENTRY_DATE from  dwd.dwd_ims_td_pack_additional_attribute ) t2 on t1.IQVIA_PACK_CODE =t2.Pack_Code
left join (select distinct IQVIA_PROD_CODE prod_code from tmp.tmp_ext_xiehe_pack_property where corp_cod='A5Z'  ) ta on t1.IQVIA_PROD_CODE=ta.prod_code
LEFT JOIN dwd.dwd_gnd_ims_tblbrandtype BRANDTYPE ON t1.IQVIA_PACK_CODE = BRANDTYPE.PACK_COD
;


-- COMMAND ----------

----------------------------------------------------补齐维度信息---------------------------
insert overwrite table tmp.tmp_dm_ext_xiehe_pack_property 
select 
    t1.MARKET_PACK_KEY 
    ,t1.market
    ,t1.IQVIA_PACK_CODE
    ,t1.IQVIA_PROD_CODE
    ,t1.pack_des
    ,t1.APP1_COD
    ,t1.APP1_DES
    ,t1.APP1_DES_C
    ,t1.APP2_COD
    ,t1.APP2_DES
    ,t1.APP2_DES_C
    ,t1.APP3_COD
    ,t1.APP3_DES
    ,t1.APP3_DES_C
    ,t1.ATC1_COD
    ,t1.ATC1_DES
    ,t1.ATC1_DES_C
    ,t1.ATC2_COD
    ,t1.ATC2_DES
    ,t1.ATC2_DES_C
    ,t1.ATC3_COD
    ,t1.ATC3_DES
    ,t1.ATC3_DES_C
    ,t1.ATC4_COD
    ,t1.ATC4_DES
    ,t1.ATC4_DES_C
    ,t1.BIO_DESC
    ,t1.CMPS_COD
    ,t1.CMPS_DES
    ,t1.CMPS_DES_C
    ,t1.corp_cod
    ,t1.CORP_DES
    ,t1.corp_des_c
    ,t1.EDL_DESC
    ,t1.ETH_OTC_DESC
    ,t1.GENE_ORIG_DESC
    ,t1.GQCE_DESC
    ,t1.manu_cod
    ,t1.manu_des
    ,t1.manu_des_c
    ,t1.MNFL_COD
    ,t1.MNFL_DES
    ,t1.NRDL_DESC
    ,t1.STGH_DES
    ,t1.PACK_LCH
    ,t1.PAED_DESC
    ,t1.PROD_DES
    ,t1.PROD_DES_C
    ,t1.TCM_DESC
    ,t1.VBP_DESC
    ,t1.Unit
    ,t1.Counting_Unit
    ,t1.Dosage_Unit
    ,t1.bu
    ,t1.Market_Ratio
    ,t1.NRDL_ENTRY_DATE
    ,t1.IS_AZ
    ,t1.AZ_MAIN
    ,t1.BRANDTYPE
    ,nvl(cla.class,'Others')   class 
    ,ta.ta
from dws.dws_ext_xiehe_pack_property t1 
left join dwd.dwd_gnd_ext_retail_dim_ta ta   on t1.market = ta.market
left join dwd.dwd_gnd_tblclass cla  on t1.MARKET = case when cla.market is null then t1.market else cla.market end  
 and nvl(t1.iqvia_pack_code,'') = case when cla.pack_code is null   then  nvl(t1.iqvia_pack_code,'') 
    else if( cla.pack_code REGEXP '^[0-9]',right(concat('000000000000',cla.pack_code),12),cla.pack_code)  end --right(concat('0000000',cla.pack_code ), 7)
 and nvl(t1.IQVIA_PROD_CODE,'') =        case when cla.product_code	  is null then nvl(t1.IQVIA_PROD_CODE       ,'')  
    else  right(concat('000000000',cla.product_code ),9 ) end --right(concat('00000',cla.product_code ),5 )
 and nvl(t1.CMPS_COD,'') =        case when cla.molecule_code	  is null then nvl(t1.cmps_cod ,'') else right(concat('000000', cla.molecule_code),6 )  end 
 and nvl(t1.corp_cod,'') =        case when cla.Corporation_code  is null then nvl(t1.corp_cod       ,'') else  cla.Corporation_code  end
  and nvl(t1.manu_cod,'')=        case when cla.Manufacturer_Code is null then nvl(t1.manu_cod,'') else cla.Manufacturer_Code end  
  and nvl(t1.atc1_cod,'')=		 case when cla.ATC1_Code is null then  nvl(t1.atc1_cod       ,'') else  cla.ATC1_Code end 
 and nvl(t1.ATC2_COD,'') =		 case when cla.ATC2_Code  is null then  nvl(t1.ATC2_COD       ,'') else cla.ATC2_Code  end  
 and nvl(t1.ATC3_COD,'') =		 case when cla.ATC3_Code is null then  nvl(t1.ATC3_COD       ,'') else  cla.ATC3_Code end
 and nvl(t1.ATC4_COD,'') =		 case when cla.ATC4_Code is null then  nvl(t1.ATC4_COD       ,'') else  cla.ATC4_Code end
 and nvl(t1.app1_cod,'') =		 case when cla.NFC1_Code is null then  nvl(t1.app1_cod       ,'') else  cla.NFC1_Code end
 and nvl(t1.APP2_COD,'') =		 case when cla.NFC2_Code is null then  nvl(t1.APP2_COD       ,'') else  cla.NFC2_Code end
 and nvl(t1.APP3_COD,'') =		 case when cla.NFC3_Code is null then  nvl(t1.APP3_COD       ,'') else  cla.NFC3_Code end
 and nvl(t1.stgh_des,'') =		 case when cla.Strength  is null then  nvl(t1.stgh_des       ,'') else  cla.Strength  end
;


-- COMMAND ----------

with ims_pack_property as (
select
		MARKET_PACK_KEY,
		max(PACK_COD) as PACK_COD,
		max(PACK_DES) as PACK_DES,
		max(STGH_DES) as STGH_DES,
		max(PACK_LCH) as PACK_LCH,
		max(Family_Code) as Family_Code,
		max(Family_Name) as Family_Name,
		max(PROD_COD) as PROD_COD,
		max(PROD_DES) as PROD_DES,
		max(PROD_DES_C) as PROD_DES_C,
		max(CMPS_COD) as CMPS_COD,
		max(CMPS_DES) as CMPS_DES,
		max(CMPS_DES_C) as CMPS_DES_C,
		max(ATC1_COD) as ATC1_COD,
		max(ATC2_COD) as ATC2_COD,
		max(ATC3_COD) as ATC3_COD,
		max(ATC4_COD) as ATC4_COD,
		max(APP1_COD) as APP1_COD,
		max(APP2_COD) as APP2_COD,
		max(APP3_COD) as APP3_COD,
		max(BIO_DESC) as BIO_DESC,
		max(GENE_ORIG_DESC) as GENE_ORIG_DESC,
		max(ETH_OTC_DESC) as ETH_OTC_DESC,
		max(NRDL_DESC) as NRDL_DESC,
		max(NRDL_Entry_Date) as NRDL_Entry_Date,
		max(EDL_DESC) as EDL_DESC,
		max(TCM_DESC) as TCM_DESC,
		max(PAED_DESC) as PAED_DESC,
		max(GQCE_DESC) as GQCE_DESC,
		max(VBP_DESC) as VBP_DESC,
		max(MANU_COD) as MANU_COD,
		max(MANU_DES) as MANU_DES,
		max(MANU_DES_C) as MANU_DES_C,
		max(MNFL_COD) as MNFL_COD,
		max(MNFL_DES) as MNFL_DES,
		max(CORP_COD) as CORP_COD,
		max(CORP_DES) as CORP_DES,
		max(CORP_DES_C) as CORP_DES_C,
		max(BrandType) as BrandType,
		max(MARKET) as MARKET,
		max(KEY_COMPETITOR) as KEY_COMPETITOR,
		max(IS_AZ) as IS_AZ,
		max(AZ_MAIN) as AZ_MAIN,
		max(AZ_Related) as AZ_Related,
		max(atc1_des) as atc1_des,
		max(atc1_des_c) as atc1_des_c,
		max(atc2_des) as atc2_des,
		max(atc2_des_c) as atc2_des_c,
		max(atc3_des) as atc3_des,
		max(atc3_des_c) as atc3_des_c,
		max(atc4_des) as atc4_des,
		max(atc4_des_c) as atc4_des_c,
		max(app1_des) as app1_des,
		max(app1_des_c) as app1_des_c,
		max(app2_des) as app2_des,
		max(app2_des_c) as app2_des_c,
		max(app3_des) as app3_des,
		max(app3_des_c) as app3_des_c,
		max(Class) as Class
from dm.dm_ims_td_pack_property
group by MARKET_PACK_KEY
)

insert overwrite table dm.dm_ext_xiehe_pack_property (
MARKET_PACK_KEY,
PACK_COD,
PACK_DES,
STGH_DES,
PACK_LCH,
Family_Code,
Family_Name,
PROD_COD,
PROD_DES,
PROD_DES_C,
CMPS_COD,
CMPS_DES,
CMPS_DES_C,
ATC1_COD,
ATC2_COD,
ATC3_COD,
ATC4_COD,
APP1_COD,
APP2_COD,
APP3_COD,
BIO_DESC,
GENE_ORIG_DESC,
ETH_OTC_DESC,
NRDL_DESC,
NRDL_Entry_Date,
EDL_DESC,
TCM_DESC,
PAED_DESC,
GQCE_DESC,
VBP_DESC,
MANU_COD,
MANU_DES,
MANU_DES_C,
MNFL_COD,
MNFL_DES,
CORP_COD,
CORP_DES,
CORP_DES_C,
BrandType,
MARKET,
KEY_COMPETITOR,
IS_AZ,
AZ_MAIN,
AZ_Related,
atc1_des,
atc1_des_c,
atc2_des,
atc2_des_c,
atc3_des,
atc3_des_c,
atc4_des,
atc4_des_c,
app1_des,
app1_des_c,
app2_des,
app2_des_c,
app3_des,
app3_des_c,
Class,
ETL_INSERT_DT,
ETL_UPDATE_DT,
Market_Ratio,
counting_unit,
bu,
Dosage_Unit,
ta
)
select distinct
    t1.market_pack_key,
    t1.iqvia_pack_code as PACK_COD,
    coalesce(t2.PACK_DES,t1.pack_des) as PACK_DES,
    coalesce(t2.STGH_DES,t1.stgh_des) as STGH_DES,
    coalesce(t2.PACK_LCH,t1.pack_lch) as PACK_LCH,
    t2.Family_Code,
    t2.Family_Name,
    coalesce(t2.PROD_COD,t1.iqvia_prod_code) as PROD_COD,
    coalesce(t2.PROD_DES,t1.prod_des) as PROD_DES,
    coalesce(t2.PROD_DES_C,t1.prod_des_c) as PROD_DES_C,
    coalesce(t2.CMPS_COD,t1.cmps_cod) as CMPS_COD,
    coalesce(t2.CMPS_DES,t1.cmps_des) as CMPS_DES,
    coalesce(t2.CMPS_DES_C,t1.cmps_des_c) as CMPS_DES_C,
    coalesce(t2.ATC1_COD,t1.atc1_cod) as ATC1_COD,
    coalesce(t2.ATC2_COD,t1.atc2_cod) as ATC2_COD,
    coalesce(t2.ATC3_COD,t1.atc3_cod) as ATC3_COD,
    coalesce(t2.ATC4_COD,t1.atc4_cod) as ATC4_COD,
    coalesce(t2.APP1_COD,t1.app1_cod) as APP1_COD,
    coalesce(t2.APP2_COD,t1.app2_cod) as APP2_COD,
    coalesce(t2.APP3_COD,t1.app3_cod) as APP3_COD,
    coalesce(t2.BIO_DESC,t1.bio_desc) as BIO_DESC,
    coalesce(t2.GENE_ORIG_DESC,t1.gene_orig_desc) as GENE_ORIG_DESC,
    coalesce(t2.ETH_OTC_DESC,t1.eth_otc_desc) as ETH_OTC_DESC,
    coalesce(t2.NRDL_DESC,t1.nrdl_desc) as NRDL_DESC,
    coalesce(t2.NRDL_Entry_Date,t1.NRDL_Entry_Date) as NRDL_Entry_Date,
    coalesce(t2.EDL_DESC,t1.edl_desc) as EDL_DESC,
    coalesce(t2.TCM_DESC,t1.tcm_desc) as TCM_DESC,
    coalesce(t2.PAED_DESC,t1.paed_desc) as PAED_DESC,
    coalesce(t2.GQCE_DESC,t1.gqce_desc) as GQCE_DESC,
    coalesce(t2.VBP_DESC,t1.vbp_desc) as VBP_DESC,
    coalesce(t2.MANU_COD,t1.manu_cod) as MANU_COD,
    coalesce(t2.MANU_DES,t1.manu_des) as MANU_DES,
    coalesce(t2.MANU_DES_C,t1.manu_des_c) as MANU_DES_C,
    coalesce(t2.MNFL_COD,t1.mnfl_cod) as MNFL_COD,
    coalesce(t2.MNFL_DES,t1.mnfl_des) as MNFL_DES,
    coalesce(t2.CORP_COD,t1.corp_cod) as CORP_COD,
    coalesce(t2.CORP_DES,t1.corp_des) as CORP_DES,
    coalesce(t2.CORP_DES_C,t1.corp_des_c) as CORP_DES_C,
    coalesce(t2.BrandType,t1.BrandType) as BrandType,
    t1.market,
    t2.KEY_COMPETITOR,
    coalesce(t2.IS_AZ,t1.IS_AZ) as IS_AZ,
    coalesce(t2.AZ_MAIN,t1.AZ_MAIN) as AZ_MAIN,
    t2.AZ_Related,
    coalesce(t2.atc1_des,t1.atc1_des) as atc1_des,
    coalesce(t2.atc1_des_c,t1.atc1_des_c) as atc1_des_c,
    coalesce(t2.atc2_des,t1.atc2_des) as atc2_des,
    coalesce(t2.atc2_des_c,t1.atc2_des_c) as atc2_des_c,
    coalesce(t2.atc3_des,t1.atc3_des) as atc3_des,
    coalesce(t2.atc3_des_c,t1.atc3_des_c) as atc3_des_c,
    coalesce(t2.atc4_des,t1.atc4_des) as atc4_des,
    coalesce(t2.atc4_des_c,t1.atc4_des_c) as atc4_des_c,
    coalesce(t2.app1_des,t1.app1_des) as app1_des,
    coalesce(t2.app1_des_c,t1.app1_des_c) as app1_des_c,
    coalesce(t2.app2_des,t1.app2_des) as app2_des,
    coalesce(t2.app2_des_c,t1.app2_des_c) as app2_des_c,
    coalesce(t2.app3_des,t1.app3_des) as app3_des,
    coalesce(t2.app3_des_c,t1.app3_des_c) as app3_des_c,
    coalesce(t2.Class,t1.Class) as Class,
    from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_INSERT_DT,
    from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_UPDATE_DT,
    cast(t1.market_ratio as decimal(30,10)) as market_ratio,
    coalesce(cast(t1.counting_unit / coalesce(t1.unit,1) as decimal(30,10)),1) as counting_unit,
    t1.bu,
    t1.Dosage_Unit,
    t1.ta
from tmp.tmp_dm_ext_xiehe_pack_property t1
left join ims_pack_property t2
on t1.market_pack_key = t2.market_pack_key
where t1.market <> 'Non Market'
union
select distinct
    concat('XIEHE ALL Market_',t1.iqvia_pack_code) as market_pack_key,
    t1.iqvia_pack_code as PACK_COD,
    max(coalesce(t2.PACK_DES,t1.pack_des)) as PACK_DES,
    max(coalesce(t2.STGH_DES,t1.stgh_des)) as STGH_DES,
    max(coalesce(t2.PACK_LCH,t1.pack_lch)) as PACK_LCH,
    max(t2.Family_Code) as Family_Code,
    max(t2.Family_Name) as Family_Name,
    max(coalesce(t2.PROD_COD,t1.iqvia_prod_code)) as PROD_COD,
    max(coalesce(t2.PROD_DES,t1.prod_des)) as PROD_DES,
    max(coalesce(t2.PROD_DES_C,t1.prod_des_c)) as PROD_DES_C,
    max(coalesce(t2.CMPS_COD,t1.cmps_cod)) as CMPS_COD,
    max(coalesce(t2.CMPS_DES,t1.cmps_des)) as CMPS_DES,
    max(coalesce(t2.CMPS_DES_C,t1.cmps_des_c)) as CMPS_DES_C,
    max(coalesce(t2.ATC1_COD,t1.atc1_cod)) as ATC1_COD,
    max(coalesce(t2.ATC2_COD,t1.atc2_cod)) as ATC2_COD,
    max(coalesce(t2.ATC3_COD,t1.atc3_cod)) as ATC3_COD,
    max(coalesce(t2.ATC4_COD,t1.atc4_cod)) as ATC4_COD,
    max(coalesce(t2.APP1_COD,t1.app1_cod)) as APP1_COD,
    max(coalesce(t2.APP2_COD,t1.app2_cod)) as APP2_COD,
    max(coalesce(t2.APP3_COD,t1.app3_cod)) as APP3_COD,
    max(coalesce(t2.BIO_DESC,t1.bio_desc)) as BIO_DESC,
    max(coalesce(t2.GENE_ORIG_DESC,t1.gene_orig_desc)) as GENE_ORIG_DESC,
    max(coalesce(t2.ETH_OTC_DESC,t1.eth_otc_desc)) as ETH_OTC_DESC,
    max(coalesce(t2.NRDL_DESC,t1.nrdl_desc)) as NRDL_DESC,
    max(coalesce(t2.NRDL_Entry_Date,t1.NRDL_Entry_Date)) as NRDL_Entry_Date,
    max(coalesce(t2.EDL_DESC,t1.edl_desc)) as EDL_DESC,
    max(coalesce(t2.TCM_DESC,t1.tcm_desc)) as TCM_DESC,
    max(coalesce(t2.PAED_DESC,t1.paed_desc)) as PAED_DESC,
    max(coalesce(t2.GQCE_DESC,t1.gqce_desc)) as GQCE_DESC,
    max(coalesce(t2.VBP_DESC,t1.vbp_desc)) as VBP_DESC,
    max(coalesce(t2.MANU_COD,t1.manu_cod)) as MANU_COD,
    max(coalesce(t2.MANU_DES,t1.manu_des)) as MANU_DES,
    max(coalesce(t2.MANU_DES_C,t1.manu_des_c)) as MANU_DES_C,
    max(coalesce(t2.MNFL_COD,t1.mnfl_cod)) as MNFL_COD,
    max(coalesce(t2.MNFL_DES,t1.mnfl_des)) as MNFL_DES,
    max(coalesce(t2.CORP_COD,t1.corp_cod)) as CORP_COD,
    max(coalesce(t2.CORP_DES,t1.corp_des)) as CORP_DES,
    max(coalesce(t2.CORP_DES_C,t1.corp_des_c)) as CORP_DES_C,
    max(coalesce(t2.BrandType,t1.BrandType)) as BrandType,
    'XIEHE ALL Market' as MARKET,
    max(t2.KEY_COMPETITOR) as KEY_COMPETITOR,
    max(coalesce(t2.IS_AZ,t1.IS_AZ)) as IS_AZ,
    max(coalesce(t2.AZ_MAIN,t1.AZ_MAIN)) as AZ_MAIN,
    max(t2.AZ_Related) as AZ_Related,
    max(coalesce(t2.atc1_des,t1.atc1_des)) as atc1_des,
    max(coalesce(t2.atc1_des_c,t1.atc1_des_c)) as atc1_des_c,
    max(coalesce(t2.atc2_des,t1.atc2_des)) as atc2_des,
    max(coalesce(t2.atc2_des_c,t1.atc2_des_c)) as atc2_des_c,
    max(coalesce(t2.atc3_des,t1.atc3_des)) as atc3_des,
    max(coalesce(t2.atc3_des_c,t1.atc3_des_c)) as atc3_des_c,
    max(coalesce(t2.atc4_des,t1.atc4_des)) as atc4_des,
    max(coalesce(t2.atc4_des_c,t1.atc4_des_c)) as atc4_des_c,
    max(coalesce(t2.app1_des,t1.app1_des)) as app1_des,
    max(coalesce(t2.app1_des_c,t1.app1_des_c)) as app1_des_c,
    max(coalesce(t2.app2_des,t1.app2_des)) as app2_des,
    max(coalesce(t2.app2_des_c,t1.app2_des_c)) as app2_des_c,
    max(coalesce(t2.app3_des,t1.app3_des)) as app3_des,
    max(coalesce(t2.app3_des_c,t1.app3_des_c)) as app3_des_c,
    max(coalesce(t2.Class,t1.Class)) as Class,
    from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_INSERT_DT,
    from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_UPDATE_DT,
    max(cast(t1.market_ratio as decimal(30,10))) as market_ratio,
    coalesce(max(cast(t1.counting_unit / coalesce(t1.unit,1) as decimal(30,10))),1) as counting_unit,
    max(t1.bu) as bu,
    max(t1.Dosage_Unit) as Dosage_Unit,
    max(t1.ta) as ta
from tmp.tmp_dm_ext_xiehe_pack_property t1
left join ims_pack_property t2
on t1.market_pack_key = t2.market_pack_key
group by t1.iqvia_pack_code
