-- Databricks notebook source
---------------------------------------------------------------------------
--修改时间：20241126
--修改人：Fanxujia
--修改内容：
--THC渠道分为14省的数据源以及全国的数据源两种，分在了2个事实表里，下面在维表在引用事实表时，需要将两个事实表union在一起
----------------------------------------------------------------------------
----------------------------------------------------------------------------
--修改时间：20241129
--修改人：Fanxujia
--修改内容：
--补充Retail直取中DTP的手工文件里的pack_cod
----------------------------------------------------------------------------
create or replace temporary view external_sales_union as
select distinct 
PACK_COD,
DATA_SOURCE,
PACK_FLAG,
CORP_COD,
SALES_VALUE_CAL,
dept_name,
new_code,
area,
YYYYMM
from dm.DM_TF_EXT_UNIONALL_SALES
union all
select distinct 
PACK_COD,
DATA_SOURCE,
PACK_FLAG,
CORP_COD,
SALES_VALUE_CAL,
dept_name,
new_code,
area,
YYYYMM
from dm.dm_tf_external_sales_thc
union all
select distinct 
pack_code,
'Retail(Quarterly)' as DATA_SOURCE,
null as PACK_FLAG,
CORP_COD,
0 as SALES_VALUE_CAL,
'' as dept_name,
'' as new_code,
'' as area,
'' as YYYYMM
from dm.dm_zk_retail_sales_kpi
where NVL(pack_code,'Others') <> 'Others'
and pack_code <> ''
----避免单独NIAD部分pack不存在于dm.dm_tf_external_sales中
union all
select distinct 
pack_cod,
DATA_SOURCE,
null as PACK_FLAG,
null CORP_COD,
0 as SALES_VALUE_CAL,
'' as dept_name,
'' as new_code,
'' as area,
'' as YYYYMM
from dm.dm_tf_external_sales_niad
where NVL(pack_cod,'Others') <> 'Others'
and pack_cod <> ''

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

----------------------------------------------------------为了避免数据量过大只获取了有销量的产品数据---------------------------------
---------------------------pack code是唯一，不允许重复或为空-----------------------------------------------------
insert overwrite dm.dm_td_external_packinfo_temp (
  PACK_COD,
  PACK_DES,
  STGH_DES,
  PACK_LCH,
  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,
  IS_AZ,
  AZ_MAIN,
  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,
  GEN_RN
)
  SELECT
    A.PACK_COD,
    MAX(B.PACK_DES) PACK_DES,
    MAX(B.STGH_DES) STGH_DES,
    MAX(B.PACK_LCH) PACK_LCH,
    MAX(B.PROD_COD) PROD_COD,
    MAX(B.PROD_DES) PROD_DES,
    MAX(B.PROD_DES_C) PROD_DES_C,
    MAX(B.CMPS_COD) CMPS_COD,
    MAX(B.CMPS_DES) CMPS_DES,
    MAX(B.CMPS_DES_C) CMPS_DES_C,
    MAX(B.ATC1_COD) ATC1_COD,
    MAX(B.ATC2_COD) ATC2_COD,
    MAX(B.ATC3_COD) ATC3_COD,
    MAX(B.ATC4_COD) ATC4_COD,
    MAX(B.APP1_COD) APP1_COD,
    MAX(B.APP2_COD) APP2_COD,
    MAX(B.APP3_COD) APP3_COD,
    MAX(B.BIO_DESC) BIO_DESC,
    MAX(B.GENE_ORIG_DESC) GENE_ORIG_DESC,
    MAX(B.ETH_OTC_DESC) ETH_OTC_DESC,
    MAX(B.NRDL_DESC) NRDL_DESC,
    MAX(B.NRDL_ENTRY_DATE) NRDL_ENTRY_DATE,
    MAX(B.EDL_DESC) EDL_DESC,
    MAX(B.TCM_DESC) TCM_DESC,
    MAX(B.PAED_DESC) PAED_DESC,
    MAX(B.GQCE_DESC) GQCE_DESC,
    CASE
      WHEN MAX(B.VBP_DESC_V) = 'VBP-IN' THEN 'VBP-IN'
      WHEN MAX(B.VBP_DESC_V) LIKE '%VBP%' THEN 'VBP-OUT'
      WHEN MAX(B.VBP_DESC_V) IS NOT NULL THEN 'Non VBP'
      ELSE NULL
    END VBP_DESC,
    MAX(B.MANU_COD) MANU_COD,
    MAX(B.MANU_DES) MANU_DES,
    MAX(B.MANU_DES_C) MANU_DES_C,
    MAX(B.MNFL_COD) MNFL_COD,
    MAX(B.MNFL_DES) MNFL_DES,
    MAX(B.CORP_COD) CORP_COD,
    MAX(B.CORP_DES) CORP_DES,
    MAX(B.CORP_DES_C) CORP_DES_C,
    MAX(B.BRANDTYPE) BRANDTYPE,
    MAX(B.IS_AZ) IS_AZ,
    MAX(B.AZ_MAIN) AZ_MAIN,
    MAX(B.ATC1_DES) ATC1_DES,
    MAX(B.ATC1_DES_C) ATC1_DES_C,
    MAX(B.ATC2_DES) ATC2_DES,
    MAX(B.ATC2_DES_C) ATC2_DES_C,
    MAX(B.ATC3_DES) ATC3_DES,
    MAX(B.ATC3_DES_C) ATC3_DES_C,
    MAX(B.ATC4_DES) ATC4_DES,
    MAX(B.ATC4_DES_C) ATC4_DES_C,
    MAX(B.APP1_DES) APP1_DES,
    MAX(B.APP1_DES_C) APP1_DES_C,
    MAX(B.APP2_DES) APP2_DES,
    MAX(B.APP2_DES_C) APP2_DES_C,
    MAX(B.APP3_DES) APP3_DES,
    MAX(B.APP3_DES_C) APP3_DES_C,
    CASE
      WHEN MAX(B.GENE_ORIG_DESC) = 'ORIG' THEN 1
      WHEN MAX(B.GENE_ORIG_DESC) = 'Branded Gen' THEN 2
      WHEN MAX(B.GENE_ORIG_DESC) = 'Unbranded Gen' THEN 3
      WHEN MAX(B.GENE_ORIG_DESC) = 'TCM' THEN 4
      ELSE 5
    END GEN_RN
  FROM
    (
      SELECT DISTINCT
        PACK_COD
      FROM
        external_sales_union-- WHERE YYYYMM >= (SELECT CAST((LEFT(MAX(YYYYMM),4)-3)*100+1 AS INT) FROM DM.DM_TF_EXT_UNIONALL_SALES)) A

    ) A
      LEFT JOIN DM.DM_IMS_TD_PACK_PROPERTY B
        ON B.PACK_COD = A.PACK_COD
  WHERE
    NVL(A.PACK_COD, '') <> ''
  GROUP BY
    A.PACK_COD

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

-------------------------------------------------------------补充COUNTY部分缺失的pack 维度--------------------------------------------------------------------------------------------
--01 根据主表数据(带多个source)关联chpa的pack_porperty表,取出相关维度
--02 按照步骤1，chpa中没有的packcode，他的维度字段会被置为空，所以要关联我们自己处理好的维度表，来补充相关维度
insert overwrite table tmp.tmp_county_dm_td_external_packinfo 
with temp as (    -- 临时表补充维度
  SELECT 
    b.PACK_COD,MAX(B.PACK_DES) PACK_DES,MAX(B.STGH_DES) STGH_DES,MAX(B.PACK_LCH) PACK_LCH,MAX(B.PROD_COD) PROD_COD,MAX(B.PROD_DES) PROD_DES,MAX(B.PROD_DES_C) PROD_DES_C,MAX(B.CMPS_COD) CMPS_COD,
MAX(B.CMPS_DES) CMPS_DES,MAX(B.CMPS_DES) CMPS_DES_C,MAX(B.ATC1_COD) ATC1_COD,MAX(B.ATC2_COD) ATC2_COD,MAX(B.ATC3_COD) ATC3_COD,MAX(B.ATC4_COD) ATC4_COD,MAX(B.APP1_COD) APP1_COD,MAX(B.APP2_COD) APP2_COD,
MAX(B.APP3_COD) APP3_COD,MAX(B.BIO_DESC) BIO_DESC,MAX(B.GENE_ORIG_DESC) GENE_ORIG_DESC,MAX(B.ETH_OTC_DESC) ETH_OTC_DESC,MAX(B.NRDL_DESC) NRDL_DESC,MAX(B.NRDL_ENTRY_DATE) NRDL_ENTRY_DATE,MAX(B.EDL_DESC) EDL_DESC,
MAX(B.TCM_DESC) TCM_DESC,MAX(B.PAED_DESC) PAED_DESC,MAX(B.GQCE_DESC) GQCE_DESC,
CASE WHEN MAX(B.VBP_DESC) = 'VBP-IN' THEN 'VBP-IN' WHEN MAX(B.VBP_DESC) LIKE '%VBP%' THEN 'VBP-OUT' WHEN MAX(B.VBP_DESC) IS NOT NULL THEN 'Non VBP' ELSE NULL END VBP_DESC,MAX(B.MANU_COD) MANU_COD,MAX(B.MANU_DES) MANU_DES,MAX(B.MANU_DES) MANU_DES_C,MAX(B.MNFL_COD) MNFL_COD,
MAX(B.MNFL_DES) MNFL_DES,MAX(B.CORP_COD) CORP_COD,MAX(B.CORP_DES) CORP_DES,MAX(B.CORP_DES_C) CORP_DES_C,MAX(B.BRANDTYPE) BRANDTYPE,MAX(B.IS_AZ) IS_AZ,MAX(B.AZ_MAIN) AZ_MAIN,MAX(B.ATC1_DES) ATC1_DES,
MAX(B.ATC1_DES_C) ATC1_DES_C,MAX(B.ATC2_DES) ATC2_DES,MAX(B.ATC2_DES_C) ATC2_DES_C,MAX(B.ATC3_DES) ATC3_DES,MAX(B.ATC3_DES_C) ATC3_DES_C,MAX(B.ATC4_DES) ATC4_DES,MAX(B.ATC4_DES_C) ATC4_DES_C,MAX(B.APP1_DES) APP1_DES,
MAX(B.APP1_DES_C) APP1_DES_C,MAX(B.APP2_DES) APP2_DES,MAX(B.APP2_DES_C) APP2_DES_C,MAX(B.APP3_DES) APP3_DES,MAX(B.APP3_DES_C) APP3_DES_C,
CASE WHEN MAX(B.GENE_ORIG_DESC) = 'ORIG' THEN 1 WHEN MAX(B.GENE_ORIG_DESC) = 'Branded Gen' THEN 2 WHEN MAX(B.GENE_ORIG_DESC) = 'Unbranded Gen' THEN 3 WHEN MAX(B.GENE_ORIG_DESC) = 'TCM' THEN 4 ELSE 5 END GEN_RN
FROM  dm.dm_ext_county_td_pack_property b
  GROUP BY b.PACK_COD
)
select
  A.PACK_COD
	,case when A.PACK_DES	='' or  A.PACK_DES	     is null then B.PACK_DES 	else  A.PACK_DES				end  AS PACK_DES
	,case when A.STGH_DES	='' or  A.STGH_DES	     is null then B.STGH_DES 	else  A.STGH_DES				end  AS STGH_DES
	,case when A.PACK_LCH	='' or  A.PACK_LCH	     is null then B.PACK_LCH 	else  A.PACK_LCH				end  AS PACK_LCH
	,case when A.PROD_COD	='' or  A.PROD_COD	     is null then B.PROD_COD 	else  A.PROD_COD				end  AS PROD_COD
	,case when A.PROD_DES	='' or  A.PROD_DES	     is null then B.PROD_DES 	else  A.PROD_DES				end  AS PROD_DES
	,case when A.PROD_DES_C	='' or  A.PROD_DES_C	 is null then B.PROD_DES_C	else  A.PROD_DES_C				end  AS PROD_DES_C
	,case when A.CMPS_COD	='' or  A.CMPS_COD	     is null then B.CMPS_COD	else  A.CMPS_COD				end  AS CMPS_COD
	,case when A.CMPS_DES	='' or  A.CMPS_DES	     is null then B.CMPS_DES	else  A.CMPS_DES				end  AS CMPS_DES
	,case when A.CMPS_DES_C	='' or  A.CMPS_DES_C	 is null then B.CMPS_DES_C	else  A.CMPS_DES_C				end  AS CMPS_DES_C
	,case when A.ATC1_COD	='' or  A.ATC1_COD	     is null then B.ATC1_COD    else  A.ATC1_COD				end  AS ATC1_COD
	,case when A.ATC2_COD	='' or  A.ATC2_COD	     is null then B.ATC2_COD    else  A.ATC2_COD				end  AS ATC2_COD
	,case when A.ATC3_COD	='' or  A.ATC3_COD	     is null then B.ATC3_COD    else  A.ATC3_COD				end  AS ATC3_COD
	,case when A.ATC4_COD	='' or  A.ATC4_COD	     is null then B.ATC4_COD    else  A.ATC4_COD				end  AS ATC4_COD
	,case when A.APP1_COD	='' or  A.APP1_COD	     is null then B.APP1_COD    else  A.APP1_COD				end  AS APP1_COD
	,case when A.APP2_COD	='' or  A.APP2_COD	     is null then B.APP2_COD    else  A.APP2_COD				end  AS APP2_COD
	,case when A.APP3_COD	='' or  A.APP3_COD	     is null then B.APP3_COD    else  A.APP3_COD				end  AS APP3_COD
	,case when A.BIO_DESC	='' or  A.BIO_DESC	     is null then B.BIO_DESC    else  A.BIO_DESC				end  AS BIO_DESC
	,case when A.GENE_ORIG_DESC	='' or A.GENE_ORIG_DESC	 is null then B.GENE_ORIG_DESC	else A.GENE_ORIG_DESC	end	 AS GENE_ORIG_DESC	 
	,case when A.ETH_OTC_DESC	='' or A.ETH_OTC_DESC	 is null then B.ETH_OTC_DESC	else A.ETH_OTC_DESC	    end	 AS ETH_OTC_DESC	 
	,case when A.NRDL_DESC		='' or A.NRDL_DESC		 is null then B.NRDL_DESC		else A.NRDL_DESC		end  AS NRDL_DESC    	 		 
	,case when A.NRDL_ENTRY_DATE='' or A.NRDL_ENTRY_DATE is null then B.NRDL_ENTRY_DATE else A.NRDL_ENTRY_DATE	end  AS NRDL_ENTRY_DATE    
	,case when A.EDL_DESC		='' or A.EDL_DESC		 is null then B.EDL_DESC		else A.EDL_DESC		    end  AS EDL_DESC          
	,case when A.TCM_DESC		='' or A.TCM_DESC		 is null then B.TCM_DESC		else A.TCM_DESC		    end  AS TCM_DESC          
	,case when A.PAED_DESC		='' or A.PAED_DESC		 is null then B.PAED_DESC		else A.PAED_DESC		end  AS PAED_DESC     	     
	,case when A.GQCE_DESC		='' or A.GQCE_DESC		 is null then B.GQCE_DESC		else A.GQCE_DESC		end  AS GQCE_DESC     	     
	,case when A.VBP_DESC		='' or A.VBP_DESC		 is null then B.VBP_DESC		else A.VBP_DESC			end	 AS VBP_DESC     
	,case when A.MANU_COD		='' or A.MANU_COD		 is null then B.MANU_COD		else A.MANU_COD			end	 AS MANU_COD     
	,case when A.MANU_DES		='' or A.MANU_DES		 is null then B.MANU_DES		else A.MANU_DES			end	 AS MANU_DES     
	,case when A.MANU_DES_C		='' or A.MANU_DES_C		 is null then B.MANU_DES_C		else A.MANU_DES_C		end  AS MANU_DES_C    
	,case when A.MNFL_COD		='' or A.MNFL_COD		 is null then B.MNFL_COD		else A.MNFL_COD			end	 AS MNFL_COD     
	,case when A.MNFL_DES		='' or A.MNFL_DES		 is null then B.MNFL_DES		else A.MNFL_DES			end	 AS MNFL_DES     
	,case when A.CORP_COD		='' or A.CORP_COD		 is null then B.CORP_COD		else A.CORP_COD			end	 AS CORP_COD    --  THC存在CORP_COD 归属不一致问题      
  ,REPLACE( case when A.CORP_DES ='' or A.CORP_DES is null then B.CORP_DES else A.CORP_DES end ,' GROUP', '' )       as CORP_DES   -- 消除group
	,case when A.CORP_DES_C		='' or A.CORP_DES_C		 is null then B.CORP_DES_C	    else A.CORP_DES_C		end  AS CORP_DES_C		     
	,case when A.BRANDTYPE		='' or A.BRANDTYPE		 is null then B.BRANDTYPE 	    else A.BRANDTYPE		end  AS BRANDTYPE			     
	,case when A.IS_AZ			='' or A.IS_AZ			 is null then B.IS_AZ		    else A.IS_AZ			end  AS IS_AZ				     
	,case when A.AZ_MAIN		='' or A.AZ_MAIN		 is null then B.AZ_MAIN		    else A.AZ_MAIN			end  AS AZ_MAIN		     
	,case when A.ATC1_DES		='' or A.ATC1_DES		 is null then B.ATC1_DES	    else A.ATC1_DES			end  AS ATC1_DES		     
	,case when A.ATC1_DES_C		='' or A.ATC1_DES_C		 is null then B.ATC1_DES_C 	    else A.ATC1_DES_C		end  AS ATC1_DES_C	         
	,case when A.ATC2_DES		='' or A.ATC2_DES		 is null then B.ATC2_DES 	    else A.ATC2_DES			end  AS ATC2_DES		     
	,case when A.ATC2_DES_C		='' or A.ATC2_DES_C		 is null then B.ATC2_DES_C	    else A.ATC2_DES_C		end  AS ATC2_DES_C	         
	,case when A.ATC3_DES		='' or A.ATC3_DES		 is null then B.ATC3_DES	    else A.ATC3_DES			end  AS ATC3_DES		     
	,case when A.ATC3_DES_C		='' or A.ATC3_DES_C		 is null then B.ATC3_DES_C 	    else A.ATC3_DES_C		end  AS ATC3_DES_C	      	 
	,case when A.ATC4_DES		='' or A.ATC4_DES		 is null then B.ATC4_DES	    else A.ATC4_DES			end  AS ATC4_DES		     
	,case when A.ATC4_DES_C		='' or A.ATC4_DES_C		 is null then B.ATC4_DES_C	    else A.ATC4_DES_C		end  AS ATC4_DES_C	      	 
	,case when A.APP1_DES		='' or A.APP1_DES		 is null then B.APP1_DES	    else A.APP1_DES			end  AS APP1_DES		     
	,case when A.APP1_DES_C		='' or A.APP1_DES_C		 is null then B.APP1_DES_C	    else A.APP1_DES_C		end  AS APP1_DES_C	      	 
	,case when A.APP2_DES		='' or A.APP2_DES		 is null then B.APP2_DES 	    else A.APP2_DES			end  AS APP2_DES		     
	,case when A.APP2_DES_C		='' or A.APP2_DES_C		 is null then B.APP2_DES_C	    else A.APP2_DES_C		end  AS APP2_DES_C	      	 
	,case when A.APP3_DES		='' or A.APP3_DES		 is null then B.APP3_DES	    else A.APP3_DES		 	end  AS APP3_DES			     
	,case when A.APP3_DES_C		='' or A.APP3_DES_C		 is null then B.APP3_DES_C	    else A.APP3_DES_C		end  AS APP3_DES_C	     	 
	,case when A.GEN_RN			='' or A.GEN_RN			 is null then B.GEN_RN          else A.GEN_RN			end  AS GEN_RN  
FROM dm.dm_td_external_packinfo_temp a 
LEFT JOIN temp B
ON B.PACK_COD = A.PACK_COD  ;

insert overwrite dm.dm_td_external_packinfo_temp(
  PACK_COD ,PACK_DES,STGH_DES,PACK_LCH,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,IS_AZ,AZ_MAIN,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,GEN_RN
)
select * from tmp.tmp_county_dm_td_external_packinfo

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

-------------------------------------------------------------补充THC部分缺失的pack 维度--------------------------------------------------------------------------------------------
--01 根据主表数据(带多个source)关联chpa的pack_porperty表,取出相关维度
--02 按照步骤1，chpa中没有的packcode，他的维度字段会被置为空，所以要关联我们自己处理好的维度表，来补充相关维度
insert overwrite table tmp.tmp_thc_dm_td_external_packinfo 
with temp as (    -- 临时表补充维度
  SELECT 
    iqvia_pack_code PACK_COD,
    MAX(B.PACK_DES) PACK_DES,
    MAX(B.STGH_DES) STGH_DES,
    MAX(B.PACK_LCH) PACK_LCH,
    MAX(B.iqvia_prod_code) PROD_COD,
    MAX(B.PROD_DES) PROD_DES,
    MAX(B.PROD_DES_C) PROD_DES_C,
    MAX(B.CMPS_COD) CMPS_COD,
    MAX(B.CMPS_DES) CMPS_DES,
    MAX(B.CMPS_DES_C) CMPS_DES_C,
    MAX(B.ATC1_COD) ATC1_COD,
    MAX(B.ATC2_COD) ATC2_COD,
    MAX(B.ATC3_COD) ATC3_COD,
    MAX(B.ATC4_COD) ATC4_COD,
    MAX(B.APP1_COD) APP1_COD,
    MAX(B.APP2_COD) APP2_COD,
    MAX(B.APP3_COD) APP3_COD,
    MAX(B.BIO_DESC) BIO_DESC,
    MAX(B.GENE_ORIG_DESC) GENE_ORIG_DESC,
    MAX(B.ETH_OTC_DESC) ETH_OTC_DESC,
    MAX(B.NRDL_DESC) NRDL_DESC,
    null as  NRDL_ENTRY_DATE,
    MAX(B.EDL_DESC) EDL_DESC,
    MAX(B.TCM_DESC) TCM_DESC,
    MAX(B.PAED_DESC) PAED_DESC,
    MAX(B.GQCE_DESC) GQCE_DESC,
    NULL AS VBP_DESC,
    MAX(B.MANU_COD) MANU_COD,
    MAX(B.MANU_DES) MANU_DES,
    MAX(B.MANU_DES_C) MANU_DES_C,
    MAX(B.MNFL_COD) MNFL_COD,
    MAX(B.MNFL_DES) MNFL_DES,
    max(B.CORP_COD) as  CORP_COD,
    max(B.CORP_DES) as  CORP_DES,
    --coalesce(MAX(C.CORP_COD) ,MAX(B.CORP_COD) ) CORP_COD,     --  THC存在CORP_COD 归属不一致问题 
    --REPLACE( coalesce( MAX(C.CORP_DES)   ,MAX(B.CORP_DES)  )  , ' GROUP', '' )  as CORP_DES,
    MAX(B.CORP_DES_C) CORP_DES_C,
    null as BRANDTYPE,
    null as IS_AZ,
    null as AZ_MAIN,
    MAX(B.ATC1_DES) ATC1_DES,
    MAX(B.ATC1_DES_C) ATC1_DES_C,
    MAX(B.ATC2_DES) ATC2_DES,
    MAX(B.ATC2_DES_C) ATC2_DES_C,
    MAX(B.ATC3_DES) ATC3_DES,
    MAX(B.ATC3_DES_C) ATC3_DES_C,
    MAX(B.ATC4_DES) ATC4_DES,
    MAX(B.ATC4_DES_C) ATC4_DES_C,
    MAX(B.APP1_DES) APP1_DES,
    MAX(B.APP1_DES_C) APP1_DES_C,
    MAX(B.APP2_DES) APP2_DES,
    MAX(B.APP2_DES_C) APP2_DES_C,
    MAX(B.APP3_DES) APP3_DES,
    MAX(B.APP3_DES_C) APP3_DES_C,
    CASE WHEN MAX(B.GENE_ORIG_DESC) = 'ORIG' THEN 1 
      WHEN MAX(B.GENE_ORIG_DESC) = 'Branded Gen' THEN 2
      WHEN MAX(B.GENE_ORIG_DESC) = 'Unbranded Gen' THEN 3
      WHEN MAX(B.GENE_ORIG_DESC) = 'TCM' THEN 4
      ELSE 5 
END GEN_RN
FROM  dwd.dwd_gnd_ext_thc_pack_property B 
  GROUP BY B.iqvia_pack_code 
)
select
  A.PACK_COD
	,case when A.PACK_DES	='' or  A.PACK_DES	     is null then B.PACK_DES 	else  A.PACK_DES				end  AS PACK_DES
	,case when A.STGH_DES	='' or  A.STGH_DES	     is null then B.STGH_DES 	else  A.STGH_DES				end  AS STGH_DES
	,case when A.PACK_LCH	='' or  A.PACK_LCH	     is null then B.PACK_LCH 	else  A.PACK_LCH				end  AS PACK_LCH
	,case when A.PROD_COD	='' or  A.PROD_COD	     is null then B.PROD_COD 	else  A.PROD_COD				end  AS PROD_COD
	,case when A.PROD_DES	='' or  A.PROD_DES	     is null then B.PROD_DES 	else  A.PROD_DES				end  AS PROD_DES
	,case when A.PROD_DES_C	='' or  A.PROD_DES_C	 is null then B.PROD_DES_C	else  A.PROD_DES_C				end  AS PROD_DES_C
	,case when A.CMPS_COD	='' or  A.CMPS_COD	     is null then B.CMPS_COD	else  A.CMPS_COD				end  AS CMPS_COD
	,case when A.CMPS_DES	='' or  A.CMPS_DES	     is null then B.CMPS_DES	else  A.CMPS_DES				end  AS CMPS_DES
	,case when A.CMPS_DES_C	='' or  A.CMPS_DES_C	 is null then B.CMPS_DES_C	else  A.CMPS_DES_C				end  AS CMPS_DES_C
	,case when A.ATC1_COD	='' or  A.ATC1_COD	     is null then B.ATC1_COD    else  A.ATC1_COD				end  AS ATC1_COD
	,case when A.ATC2_COD	='' or  A.ATC2_COD	     is null then B.ATC2_COD    else  A.ATC2_COD				end  AS ATC2_COD
	,case when A.ATC3_COD	='' or  A.ATC3_COD	     is null then B.ATC3_COD    else  A.ATC3_COD				end  AS ATC3_COD
	,case when A.ATC4_COD	='' or  A.ATC4_COD	     is null then B.ATC4_COD    else  A.ATC4_COD				end  AS ATC4_COD
	,case when A.APP1_COD	='' or  A.APP1_COD	     is null then B.APP1_COD    else  A.APP1_COD				end  AS APP1_COD
	,case when A.APP2_COD	='' or  A.APP2_COD	     is null then B.APP2_COD    else  A.APP2_COD				end  AS APP2_COD
	,case when A.APP3_COD	='' or  A.APP3_COD	     is null then B.APP3_COD    else  A.APP3_COD				end  AS APP3_COD
	,case when A.BIO_DESC	='' or  A.BIO_DESC	     is null then B.BIO_DESC    else  A.BIO_DESC				end  AS BIO_DESC
	,case when A.GENE_ORIG_DESC	='' or A.GENE_ORIG_DESC	 is null then B.GENE_ORIG_DESC	else A.GENE_ORIG_DESC	end	 AS GENE_ORIG_DESC	 
	,case when A.ETH_OTC_DESC	='' or A.ETH_OTC_DESC	 is null then B.ETH_OTC_DESC	else A.ETH_OTC_DESC	    end	 AS ETH_OTC_DESC	 
	,case when A.NRDL_DESC		='' or A.NRDL_DESC		 is null then B.NRDL_DESC		else A.NRDL_DESC		end  AS NRDL_DESC    	 		 
	,case when A.NRDL_ENTRY_DATE='' or A.NRDL_ENTRY_DATE is null then B.NRDL_ENTRY_DATE else A.NRDL_ENTRY_DATE	end  AS NRDL_ENTRY_DATE    
	,case when A.EDL_DESC		='' or A.EDL_DESC		 is null then B.EDL_DESC		else A.EDL_DESC		    end  AS EDL_DESC          
	,case when A.TCM_DESC		='' or A.TCM_DESC		 is null then B.TCM_DESC		else A.TCM_DESC		    end  AS TCM_DESC          
	,case when A.PAED_DESC		='' or A.PAED_DESC		 is null then B.PAED_DESC		else A.PAED_DESC		end  AS PAED_DESC     	     
	,case when A.GQCE_DESC		='' or A.GQCE_DESC		 is null then B.GQCE_DESC		else A.GQCE_DESC		end  AS GQCE_DESC     	     
	,case when A.VBP_DESC		='' or A.VBP_DESC		 is null then B.VBP_DESC		else A.VBP_DESC			end	 AS VBP_DESC     
	,case when A.MANU_COD		='' or A.MANU_COD		 is null then B.MANU_COD		else A.MANU_COD			end	 AS MANU_COD     
	,case when A.MANU_DES		='' or A.MANU_DES		 is null then B.MANU_DES		else A.MANU_DES			end	 AS MANU_DES     
	,case when A.MANU_DES_C		='' or A.MANU_DES_C		 is null then B.MANU_DES_C		else A.MANU_DES_C		end  AS MANU_DES_C    
	,case when A.MNFL_COD		='' or A.MNFL_COD		 is null then B.MNFL_COD		else A.MNFL_COD			end	 AS MNFL_COD     
	,case when A.MNFL_DES		='' or A.MNFL_DES		 is null then B.MNFL_DES		else A.MNFL_DES			end	 AS MNFL_DES     
	,case when A.CORP_COD		='' or A.CORP_COD		 is null then B.CORP_COD		else A.CORP_COD			end	 AS CORP_COD    --  THC存在CORP_COD 归属不一致问题      
  ,REPLACE( case when A.CORP_DES ='' or A.CORP_DES is null then B.CORP_DES else A.CORP_DES end ,' GROUP', '' )       as CORP_DES   -- 消除group
	,case when A.CORP_DES_C		='' or A.CORP_DES_C		 is null then B.CORP_DES_C	    else A.CORP_DES_C		end  AS CORP_DES_C		     
	,case when A.BRANDTYPE		='' or A.BRANDTYPE		 is null then B.BRANDTYPE 	    else A.BRANDTYPE		end  AS BRANDTYPE			     
	,case when A.IS_AZ			='' or A.IS_AZ			 is null then B.IS_AZ		    else A.IS_AZ			end  AS IS_AZ				     
	,case when A.AZ_MAIN		='' or A.AZ_MAIN		 is null then B.AZ_MAIN		    else A.AZ_MAIN			end  AS AZ_MAIN		     
	,case when A.ATC1_DES		='' or A.ATC1_DES		 is null then B.ATC1_DES	    else A.ATC1_DES			end  AS ATC1_DES		     
	,case when A.ATC1_DES_C		='' or A.ATC1_DES_C		 is null then B.ATC1_DES_C 	    else A.ATC1_DES_C		end  AS ATC1_DES_C	         
	,case when A.ATC2_DES		='' or A.ATC2_DES		 is null then B.ATC2_DES 	    else A.ATC2_DES			end  AS ATC2_DES		     
	,case when A.ATC2_DES_C		='' or A.ATC2_DES_C		 is null then B.ATC2_DES_C	    else A.ATC2_DES_C		end  AS ATC2_DES_C	         
	,case when A.ATC3_DES		='' or A.ATC3_DES		 is null then B.ATC3_DES	    else A.ATC3_DES			end  AS ATC3_DES		     
	,case when A.ATC3_DES_C		='' or A.ATC3_DES_C		 is null then B.ATC3_DES_C 	    else A.ATC3_DES_C		end  AS ATC3_DES_C	      	 
	,case when A.ATC4_DES		='' or A.ATC4_DES		 is null then B.ATC4_DES	    else A.ATC4_DES			end  AS ATC4_DES		     
	,case when A.ATC4_DES_C		='' or A.ATC4_DES_C		 is null then B.ATC4_DES_C	    else A.ATC4_DES_C		end  AS ATC4_DES_C	      	 
	,case when A.APP1_DES		='' or A.APP1_DES		 is null then B.APP1_DES	    else A.APP1_DES			end  AS APP1_DES		     
	,case when A.APP1_DES_C		='' or A.APP1_DES_C		 is null then B.APP1_DES_C	    else A.APP1_DES_C		end  AS APP1_DES_C	      	 
	,case when A.APP2_DES		='' or A.APP2_DES		 is null then B.APP2_DES 	    else A.APP2_DES			end  AS APP2_DES		     
	,case when A.APP2_DES_C		='' or A.APP2_DES_C		 is null then B.APP2_DES_C	    else A.APP2_DES_C		end  AS APP2_DES_C	      	 
	,case when A.APP3_DES		='' or A.APP3_DES		 is null then B.APP3_DES	    else A.APP3_DES		 	end  AS APP3_DES			     
	,case when A.APP3_DES_C		='' or A.APP3_DES_C		 is null then B.APP3_DES_C	    else A.APP3_DES_C		end  AS APP3_DES_C	     	 
	,case when A.GEN_RN			='' or A.GEN_RN			 is null then B.GEN_RN          else A.GEN_RN			end  AS GEN_RN  
FROM   dm.dm_td_external_packinfo_temp  a 
LEFT JOIN temp  B
ON B.PACK_COD = A.PACK_COD  ;

insert overwrite dm.dm_td_external_packinfo_temp(
  PACK_COD ,PACK_DES,STGH_DES,PACK_LCH,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,IS_AZ,AZ_MAIN,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,GEN_RN
)
select * from tmp.tmp_thc_dm_td_external_packinfo

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

-------------------------------------------------------------补充CHC部分缺失的维度数据--------------------------------------------------------------------------------------------
insert overwrite table tmp.tmp_chc_dm_td_external_packinfo 
with temp as (                    -- dm.dm_td_external_packinfo_temp 该表数据 
                                  --01首先是根据主表数据(带多个source)关联chpa的pack_porperty表,取出相关维度
                                  --02chpa中,没有的code，他的维度字段会被置为空，所以要关联我们自己处理好的维度表，来补充相关维度
  SELECT                         
    iqvia_pack_code PACK_COD,
    MAX(B.PACK_DES) PACK_DES,
    MAX(B.STGH_DES) STGH_DES,
    MAX(B.PACK_LCH) PACK_LCH,
    MAX(B.iqvia_prod_code) PROD_COD,
    MAX(B.PROD_DES) PROD_DES,
    MAX(B.PROD_DES_C) PROD_DES_C,
    MAX(B.CMPS_COD) CMPS_COD,
    MAX(B.CMPS_DES) CMPS_DES,
    MAX(B.CMPS_DES_C) CMPS_DES_C,
    MAX(B.ATC1_COD) ATC1_COD,
    MAX(B.ATC2_COD) ATC2_COD,
    MAX(B.ATC3_COD) ATC3_COD,
    MAX(B.ATC4_COD) ATC4_COD,
    MAX(B.APP1_COD) APP1_COD,
    MAX(B.APP2_COD) APP2_COD,
    MAX(B.APP3_COD) APP3_COD,
    MAX(B.BIO_DESC) BIO_DESC,
    MAX(B.GENE_ORIG_DESC) GENE_ORIG_DESC,
    MAX(B.ETH_OTC_DESC) ETH_OTC_DESC,
    MAX(B.NRDL_DESC) NRDL_DESC,
    null as  NRDL_ENTRY_DATE,
    MAX(B.EDL_DESC) EDL_DESC,
    MAX(B.TCM_DESC) TCM_DESC,
    MAX(B.PAED_DESC) PAED_DESC,
    MAX(B.GQCE_DESC) GQCE_DESC,
    NULL AS VBP_DESC,
    MAX(B.MANU_COD) MANU_COD,
    MAX(B.MANU_DES) MANU_DES,
    MAX(B.MANU_DES_C) MANU_DES_C,
    MAX(B.MNFL_COD) MNFL_COD,
    MAX(B.MNFL_DES) MNFL_DES,
    max(B.CORP_COD) as  CORP_COD,
    max(B.CORP_DES) as  CORP_DES,
    -- coalesce(MAX(C.CORP_COD) ,MAX(B.CORP_COD) ) CORP_COD,     --  THC存在CORP_COD 归属不一致问题 
    -- REPLACE( coalesce( MAX(C.CORP_DES)   ,MAX(B.CORP_DES)  )  , ' GROUP', '' )  as CORP_DES,
    MAX(B.CORP_DES_C) CORP_DES_C,
    null as BRANDTYPE,
    null as IS_AZ,
    null as AZ_MAIN,
    MAX(B.ATC1_DES) ATC1_DES,
    MAX(B.ATC1_DES_C) ATC1_DES_C,
    MAX(B.ATC2_DES) ATC2_DES,
    MAX(B.ATC2_DES_C) ATC2_DES_C,
    MAX(B.ATC3_DES) ATC3_DES,
    MAX(B.ATC3_DES_C) ATC3_DES_C,
    MAX(B.ATC4_DES) ATC4_DES,
    MAX(B.ATC4_DES_C) ATC4_DES_C,
    MAX(B.APP1_DES) APP1_DES,
    MAX(B.APP1_DES_C) APP1_DES_C,
    MAX(B.APP2_DES) APP2_DES,
    MAX(B.APP2_DES_C) APP2_DES_C,
    MAX(B.APP3_DES) APP3_DES,
    MAX(B.APP3_DES_C) APP3_DES_C,
    CASE WHEN MAX(B.GENE_ORIG_DESC) = 'ORIG' THEN 1 
      WHEN MAX(B.GENE_ORIG_DESC) = 'Branded Gen' THEN 2
      WHEN MAX(B.GENE_ORIG_DESC) = 'Unbranded Gen' THEN 3
      WHEN MAX(B.GENE_ORIG_DESC) = 'TCM' THEN 4
      ELSE 5 
END GEN_RN
FROM  dwd.dwd_gnd_ext_chc_pack_property B 
GROUP BY B.iqvia_pack_code 
)
select
  A.PACK_COD
	,case when A.PACK_DES	='' or  A.PACK_DES	     is null then B.PACK_DES 	else  A.PACK_DES				end  AS PACK_DES
	,case when A.STGH_DES	='' or  A.STGH_DES	     is null then B.STGH_DES 	else  A.STGH_DES				end  AS STGH_DES
	,case when A.PACK_LCH	='' or  A.PACK_LCH	     is null then B.PACK_LCH 	else  A.PACK_LCH				end  AS PACK_LCH
	,case when A.PROD_COD	='' or  A.PROD_COD	     is null then B.PROD_COD 	else  A.PROD_COD				end  AS PROD_COD
	,case when A.PROD_DES	='' or  A.PROD_DES	     is null then B.PROD_DES 	else  A.PROD_DES				end  AS PROD_DES
	,case when A.PROD_DES_C	='' or  A.PROD_DES_C	 is null then B.PROD_DES_C	else  A.PROD_DES_C				end  AS PROD_DES_C
	,case when A.CMPS_COD	='' or  A.CMPS_COD	     is null then B.CMPS_COD	else  A.CMPS_COD				end  AS CMPS_COD
	,case when A.CMPS_DES	='' or  A.CMPS_DES	     is null then B.CMPS_DES	else  A.CMPS_DES				end  AS CMPS_DES
	,case when A.CMPS_DES_C	='' or  A.CMPS_DES_C	 is null then B.CMPS_DES_C	else  A.CMPS_DES_C				end  AS CMPS_DES_C
	,case when A.ATC1_COD	='' or  A.ATC1_COD	     is null then B.ATC1_COD    else  A.ATC1_COD				end  AS ATC1_COD
	,case when A.ATC2_COD	='' or  A.ATC2_COD	     is null then B.ATC2_COD    else  A.ATC2_COD				end  AS ATC2_COD
	,case when A.ATC3_COD	='' or  A.ATC3_COD	     is null then B.ATC3_COD    else  A.ATC3_COD				end  AS ATC3_COD
	,case when A.ATC4_COD	='' or  A.ATC4_COD	     is null then B.ATC4_COD    else  A.ATC4_COD				end  AS ATC4_COD
	,case when A.APP1_COD	='' or  A.APP1_COD	     is null then B.APP1_COD    else  A.APP1_COD				end  AS APP1_COD
	,case when A.APP2_COD	='' or  A.APP2_COD	     is null then B.APP2_COD    else  A.APP2_COD				end  AS APP2_COD
	,case when A.APP3_COD	='' or  A.APP3_COD	     is null then B.APP3_COD    else  A.APP3_COD				end  AS APP3_COD
	,case when A.BIO_DESC	='' or  A.BIO_DESC	     is null then B.BIO_DESC    else  A.BIO_DESC				end  AS BIO_DESC
	,case when A.GENE_ORIG_DESC	='' or A.GENE_ORIG_DESC	 is null then B.GENE_ORIG_DESC	else A.GENE_ORIG_DESC	end	 AS GENE_ORIG_DESC	 
	,case when A.ETH_OTC_DESC	='' or A.ETH_OTC_DESC	 is null then B.ETH_OTC_DESC	else A.ETH_OTC_DESC	    end	 AS ETH_OTC_DESC	 
	,case when A.NRDL_DESC		='' or A.NRDL_DESC		 is null then B.NRDL_DESC		else A.NRDL_DESC		end  AS NRDL_DESC    	 		 
	,case when A.NRDL_ENTRY_DATE='' or A.NRDL_ENTRY_DATE is null then B.NRDL_ENTRY_DATE else A.NRDL_ENTRY_DATE	end  AS NRDL_ENTRY_DATE    
	,case when A.EDL_DESC		='' or A.EDL_DESC		 is null then B.EDL_DESC		else A.EDL_DESC		    end  AS EDL_DESC          
	,case when A.TCM_DESC		='' or A.TCM_DESC		 is null then B.TCM_DESC		else A.TCM_DESC		    end  AS TCM_DESC          
	,case when A.PAED_DESC		='' or A.PAED_DESC		 is null then B.PAED_DESC		else A.PAED_DESC		end  AS PAED_DESC     	     
	,case when A.GQCE_DESC		='' or A.GQCE_DESC		 is null then B.GQCE_DESC		else A.GQCE_DESC		end  AS GQCE_DESC     	     
	,case when A.VBP_DESC		='' or A.VBP_DESC		 is null then B.VBP_DESC		else A.VBP_DESC			end	 AS VBP_DESC     
	,case when A.MANU_COD		='' or A.MANU_COD		 is null then B.MANU_COD		else A.MANU_COD			end	 AS MANU_COD     
	,case when A.MANU_DES		='' or A.MANU_DES		 is null then B.MANU_DES		else A.MANU_DES			end	 AS MANU_DES     
	,case when A.MANU_DES_C		='' or A.MANU_DES_C		 is null then B.MANU_DES_C		else A.MANU_DES_C		end  AS MANU_DES_C    
	,case when A.MNFL_COD		='' or A.MNFL_COD		 is null then B.MNFL_COD		else A.MNFL_COD			end	 AS MNFL_COD     
	,case when A.MNFL_DES		='' or A.MNFL_DES		 is null then B.MNFL_DES		else A.MNFL_DES			end	 AS MNFL_DES     
	,case when A.CORP_COD		='' or A.CORP_COD		 is null then B.CORP_COD		else A.CORP_COD			end	 AS CORP_COD    --  THC存在CORP_COD 归属不一致问题      
  ,REPLACE( case when A.CORP_DES ='' or A.CORP_DES is null then B.CORP_DES else A.CORP_DES end ,' GROUP', '' )       as CORP_DES   -- 消除group
	,case when A.CORP_DES_C		='' or A.CORP_DES_C		 is null then B.CORP_DES_C	    else A.CORP_DES_C		end  AS CORP_DES_C		     
	,case when A.BRANDTYPE		='' or A.BRANDTYPE		 is null then B.BRANDTYPE 	    else A.BRANDTYPE		end  AS BRANDTYPE			     
	,case when A.IS_AZ			='' or A.IS_AZ			 is null then B.IS_AZ		    else A.IS_AZ			end  AS IS_AZ				     
	,case when A.AZ_MAIN		='' or A.AZ_MAIN		 is null then B.AZ_MAIN		    else A.AZ_MAIN			end  AS AZ_MAIN		     
	,case when A.ATC1_DES		='' or A.ATC1_DES		 is null then B.ATC1_DES	    else A.ATC1_DES			end  AS ATC1_DES		     
	,case when A.ATC1_DES_C		='' or A.ATC1_DES_C		 is null then B.ATC1_DES_C 	    else A.ATC1_DES_C		end  AS ATC1_DES_C	         
	,case when A.ATC2_DES		='' or A.ATC2_DES		 is null then B.ATC2_DES 	    else A.ATC2_DES			end  AS ATC2_DES		     
	,case when A.ATC2_DES_C		='' or A.ATC2_DES_C		 is null then B.ATC2_DES_C	    else A.ATC2_DES_C		end  AS ATC2_DES_C	         
	,case when A.ATC3_DES		='' or A.ATC3_DES		 is null then B.ATC3_DES	    else A.ATC3_DES			end  AS ATC3_DES		     
	,case when A.ATC3_DES_C		='' or A.ATC3_DES_C		 is null then B.ATC3_DES_C 	    else A.ATC3_DES_C		end  AS ATC3_DES_C	      	 
	,case when A.ATC4_DES		='' or A.ATC4_DES		 is null then B.ATC4_DES	    else A.ATC4_DES			end  AS ATC4_DES		     
	,case when A.ATC4_DES_C		='' or A.ATC4_DES_C		 is null then B.ATC4_DES_C	    else A.ATC4_DES_C		end  AS ATC4_DES_C	      	 
	,case when A.APP1_DES		='' or A.APP1_DES		 is null then B.APP1_DES	    else A.APP1_DES			end  AS APP1_DES		     
	,case when A.APP1_DES_C		='' or A.APP1_DES_C		 is null then B.APP1_DES_C	    else A.APP1_DES_C		end  AS APP1_DES_C	      	 
	,case when A.APP2_DES		='' or A.APP2_DES		 is null then B.APP2_DES 	    else A.APP2_DES			end  AS APP2_DES		     
	,case when A.APP2_DES_C		='' or A.APP2_DES_C		 is null then B.APP2_DES_C	    else A.APP2_DES_C		end  AS APP2_DES_C	      	 
	,case when A.APP3_DES		='' or A.APP3_DES		 is null then B.APP3_DES	    else A.APP3_DES		 	end  AS APP3_DES			     
	,case when A.APP3_DES_C		='' or A.APP3_DES_C		 is null then B.APP3_DES_C	    else A.APP3_DES_C		end  AS APP3_DES_C	     	 
	,case when A.GEN_RN			='' or A.GEN_RN			 is null then B.GEN_RN          else A.GEN_RN			end  AS GEN_RN  
FROM dm.dm_td_external_packinfo_temp  a 
LEFT JOIN temp B
ON B.PACK_COD = A.PACK_COD  ;

insert overwrite dm.dm_td_external_packinfo_temp(
  PACK_COD ,PACK_DES,STGH_DES,PACK_LCH,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,IS_AZ,AZ_MAIN,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,GEN_RN
)
select * from tmp.tmp_chc_dm_td_external_packinfo 



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

-----------------------------------------------retail 补齐 retail 缺失部分pack 维度------------------------------------------------
-------------将补全数据存入临时表，再写入目标表格----------------
insert overwrite table tmp.tmp_retail_dm_td_external_packinfo 
WITH dm_retail_pack AS (
  SELECT 
iqvia_pack_code PACK_COD,MAX(B.PACK_DES) PACK_DES,MAX(B.STGH_DES) STGH_DES,MAX(B.PACK_LCH) PACK_LCH,MAX(B.iqvia_prod_code) PROD_COD,MAX(B.PROD_DES) PROD_DES,MAX(B.PROD_DES_C) PROD_DES_C,MAX(B.CMPS_COD) CMPS_COD,
MAX(B.CMPS_DES) CMPS_DES,MAX(B.CMPS_DES_C) CMPS_DES_C,MAX(B.ATC1_COD) ATC1_COD,MAX(B.ATC2_COD) ATC2_COD,MAX(B.ATC3_COD) ATC3_COD,MAX(B.ATC4_COD) ATC4_COD,MAX(B.APP1_COD) APP1_COD,MAX(B.APP2_COD) APP2_COD,
MAX(B.APP3_COD) APP3_COD,MAX(B.BIO_DESC) BIO_DESC,MAX(B.GENE_ORIG_DESC) GENE_ORIG_DESC,MAX(B.ETH_OTC_DESC) ETH_OTC_DESC,MAX(B.NRDL_DESC) NRDL_DESC,MAX(B.NRDL_ENTRY_DATE) NRDL_ENTRY_DATE,MAX(B.EDL_DESC) EDL_DESC,
MAX(B.TCM_DESC) TCM_DESC,MAX(B.PAED_DESC) PAED_DESC,MAX(B.GQCE_DESC) GQCE_DESC,
case when MAX(B.VBP_DESC) = 'VBP-IN' then 'VBP-IN' when MAX(B.VBP_DESC) LIKE '%VBP%' THEN 'VBP-OUT' WHEN MAX(B.VBP_DESC) IS NOT NULL THEN 'Non VBP' ELSE NULL end as VBP_DESC,
MAX(B.MANU_COD) MANU_COD,MAX(B.MANU_DES) MANU_DES,MAX(B.MANU_DES_C) MANU_DES_C,MAX(B.MNFL_COD) MNFL_COD,
MAX(B.MNFL_DES) MNFL_DES,MAX(B.CORP_COD) CORP_COD,MAX(B.CORP_DES) CORP_DES,MAX(B.CORP_DES_C) CORP_DES_C,MAX(B.BRANDTYPE) BRANDTYPE,MAX(B.IS_AZ) IS_AZ,MAX(B.AZ_MAIN) AZ_MAIN,MAX(B.ATC1_DES) ATC1_DES,
MAX(B.ATC1_DES_C) ATC1_DES_C,MAX(B.ATC2_DES) ATC2_DES,MAX(B.ATC2_DES_C) ATC2_DES_C,MAX(B.ATC3_DES) ATC3_DES,MAX(B.ATC3_DES_C) ATC3_DES_C,MAX(B.ATC4_DES) ATC4_DES,MAX(B.ATC4_DES_C) ATC4_DES_C,MAX(B.APP1_DES) APP1_DES,
MAX(B.APP1_DES_C) APP1_DES_C,MAX(B.APP2_DES) APP2_DES,MAX(B.APP2_DES_C) APP2_DES_C,MAX(B.APP3_DES) APP3_DES,MAX(B.APP3_DES_C) APP3_DES_C,
CASE WHEN MAX(B.GENE_ORIG_DESC) = 'ORIG' THEN 1 WHEN MAX(B.GENE_ORIG_DESC) = 'Branded Gen' THEN 2 WHEN MAX(B.GENE_ORIG_DESC) = 'Unbranded Gen' THEN 3 WHEN MAX(B.GENE_ORIG_DESC) = 'TCM' THEN 4 ELSE 5 END GEN_RN
FROM dm.dm_zk_retail_pack_property  B 
GROUP BY B.iqvia_pack_code
union  
  SELECT 
B.iqvia_pack_code PACK_COD,MAX(B.PACK_DES) PACK_DES,MAX(B.STGH_DES) STGH_DES,MAX(B.PACK_LCH) PACK_LCH,MAX(B.PROD_COD) PROD_COD,MAX(B.PROD_DES) PROD_DES,MAX(B.PROD_DES_C) PROD_DES_C,MAX(B.CMPS_COD) CMPS_COD,
MAX(B.CMPS_DES) CMPS_DES,MAX(B.CMPS_DES_C) CMPS_DES_C,MAX(B.ATC1_COD) ATC1_COD,MAX(B.ATC2_COD) ATC2_COD,MAX(B.ATC3_COD) ATC3_COD,MAX(B.ATC4_COD) ATC4_COD,MAX(B.APP1_COD) APP1_COD,MAX(B.APP2_COD) APP2_COD,
MAX(B.APP3_COD) APP3_COD,MAX(B.BIO_DESC) BIO_DESC,MAX(B.GENE_ORIG_DESC) GENE_ORIG_DESC,MAX(B.ETH_OTC_DESC) ETH_OTC_DESC,MAX(B.NRDL_DESC) NRDL_DESC,MAX(B.NRDL_ENTRY_DATE) NRDL_ENTRY_DATE,MAX(B.EDL_DESC) EDL_DESC,
MAX(B.TCM_DESC) TCM_DESC,MAX(B.PAED_DESC) PAED_DESC,MAX(B.GQCE_DESC) GQCE_DESC,
case when MAX(B.VBP_DESC) = 'VBP-IN' then 'VBP-IN' when MAX(B.VBP_DESC) LIKE '%VBP%' THEN 'VBP-OUT' WHEN MAX(B.VBP_DESC) IS NOT NULL THEN 'Non VBP' ELSE NULL end as VBP_DESC,
MAX(B.MANU_COD) MANU_COD,MAX(B.MANU_DES) MANU_DES,MAX(B.MANU_DES_C) MANU_DES_C,MAX(B.MNFL_COD) MNFL_COD,
MAX(B.MNFL_DES) MNFL_DES,MAX(B.CORP_COD) CORP_COD,MAX(B.CORP_DES) CORP_DES,MAX(B.CORP_DES_C) CORP_DES_C,MAX(B.BRANDTYPE) BRANDTYPE,MAX(B.IS_AZ) IS_AZ,MAX(B.AZ_MAIN) AZ_MAIN,MAX(B.ATC1_DES) ATC1_DES,
MAX(B.ATC1_DES_C) ATC1_DES_C,MAX(B.ATC2_DES) ATC2_DES,MAX(B.ATC2_DES_C) ATC2_DES_C,MAX(B.ATC3_DES) ATC3_DES,MAX(B.ATC3_DES_C) ATC3_DES_C,MAX(B.ATC4_DES) ATC4_DES,MAX(B.ATC4_DES_C) ATC4_DES_C,MAX(B.APP1_DES) APP1_DES,
MAX(B.APP1_DES_C) APP1_DES_C,MAX(B.APP2_DES) APP2_DES,MAX(B.APP2_DES_C) APP2_DES_C,MAX(B.APP3_DES) APP3_DES,MAX(B.APP3_DES_C) APP3_DES_C,
CASE WHEN MAX(B.GENE_ORIG_DESC) = 'ORIG' THEN 1 WHEN MAX(B.GENE_ORIG_DESC) = 'Branded Gen' THEN 2 WHEN MAX(B.GENE_ORIG_DESC) = 'Unbranded Gen' THEN 3 WHEN MAX(B.GENE_ORIG_DESC) = 'TCM' THEN 4 ELSE 5 END GEN_RN
FROM dm.dm_zk_retail_dtp_pack_property  B 
GROUP BY B.iqvia_pack_code
)
SELECT 
  A.PACK_COD
	,case when A.PACK_DES	='' or  A.PACK_DES	     is null then B.PACK_DES 	else  A.PACK_DES				end  AS PACK_DES
	,case when A.STGH_DES	='' or  A.STGH_DES	     is null then B.STGH_DES 	else  A.STGH_DES				end  AS STGH_DES
	,case when A.PACK_LCH	='' or  A.PACK_LCH	     is null then B.PACK_LCH 	else  A.PACK_LCH				end  AS PACK_LCH
	,case when A.PROD_COD	='' or  A.PROD_COD	     is null then B.PROD_COD 	else  A.PROD_COD				end  AS PROD_COD
	,case when A.PROD_DES	='' or  A.PROD_DES	     is null then B.PROD_DES  else A.PROD_DES         end  AS PROD_DES
	,case when A.PROD_DES_C	='' or  A.PROD_DES_C	 is null then B.PROD_DES_C else  A.PROD_DES_C			end  AS PROD_DES_C	      
	,case when A.CMPS_COD	='' or  A.CMPS_COD	     is null then B.CMPS_COD	else  A.CMPS_COD				end  AS CMPS_COD
	,case when A.CMPS_DES	='' or  A.CMPS_DES	     is null then B.CMPS_DES	else  A.CMPS_DES				end  AS CMPS_DES
	,case when A.CMPS_DES_C	='' or  A.CMPS_DES_C	 is null then B.CMPS_DES_C	else  A.CMPS_DES_C				end  AS CMPS_DES_C
	,case when A.ATC1_COD	='' or  A.ATC1_COD	     is null then B.ATC1_COD    else  A.ATC1_COD				end  AS ATC1_COD
	,case when A.ATC2_COD	='' or  A.ATC2_COD	     is null then B.ATC2_COD    else  A.ATC2_COD				end  AS ATC2_COD
	,case when A.ATC3_COD	='' or  A.ATC3_COD	     is null then B.ATC3_COD    else  A.ATC3_COD				end  AS ATC3_COD
	,case when A.ATC4_COD	='' or  A.ATC4_COD	     is null then B.ATC4_COD    else  A.ATC4_COD				end  AS ATC4_COD
	,case when A.APP1_COD	='' or  A.APP1_COD	     is null then B.APP1_COD    else  A.APP1_COD				end  AS APP1_COD
	,case when A.APP2_COD	='' or  A.APP2_COD	     is null then B.APP2_COD    else  A.APP2_COD				end  AS APP2_COD
	,case when A.APP3_COD	='' or  A.APP3_COD	     is null then B.APP3_COD    else  A.APP3_COD				end  AS APP3_COD
	,case when A.BIO_DESC	='' or  A.BIO_DESC	     is null then B.BIO_DESC    else  A.BIO_DESC				end  AS BIO_DESC
	,case when A.GENE_ORIG_DESC	='' or A.GENE_ORIG_DESC	 is null then B.GENE_ORIG_DESC	else A.GENE_ORIG_DESC	end	 AS GENE_ORIG_DESC	 
	,case when A.ETH_OTC_DESC	='' or A.ETH_OTC_DESC	 is null then B.ETH_OTC_DESC	else A.ETH_OTC_DESC	    end	 AS ETH_OTC_DESC	 
	,case when A.NRDL_DESC		='' or A.NRDL_DESC		 is null then B.NRDL_DESC		else A.NRDL_DESC		end  AS NRDL_DESC    	 		 
	,case when A.NRDL_ENTRY_DATE='' or A.NRDL_ENTRY_DATE is null then B.NRDL_ENTRY_DATE else A.NRDL_ENTRY_DATE	end  AS NRDL_ENTRY_DATE    
	,case when A.EDL_DESC		='' or A.EDL_DESC		 is null then B.EDL_DESC		else A.EDL_DESC		    end  AS EDL_DESC          
	,case when A.TCM_DESC		='' or A.TCM_DESC		 is null then B.TCM_DESC		else A.TCM_DESC		    end  AS TCM_DESC          
	,case when A.PAED_DESC		='' or A.PAED_DESC		 is null then B.PAED_DESC		else A.PAED_DESC		end  AS PAED_DESC     	     
	,case when A.GQCE_DESC		='' or A.GQCE_DESC		 is null then B.GQCE_DESC		else A.GQCE_DESC		end  AS GQCE_DESC     	     
	,case when A.VBP_DESC		='' or A.VBP_DESC		 is null then B.VBP_DESC		else A.VBP_DESC			end	 AS VBP_DESC     
	,case when A.MANU_COD		='' or A.MANU_COD		 is null then B.MANU_COD		else A.MANU_COD			end	 AS MANU_COD     
	,case when A.MANU_DES		='' or A.MANU_DES		 is null then B.MANU_DES		else A.MANU_DES			end	 AS MANU_DES     
	,case when A.MANU_DES_C		='' or A.MANU_DES_C		 is null then B.MANU_DES_C		else A.MANU_DES_C		end  AS MANU_DES_C    
	,case when A.MNFL_COD		='' or A.MNFL_COD		 is null then B.MNFL_COD		else A.MNFL_COD			end	 AS MNFL_COD     
	,case when A.MNFL_DES		='' or A.MNFL_DES		 is null then B.MNFL_DES		else A.MNFL_DES			end	 AS MNFL_DES     
	,case when A.CORP_COD		='' or A.CORP_COD		 is null then B.CORP_COD		else A.CORP_COD			end	 AS CORP_COD     
  ,REPLACE( case when A.CORP_DES ='' or A.CORP_DES is null then B.CORP_DES else A.CORP_DES end ,' GROUP', '' )       as CORP_DES   -- 消除group
	,case when A.CORP_DES_C		='' or A.CORP_DES_C		 is null then B.CORP_DES_C	    else A.CORP_DES_C		end  AS CORP_DES_C		     
	,case when A.BRANDTYPE		='' or A.BRANDTYPE		 is null then B.BRANDTYPE 	    else A.BRANDTYPE		end  AS BRANDTYPE			     
	,case when A.IS_AZ			='' or A.IS_AZ			 is null then B.IS_AZ		    else A.IS_AZ			end  AS IS_AZ				     
	,case when A.AZ_MAIN		='' or A.AZ_MAIN		 is null then B.AZ_MAIN		    else A.AZ_MAIN			end  AS AZ_MAIN		     
	,case when A.ATC1_DES		='' or A.ATC1_DES		 is null then B.ATC1_DES	    else A.ATC1_DES			end  AS ATC1_DES		     
	,case when A.ATC1_DES_C		='' or A.ATC1_DES_C		 is null then B.ATC1_DES_C 	    else A.ATC1_DES_C		end  AS ATC1_DES_C	         
	,case when A.ATC2_DES		='' or A.ATC2_DES		 is null then B.ATC2_DES 	    else A.ATC2_DES			end  AS ATC2_DES		     
	,case when A.ATC2_DES_C		='' or A.ATC2_DES_C		 is null then B.ATC2_DES_C	    else A.ATC2_DES_C		end  AS ATC2_DES_C	         
	,case when A.ATC3_DES		='' or A.ATC3_DES		 is null then B.ATC3_DES	    else A.ATC3_DES			end  AS ATC3_DES		     
	,case when A.ATC3_DES_C		='' or A.ATC3_DES_C		 is null then B.ATC3_DES_C 	    else A.ATC3_DES_C		end  AS ATC3_DES_C	      	 
	,case when A.ATC4_DES		='' or A.ATC4_DES		 is null then B.ATC4_DES	    else A.ATC4_DES			end  AS ATC4_DES		     
	,case when A.ATC4_DES_C		='' or A.ATC4_DES_C		 is null then B.ATC4_DES_C	    else A.ATC4_DES_C		end  AS ATC4_DES_C	      	 
	,case when A.APP1_DES		='' or A.APP1_DES		 is null then B.APP1_DES	    else A.APP1_DES			end  AS APP1_DES		     
	,case when A.APP1_DES_C		='' or A.APP1_DES_C		 is null then B.APP1_DES_C	    else A.APP1_DES_C		end  AS APP1_DES_C	      	 
	,case when A.APP2_DES		='' or A.APP2_DES		 is null then B.APP2_DES 	    else A.APP2_DES			end  AS APP2_DES		     
	,case when A.APP2_DES_C		='' or A.APP2_DES_C		 is null then B.APP2_DES_C	    else A.APP2_DES_C		end  AS APP2_DES_C	      	 
	,case when A.APP3_DES		='' or A.APP3_DES		 is null then B.APP3_DES	    else A.APP3_DES		 	end  AS APP3_DES			     
	,case when A.APP3_DES_C		='' or A.APP3_DES_C		 is null then B.APP3_DES_C	    else A.APP3_DES_C		end  AS APP3_DES_C	     	 
	,case when A.GEN_RN			='' or A.GEN_RN			 is null then B.GEN_RN          else A.GEN_RN			end  AS GEN_RN
FROM   dm.dm_td_external_packinfo_temp  a 
LEFT JOIN dm_retail_pack  B
ON B.PACK_COD = A.PACK_COD  ;
insert overwrite dm.dm_td_external_packinfo_temp(
  PACK_COD ,PACK_DES,STGH_DES,PACK_LCH,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,IS_AZ,AZ_MAIN,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,GEN_RN
)
select * from tmp.tmp_retail_dm_td_external_packinfo;



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


---------------------------------------------------ec---------------------------------------------------
insert overwrite table tmp.tmp_ec_dm_td_external_packinfo 
WITH dm_ec_pack AS (
SELECT 
  iqvia_pack_code PACK_COD,
  MAX(B.PACK_DES) PACK_DES,
  MAX(B.STGH_DES) STGH_DES,
  MAX(B.PACK_LCH) PACK_LCH,
  MAX(B.prod_cod) PROD_COD,
  MAX(B.PROD_DES) PROD_DES,
  MAX(B.PROD_DES_C) PROD_DES_C,
  MAX(B.CMPS_COD) CMPS_COD,
  MAX(B.CMPS_DES) CMPS_DES,
  MAX(B.CMPS_DES_C) CMPS_DES_C,
  MAX(B.ATC1_COD) ATC1_COD,
  MAX(B.ATC2_COD) ATC2_COD,
  MAX(B.ATC3_COD) ATC3_COD,
  MAX(B.ATC4_COD) ATC4_COD,
  MAX(B.APP1_COD) APP1_COD,
  MAX(B.APP2_COD) APP2_COD,
  MAX(B.APP3_COD) APP3_COD,
  MAX(B.BIO_DESC) BIO_DESC,
  MAX(B.GENE_ORIG_DESC) GENE_ORIG_DESC,
  MAX(B.ETH_OTC_DESC) ETH_OTC_DESC,
  MAX(B.NRDL_DESC) NRDL_DESC,
  MAX(B.NRDL_ENTRY_DATE) NRDL_ENTRY_DATE,
  MAX(B.EDL_DESC) EDL_DESC,
  MAX(B.TCM_DESC) TCM_DESC,
  MAX(B.PAED_DESC) PAED_DESC,
  MAX(B.GQCE_DESC) GQCE_DESC,
  case when MAX(B.VBP_DESC) = 'VBP-IN' then 'VBP-IN' when MAX(B.VBP_DESC) LIKE '%VBP%' THEN 'VBP-OUT' WHEN MAX(B.VBP_DESC) IS NOT NULL THEN 'Non VBP' ELSE NULL end as VBP_DESC,
  MAX(B.MANU_COD) MANU_COD,
  MAX(B.MANU_DES) MANU_DES,
  MAX(B.MANU_DES_C) MANU_DES_C,
  MAX(B.MNFL_COD) MNFL_COD,
  MAX(B.MNFL_DES) MNFL_DES,
  MAX(B.CORP_COD) CORP_COD,
  MAX(B.CORP_DES) CORP_DES,
  MAX(B.CORP_DES_C) CORP_DES_C,
  MAX(B.BRANDTYPE) BRANDTYPE,
  MAX(B.IS_AZ) IS_AZ,
  MAX(B.AZ_MAIN) AZ_MAIN,
  MAX(B.ATC1_DES) ATC1_DES,
  MAX(B.ATC1_DES_C) ATC1_DES_C,
  MAX(B.ATC2_DES) ATC2_DES,
  MAX(B.ATC2_DES_C) ATC2_DES_C,
  MAX(B.ATC3_DES) ATC3_DES,
  MAX(B.ATC3_DES_C) ATC3_DES_C,
  MAX(B.ATC4_DES) ATC4_DES,
  MAX(B.ATC4_DES_C) ATC4_DES_C,
  MAX(B.APP1_DES) APP1_DES,
  MAX(B.APP1_DES_C) APP1_DES_C,
  MAX(B.APP2_DES) APP2_DES,
  MAX(B.APP2_DES_C) APP2_DES_C,
  MAX(B.APP3_DES) APP3_DES,
  MAX(B.APP3_DES_C) APP3_DES_C,
  CASE 
    WHEN MAX(B.GENE_ORIG_DESC) = 'ORIG' THEN 1 
    WHEN MAX(B.GENE_ORIG_DESC) = 'Branded Gen' THEN 2 
    WHEN MAX(B.GENE_ORIG_DESC) = 'Unbranded Gen' THEN 3 
    WHEN MAX(B.GENE_ORIG_DESC) = 'TCM' THEN 4 ELSE 5 END GEN_RN
FROM dm.dm_zk_ec_pack_property  B 
GROUP BY B.iqvia_pack_code
)
SELECT 
   A.PACK_COD
	,NVL(A.PACK_DES,B.PACK_DES) 					      AS PACK_DES
	,NVL(A.STGH_DES,B.STGH_DES) 		            AS STGH_DES
	,NVL(A.PACK_LCH,B.PACK_LCH) 		            AS PACK_LCH
	,NVL(A.PROD_COD,B.PROD_COD) 	              AS PROD_COD
	,NVL(A.PROD_DES,B.PROD_DES ) 		            AS PROD_DES
	,NVL(A.PROD_DES_C,B.PROD_DES_C ) 		        AS PROD_DES_C
	,NVL(A.CMPS_COD,B.CMPS_COD) 		            AS CMPS_COD
	,NVL(A.CMPS_DES,B.CMPS_DES) 		            AS CMPS_DES
	,NVL(A.CMPS_DES_C,B.CMPS_DES_C) 		        AS CMPS_DES_C
	,NVL(A.ATC1_COD,B.ATC1_COD) 		            AS ATC1_COD
	,NVL(A.ATC2_COD,B.ATC2_COD) 		            AS ATC2_COD
	,NVL(A.ATC3_COD,B.ATC3_COD) 		            AS ATC3_COD
	,NVL(A.ATC4_COD,B.ATC4_COD) 		            AS ATC4_COD
	,NVL(A.APP1_COD,B.APP1_COD) 		            AS APP1_COD
	,NVL(A.APP2_COD,B.APP2_COD) 		            AS APP2_COD
	,NVL(A.APP3_COD,B.APP3_COD) 		            AS APP3_COD
	,NVL(A.BIO_DESC,B.BIO_DESC) 		            AS BIO_DESC
	,NVL(A.GENE_ORIG_DESC,B.GENE_ORIG_DESC) 	  AS GENE_ORIG_DESC
	,NVL(A.ETH_OTC_DESC,B.ETH_OTC_DESC) 	      AS ETH_OTC_DESC
	,NVL(A.NRDL_DESC,B.NRDL_DESC) 		       	 	AS NRDL_DESC
	,NVL(A.NRDL_ENTRY_DATE,B.NRDL_ENTRY_DATE) 	AS NRDL_ENTRY_DATE
	,NVL(A.EDL_DESC,B.EDL_DESC) 		            AS EDL_DESC
	,NVL(A.TCM_DESC,B.TCM_DESC) 		            AS TCM_DESC
	,NVL(A.PAED_DESC,B.PAED_DESC) 		        	AS PAED_DESC
	,NVL(A.GQCE_DESC,B.GQCE_DESC) 		        	AS GQCE_DESC
	,NVL(A.VBP_DESC,B.VBP_DESC) 					      AS VBP_DESC
	,NVL(A.MANU_COD,B.MANU_COD) 					      AS MANU_COD
	,NVL(A.MANU_DES,B.MANU_DES) 					      AS MANU_DES
	,NVL(A.MANU_DES_C,B.MANU_DES_C) 				    AS MANU_DES_C
	,NVL(A.MNFL_COD,B.MNFL_COD) 					      AS MNFL_COD
	,NVL(A.MNFL_DES,B.MNFL_DES) 					      AS MNFL_DES
	,NVL(A.CORP_COD,B.CORP_COD) 					      AS CORP_COD
	,NVL(A.CORP_DES,B.CORP_DES) 					      AS CORP_DES
	,NVL(A.CORP_DES_C,B.CORP_DES_C) 				    AS CORP_DES_C
	,NVL(A.BRANDTYPE,B.BRANDTYPE) 					    AS BRANDTYPE
	,NVL(A.IS_AZ,B.IS_AZ) 							        AS IS_AZ
	,NVL(A.AZ_MAIN,B.AZ_MAIN) 						      AS AZ_MAIN
	,NVL(A.ATC1_DES,B.ATC1_DES) 					      AS ATC1_DES
	,NVL(A.ATC1_DES_C,B.ATC1_DES_C) 			      AS ATC1_DES_C
	,NVL(A.ATC2_DES,B.ATC2_DES) 					      AS ATC2_DES
	,NVL(A.ATC2_DES_C,B.ATC2_DES_C) 			      AS ATC2_DES_C
	,NVL(A.ATC3_DES,B.ATC3_DES) 					      AS ATC3_DES
	,NVL(A.ATC3_DES_C,B.ATC3_DES_C) 			      AS ATC3_DES_C
	,NVL(A.ATC4_DES,B.ATC4_DES) 					      AS ATC4_DES
	,NVL(A.ATC4_DES_C,B.ATC4_DES_C) 			      AS ATC4_DES_C
	,NVL(A.APP1_DES,B.APP1_DES) 					      AS APP1_DES
	,NVL(A.APP1_DES_C,B.APP1_DES_C) 			      AS APP1_DES_C
	,NVL(A.APP2_DES,B.APP2_DES) 					      AS APP2_DES
	,NVL(A.APP2_DES_C,B.APP2_DES_C) 			      AS APP2_DES_C
	,NVL(A.APP3_DES,B.APP3_DES)	 					      AS APP3_DES
	,NVL(A.APP3_DES_C,B.APP3_DES_C) 			      AS APP3_DES_C
  ,NVL(A.GEN_RN,B.GEN_RN)                     AS GEN_RN
FROM   dm.dm_td_external_packinfo_temp  a 
LEFT JOIN dm_ec_pack  B
ON B.PACK_COD = A.PACK_COD  ;

insert overwrite dm.dm_td_external_packinfo_temp(
  PACK_COD ,PACK_DES,STGH_DES,PACK_LCH,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,IS_AZ,AZ_MAIN,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,GEN_RN
)
select * from tmp.tmp_ec_dm_td_external_packinfo;


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

-- MAGIC %md
-- MAGIC # AIA渠道

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


--PACK_COD是唯一的，不允许重复
create or replace temporary view aia_external_packinfo_1 as
select 
    case when PACK_COD like '%OTHERS%' or length(PACK_COD) >= 12 or (not PACK_COD REGEXP '^[0-9]') then PACK_COD 
         else right(concat('000000000000',PACK_COD),12)
     end as PACK_COD,
    max(PACK_DES) as PACK_DES,
    max(STGH_DES) as STGH_DES,
    max(PACK_LCH) as PACK_LCH,
    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,
    NULL 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(IS_AZ) as IS_AZ,
    max(AZ_MAIN) as AZ_MAIN,
    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,
    CASE WHEN max(GENE_ORIG_DESC) = 'ORIG' THEN 1 
         WHEN max(GENE_ORIG_DESC) = 'Branded Gen' THEN 2
         WHEN max(GENE_ORIG_DESC) = 'Unbranded Gen' THEN 3
         WHEN max(GENE_ORIG_DESC) = 'TCM' THEN 4
         ELSE 5 
     END as GEN_RN
from dm.dm_aia_pack_property
group by PACK_COD


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


--补全维度
insert overwrite table tmp.aia_external_packinfo_2 
select distinct
  case when A.PACK_COD like '%OTHERS%' or length(A.PACK_COD) >= 12 or (not A.PACK_COD REGEXP '^[0-9]') then A.PACK_COD
       else right(concat('000000000000',A.PACK_COD),12)
	 end as PACK_COD
	,NVL(A.PACK_DES,B.PACK_DES) 					      AS PACK_DES
	,NVL(A.STGH_DES,B.STGH_DES) 		            AS STGH_DES
	,NVL(A.PACK_LCH,B.PACK_LCH) 		            AS PACK_LCH
	,NVL(A.PROD_COD,B.PROD_COD) 	              AS PROD_COD
	,NVL(A.PROD_DES,B.PROD_DES) 		            AS PROD_DES
	,NVL(A.PROD_DES_C,B.PROD_DES_C) 		        AS PROD_DES_C
	,NVL(A.CMPS_COD,B.CMPS_COD) 		            AS CMPS_COD
	,NVL(A.CMPS_DES,B.CMPS_DES) 		            AS CMPS_DES
	,NVL(A.CMPS_DES_C,B.CMPS_DES_C) 		        AS CMPS_DES_C
	,NVL(A.ATC1_COD,B.ATC1_COD) 		            AS ATC1_COD
	,NVL(A.ATC2_COD,B.ATC2_COD) 		            AS ATC2_COD
	,NVL(A.ATC3_COD,B.ATC3_COD) 		            AS ATC3_COD
	,NVL(A.ATC4_COD,B.ATC4_COD) 		            AS ATC4_COD
	,NVL(A.APP1_COD,B.APP1_COD) 		            AS APP1_COD
	,NVL(A.APP2_COD,B.APP2_COD) 		            AS APP2_COD
	,NVL(A.APP3_COD,B.APP3_COD) 		            AS APP3_COD
	,NVL(A.BIO_DESC,B.BIO_DESC) 		            AS BIO_DESC
	,NVL(A.GENE_ORIG_DESC,B.GENE_ORIG_DESC) 	  AS GENE_ORIG_DESC
	,NVL(A.ETH_OTC_DESC,B.ETH_OTC_DESC) 	      AS ETH_OTC_DESC
	,NVL(A.NRDL_DESC,B.NRDL_DESC) 		       	 	AS NRDL_DESC
	,NVL(A.NRDL_ENTRY_DATE,B.NRDL_ENTRY_DATE) 	AS NRDL_ENTRY_DATE
	,NVL(A.EDL_DESC,B.EDL_DESC) 		            AS EDL_DESC
	,NVL(A.TCM_DESC,B.TCM_DESC) 		            AS TCM_DESC
	,NVL(A.PAED_DESC,B.PAED_DESC) 		        	AS PAED_DESC
	,NVL(A.GQCE_DESC,B.GQCE_DESC) 		        	AS GQCE_DESC
	,NVL(A.VBP_DESC,B.VBP_DESC) 					      AS VBP_DESC
	,NVL(A.MANU_COD,B.MANU_COD) 					      AS MANU_COD
	,NVL(A.MANU_DES,B.MANU_DES) 					      AS MANU_DES
	,NVL(A.MANU_DES_C,B.MANU_DES_C) 				    AS MANU_DES_C
	,NVL(A.MNFL_COD,B.MNFL_COD) 					      AS MNFL_COD
	,NVL(A.MNFL_DES,B.MNFL_DES) 					      AS MNFL_DES
	,NVL(A.CORP_COD,B.CORP_COD) 					      AS CORP_COD
  ,REPLACE( NVL(A.CORP_DES,B.CORP_DES) ,' GROUP', '' )  as CORP_DES   -- 消除group
	,NVL(A.CORP_DES_C,B.CORP_DES_C) 				    AS CORP_DES_C
	,NVL(A.BRANDTYPE,B.BRANDTYPE) 					    AS BRANDTYPE
	,NVL(A.IS_AZ,B.IS_AZ) 							        AS IS_AZ
	,NVL(A.AZ_MAIN,B.AZ_MAIN) 						      AS AZ_MAIN
	,NVL(A.ATC1_DES,B.ATC1_DES) 					      AS ATC1_DES
	,NVL(A.ATC1_DES_C,B.ATC1_DES_C) 			      AS ATC1_DES_C
	,NVL(A.ATC2_DES,B.ATC2_DES) 					      AS ATC2_DES
	,NVL(A.ATC2_DES_C,B.ATC2_DES_C) 			      AS ATC2_DES_C
	,NVL(A.ATC3_DES,B.ATC3_DES) 					      AS ATC3_DES
	,NVL(A.ATC3_DES_C,B.ATC3_DES_C) 			      AS ATC3_DES_C
	,NVL(A.ATC4_DES,B.ATC4_DES) 					      AS ATC4_DES
	,NVL(A.ATC4_DES_C,B.ATC4_DES_C) 			      AS ATC4_DES_C
	,NVL(A.APP1_DES,B.APP1_DES) 					      AS APP1_DES
	,NVL(A.APP1_DES_C,B.APP1_DES_C) 			      AS APP1_DES_C
	,NVL(A.APP2_DES,B.APP2_DES) 					      AS APP2_DES
	,NVL(A.APP2_DES_C,B.APP2_DES_C) 			      AS APP2_DES_C
	,NVL(A.APP3_DES,B.APP3_DES)	 					      AS APP3_DES
	,NVL(A.APP3_DES_C,B.APP3_DES_C) 			      AS APP3_DES_C
  ,NVL(A.GEN_RN,B.GEN_RN)                     AS GEN_RN
FROM   dm.dm_td_external_packinfo_temp  A
LEFT JOIN aia_external_packinfo_1  B
ON case when A.PACK_COD like '%OTHERS%' or length(A.PACK_COD) >= 12 or (not A.PACK_COD REGEXP '^[0-9]') then A.PACK_COD else right(concat('000000000000',A.PACK_COD),12) end 
 = B.PACK_COD


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


--更新dm.dm_td_external_packinfo_temp
insert overwrite dm.dm_td_external_packinfo_temp (
PACK_COD,
PACK_DES,
STGH_DES,
PACK_LCH,
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,
IS_AZ,
AZ_MAIN,
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,
GEN_RN
)
select 
PACK_COD,
PACK_DES,
STGH_DES,
PACK_LCH,
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,
IS_AZ,
AZ_MAIN,
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,
GEN_RN
from tmp.aia_external_packinfo_2


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

-- MAGIC %md
-- MAGIC # XIEHE渠道

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


--PACK_COD是唯一的，不允许重复
create or replace temporary view xiehe_external_packinfo_1 as
select 
    A.PACK_COD,
    max(A.PACK_DES) as PACK_DES,
    max(A.STGH_DES) as STGH_DES,
    max(A.PACK_LCH) as PACK_LCH,
    max(A.PROD_COD) as PROD_COD,
    max(A.PROD_DES) as PROD_DES,
    max(A.PROD_DES_C) as PROD_DES_C,
    max(A.CMPS_COD) as CMPS_COD,
    max(A.CMPS_DES) as CMPS_DES,
    max(A.CMPS_DES_C) as CMPS_DES_C,
    max(A.ATC1_COD) as ATC1_COD,
    max(A.ATC2_COD) as ATC2_COD,
    max(A.ATC3_COD) as ATC3_COD,
    max(A.ATC4_COD) as ATC4_COD,
    max(A.APP1_COD) as APP1_COD,
    max(A.APP2_COD) as APP2_COD,
    max(A.APP3_COD) as APP3_COD,
    max(A.BIO_DESC) as BIO_DESC,
    max(A.GENE_ORIG_DESC) as GENE_ORIG_DESC,
    max(A.ETH_OTC_DESC) as ETH_OTC_DESC,
    max(A.NRDL_DESC) as NRDL_DESC,
    max(A.NRDL_Entry_Date) as NRDL_ENTRY_DATE,
    max(A.EDL_DESC) as EDL_DESC,
    max(A.TCM_DESC) as TCM_DESC,
    max(A.PAED_DESC) as PAED_DESC,
    max(A.GQCE_DESC) as GQCE_DESC,
    NULL as VBP_DESC,
    max(A.MANU_COD) as MANU_COD,
    max(A.MANU_DES) as MANU_DES,
    max(A.MANU_DES_C) as MANU_DES_C,
    max(A.MNFL_COD) as MNFL_COD,
    max(A.MNFL_DES) as MNFL_DES,
    max(A.CORP_COD) as CORP_COD,
    max(A.CORP_DES) as CORP_DES,
    max(A.CORP_DES_C) as CORP_DES_C,
    max(A.BrandType) as BRANDTYPE,
    max(A.IS_AZ) as IS_AZ,
    max(A.AZ_MAIN) as AZ_MAIN,
    max(A.ATC1_DES) as ATC1_DES,
    max(A.ATC1_DES_C) as ATC1_DES_C,
    max(A.ATC2_DES) as ATC2_DES,
    max(A.ATC2_DES_C) as ATC2_DES_C,
    max(A.ATC3_DES) as ATC3_DES,
    max(A.ATC3_DES_C) as ATC3_DES_C,
    max(A.ATC4_DES) as ATC4_DES,
    max(A.ATC4_DES_C) as ATC4_DES_C,
    max(A.APP1_DES) as APP1_DES,
    max(A.APP1_DES_C) as APP1_DES_C,
    max(A.APP2_DES) as APP2_DES,
    max(A.APP2_DES_C) as APP2_DES_C,
    max(A.APP3_DES) as APP3_DES,
    max(A.APP3_DES_C) as APP3_DES_C,
    CASE WHEN max(A.GENE_ORIG_DESC) = 'ORIG' THEN 1 
         WHEN max(A.GENE_ORIG_DESC) = 'Branded Gen' THEN 2
         WHEN max(A.GENE_ORIG_DESC) = 'Unbranded Gen' THEN 3
         WHEN max(A.GENE_ORIG_DESC) = 'TCM' THEN 4
         ELSE 5 
     END as GEN_RN
from dm.dm_xiehe_pack_property a 
group by A.PACK_COD


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


--补全维度
insert overwrite table tmp.xiehe_external_packinfo_2 
select distinct
  A.PACK_COD
	,NVL(A.PACK_DES,B.PACK_DES) 					      AS PACK_DES
	,NVL(A.STGH_DES,B.STGH_DES) 		            AS STGH_DES
	,NVL(A.PACK_LCH,B.PACK_LCH) 		            AS PACK_LCH
	,NVL(A.PROD_COD,B.PROD_COD) 	              AS PROD_COD
	,NVL(A.PROD_DES,B.PROD_DES) 		            AS PROD_DES
	,NVL(A.PROD_DES_C,B.PROD_DES_C) 		        AS PROD_DES_C
	,NVL(A.CMPS_COD,B.CMPS_COD) 		            AS CMPS_COD
	,NVL(A.CMPS_DES,B.CMPS_DES) 		            AS CMPS_DES
	,NVL(A.CMPS_DES_C,B.CMPS_DES_C) 		        AS CMPS_DES_C
	,NVL(A.ATC1_COD,B.ATC1_COD) 		            AS ATC1_COD
	,NVL(A.ATC2_COD,B.ATC2_COD) 		            AS ATC2_COD
	,NVL(A.ATC3_COD,B.ATC3_COD) 		            AS ATC3_COD
	,NVL(A.ATC4_COD,B.ATC4_COD) 		            AS ATC4_COD
	,NVL(A.APP1_COD,B.APP1_COD) 		            AS APP1_COD
	,NVL(A.APP2_COD,B.APP2_COD) 		            AS APP2_COD
	,NVL(A.APP3_COD,B.APP3_COD) 		            AS APP3_COD
	,NVL(A.BIO_DESC,B.BIO_DESC) 		            AS BIO_DESC
	,NVL(A.GENE_ORIG_DESC,B.GENE_ORIG_DESC) 	  AS GENE_ORIG_DESC
	,NVL(A.ETH_OTC_DESC,B.ETH_OTC_DESC) 	      AS ETH_OTC_DESC
	,NVL(A.NRDL_DESC,B.NRDL_DESC) 		       	 	AS NRDL_DESC
	,NVL(A.NRDL_ENTRY_DATE,B.NRDL_ENTRY_DATE) 	AS NRDL_ENTRY_DATE
	,NVL(A.EDL_DESC,B.EDL_DESC) 		            AS EDL_DESC
	,NVL(A.TCM_DESC,B.TCM_DESC) 		            AS TCM_DESC
	,NVL(A.PAED_DESC,B.PAED_DESC) 		        	AS PAED_DESC
	,NVL(A.GQCE_DESC,B.GQCE_DESC) 		        	AS GQCE_DESC
	,NVL(A.VBP_DESC,B.VBP_DESC) 					      AS VBP_DESC
	,NVL(A.MANU_COD,B.MANU_COD) 					      AS MANU_COD
	,NVL(A.MANU_DES,B.MANU_DES) 					      AS MANU_DES
	,NVL(A.MANU_DES_C,B.MANU_DES_C) 				    AS MANU_DES_C
	,NVL(A.MNFL_COD,B.MNFL_COD) 					      AS MNFL_COD
	,NVL(A.MNFL_DES,B.MNFL_DES) 					      AS MNFL_DES
	,NVL(A.CORP_COD,B.CORP_COD) 					      AS CORP_COD
  ,REPLACE( NVL(A.CORP_DES,B.CORP_DES) ,' GROUP', '' )  as CORP_DES   -- 消除group
	,NVL(A.CORP_DES_C,B.CORP_DES_C) 				    AS CORP_DES_C
	,NVL(A.BRANDTYPE,B.BRANDTYPE) 					    AS BRANDTYPE
	,NVL(A.IS_AZ,B.IS_AZ) 							        AS IS_AZ
	,NVL(A.AZ_MAIN,B.AZ_MAIN) 						      AS AZ_MAIN
	,NVL(A.ATC1_DES,B.ATC1_DES) 					      AS ATC1_DES
	,NVL(A.ATC1_DES_C,B.ATC1_DES_C) 			      AS ATC1_DES_C
	,NVL(A.ATC2_DES,B.ATC2_DES) 					      AS ATC2_DES
	,NVL(A.ATC2_DES_C,B.ATC2_DES_C) 			      AS ATC2_DES_C
	,NVL(A.ATC3_DES,B.ATC3_DES) 					      AS ATC3_DES
	,NVL(A.ATC3_DES_C,B.ATC3_DES_C) 			      AS ATC3_DES_C
	,NVL(A.ATC4_DES,B.ATC4_DES) 					      AS ATC4_DES
	,NVL(A.ATC4_DES_C,B.ATC4_DES_C) 			      AS ATC4_DES_C
	,NVL(A.APP1_DES,B.APP1_DES) 					      AS APP1_DES
	,NVL(A.APP1_DES_C,B.APP1_DES_C) 			      AS APP1_DES_C
	,NVL(A.APP2_DES,B.APP2_DES) 					      AS APP2_DES
	,NVL(A.APP2_DES_C,B.APP2_DES_C) 			      AS APP2_DES_C
	,NVL(A.APP3_DES,B.APP3_DES)	 					      AS APP3_DES
	,NVL(A.APP3_DES_C,B.APP3_DES_C) 			      AS APP3_DES_C
  ,NVL(A.GEN_RN,B.GEN_RN)                     AS GEN_RN
FROM   dm.dm_td_external_packinfo_temp  A
LEFT JOIN xiehe_external_packinfo_1  B
ON A.PACK_COD = B.PACK_COD


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

--更新dm.dm_td_external_packinfo_temp
insert overwrite dm.dm_td_external_packinfo_temp (
PACK_COD,
PACK_DES,
STGH_DES,
PACK_LCH,
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,
IS_AZ,
AZ_MAIN,
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,
GEN_RN
)
select 
PACK_COD,
PACK_DES,
STGH_DES,
PACK_LCH,
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,
IS_AZ,
AZ_MAIN,
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,
GEN_RN
from tmp.xiehe_external_packinfo_2


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

-----------------------------------------------------------------------------------
--修改时间：20240827
--修改人：FanXujia
--修改内容：
--有一部分pack的商品名、公司名为空，这两不能为空，将他们设置成OTHERS
-----------------------------------------------------------------------------------
update dm.dm_td_external_packinfo_temp
set PROD_DES = 'OTHERS'
where PROD_DES is null;

update dm.dm_td_external_packinfo_temp
set PROD_DES_C = 'OTHERS'
where PROD_DES_C is null;

update dm.dm_td_external_packinfo_temp
set CORP_DES = 'OTHERS'
where CORP_DES is null or CORP_DES='';

update dm.dm_td_external_packinfo_temp
set CORP_DES_C = 'OTHERS'
where CORP_DES_C is null or CORP_DES_C='';


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

-- MAGIC %md
-- MAGIC # 增加data_source字段

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

--增加DATA_SOURCE字段
insert overwrite table dm.dm_td_external_packinfo_temp2 (
PACK_COD,
PACK_DES,
STGH_DES,
PACK_LCH,
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,
IS_AZ,
AZ_MAIN,
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,
GEN_RN,
DATA_SOURCE,
area,
h_level,
dept_name,
reimburse,
reimburse_type,
prescription_source,
atc,
new_code,
common_name,
product_name,
manu_des_xiehe,
pack_des_xiehe,
drug_delivery_route,
nfc,
LaunchTime,
-- ,Family_Code,
-- Family_Name
VBP_BATCH,
VBP_IMPLEMENTING_TIME
)
select 
case when t1.PACK_COD is null or t1.PACK_COD = '' then concat('PACK_COD_',t2.DATA_SOURCE)
     else t1.PACK_COD end as PACK_COD,
PACK_DES,
STGH_DES,
PACK_LCH,
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,
case when CORP_COD is null or CORP_COD = '' then concat('CORP_COD_',t2.DATA_SOURCE)
     else CORP_COD end as CORP_COD,
CORP_DES,
CORP_DES_C,
BRANDTYPE,
IS_AZ,
AZ_MAIN,
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,
GEN_RN,
t2.DATA_SOURCE,
'' as area,
null as h_level,
'' as dept_name,
null as reimburse,
null as reimburse_type,
null as prescription_source,
null as atc,
'' as new_code,
null as common_name,
null as product_name,
null as manu_des_xiehe,
null as pack_des_xiehe,
null as drug_delivery_route,
null as nfc,
null as LaunchTime,
-- ,null as Family_Code,
-- null as Family_Name
'' as VBP_BATCH,
'' as VBP_IMPLEMENTING_TIME
from dm.dm_td_external_packinfo_temp t1
inner join (select distinct pack_cod,DATA_SOURCE from external_sales_union where NVL(PACK_FLAG,'1') <> 0) t2
on t1.PACK_COD = t2.pack_cod
union all
select 
case when t1.PACK_COD is null or t1.PACK_COD = '' then concat('PACK_COD_',t2.DATA_SOURCE)
     else t1.PACK_COD end as PACK_COD,
PACK_DES,
STGH_DES,
PACK_LCH,
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,
null as VBP_DESC,
MANU_COD,
MANU_DES,
MANU_DES_C,
MNFL_COD,
MNFL_DES,
case when CORP_COD is null or CORP_COD = '' then concat('CORP_COD_',t2.DATA_SOURCE)
     else CORP_COD end as CORP_COD,
CORP_DES,
CORP_DES_C,
BRANDTYPE,
IS_AZ,
AZ_MAIN,
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,
GEN_RN,
t2.DATA_SOURCE,
'' as area,
null as h_level,
'' as dept_name,
null as reimburse,
null as reimburse_type,
null as prescription_source,
null as atc,
'' as new_code,
null as common_name,
null as product_name,
null as manu_des_xiehe,
null as pack_des_xiehe,
null as drug_delivery_route,
null as nfc,
null as LaunchTime,
-- ,null as Family_Code,
-- null as Family_Name
'' as VBP_BATCH,
'' as VBP_IMPLEMENTING_TIME
from dm.dm_td_external_packinfo_temp t1
inner join (select distinct pack_cod,DATA_SOURCE from external_sales_union where PACK_FLAG = 0) t2
on t1.PACK_COD = t2.pack_cod

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

-----------------------------------------------------------------------------------------------------
--修改时间：20240829
--修改人:FanXujia
--修改内容：
--增加协和相关产品属性值
------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------
--修改时间：20250621
--修改人:chenwu
--修改内容：
--xiehe数据源变更格式故重写部分逻辑
------------------------------------------------------------------------------------------------------
with raw_data as (
--一个pack_cod对应多个科室信息，也全部显示出来
select distinct
NVL(new_code,'') new_code,
NVL(province,'') area,
max(h_level) h_level,
NVL(dept_name,'') dept_name,
max(reimburse) reimburse,
max(reimburse_type) reimburse_type,
max(prescription_source) prescription_source,
max(atc) atc,
max(common_name) common_name,
max(product_name) product_name,
max(manu_des) manu_des,
max(pack_des) pack_des,
max(drug_delivery_route) drug_delivery_route,
max(nfc) nfc,
max(iqvia_pack_code) iqvia_pack_code
from dm.dm_ext_xiehe_sales--dwd.dwd_gnd_ext_xiehe_raw_data_new
group by new_code,dept_name,area
)
,packinfo as (
select 
t1.PACK_COD,
t1.PACK_DES,
t1.STGH_DES,
t1.PACK_LCH,
t1.PROD_COD,
t1.PROD_DES,
t1.PROD_DES_C,
t1.CMPS_COD,
t1.CMPS_DES,
t1.CMPS_DES_C,
t1.ATC1_COD,
t1.ATC2_COD,
t1.ATC3_COD,
t1.ATC4_COD,
t1.APP1_COD,
t1.APP2_COD,
t1.APP3_COD,
t1.BIO_DESC,
t1.GENE_ORIG_DESC,
t1.ETH_OTC_DESC,
t1.NRDL_DESC,
t1.NRDL_ENTRY_DATE,
t1.EDL_DESC,
t1.TCM_DESC,
t1.PAED_DESC,
t1.GQCE_DESC,
t1.VBP_DESC,
t1.MANU_COD,
t1.MANU_DES,
t1.MANU_DES_C,
t1.MNFL_COD,
t1.MNFL_DES,
t1.CORP_COD,
t1.CORP_DES,
t1.CORP_DES_C,
t1.BRANDTYPE,
t1.IS_AZ,
t1.AZ_MAIN,
t1.ATC1_DES,
t1.ATC1_DES_C,
t1.ATC2_DES,
t1.ATC2_DES_C,
t1.ATC3_DES,
t1.ATC3_DES_C,
t1.ATC4_DES,
t1.ATC4_DES_C,
t1.APP1_DES,
t1.APP1_DES_C,
t1.APP2_DES,
t1.APP2_DES_C,
t1.APP3_DES,
t1.APP3_DES_C,
t1.GEN_RN,
t1.DATA_SOURCE,
t3.area,
t3.h_level,
t3.dept_name,
t3.reimburse,
t3.reimburse_type,
t3.prescription_source,
t3.atc,
t3.new_code,
t3.common_name,
t3.product_name,
t3.manu_des manu_des_xiehe,
t3.pack_des pack_des_xiehe,
t3.drug_delivery_route,
t3.nfc,
null as LaunchTime,
-- ,null as Family_Code,
-- null as Family_Name
'' as VBP_BATCH,
'' as VBP_IMPLEMENTING_TIME
from dm.dm_td_external_packinfo_temp2 t1
left join raw_data t3
on upper(t1.pack_cod) = upper(t3.iqvia_pack_code)
where t1.DATA_SOURCE = 'XH Data(Quarterly)'
union all
select 
*
from dm.dm_td_external_packinfo_temp2
where DATA_SOURCE <> 'XH Data(Quarterly)'
)


insert overwrite table dm.dm_td_external_packinfo_temp2 (
PACK_COD,
PACK_DES,
STGH_DES,
PACK_LCH,
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,
IS_AZ,
AZ_MAIN,
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,
GEN_RN,
DATA_SOURCE,
area,
h_level,
dept_name,
reimburse,
reimburse_type,
prescription_source,
atc,
new_code,
common_name,
product_name,
manu_des_xiehe,
pack_des_xiehe,
drug_delivery_route,
nfc,
LaunchTime,
-- ,Family_Code,
-- Family_Name
VBP_BATCH,
VBP_IMPLEMENTING_TIME
)
select *
from packinfo;

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

-----------------------------------------------------------------------------------------------------
--修改时间：20240902
--修改人:FanXujia
--修改内容：
--增加CHPA的维度信息
------------------------------------------------------------------------------------------------------
with PACK_launchtime as (
select PACK.Pack_Code,max(PACK.launchtime) launchtime
from dwd.dwd_ims_td_pack PACK
group by PACK.Pack_Code
)
-- ,Family as (
-- select PACK_COD,max(Family_Code) Family_Code,max(Family_Name) Family_Name
-- from DM.DM_IMS_TD_PACK_PROPERTY
-- where MARKET = 'IMS ALL Market'
-- group by PACK_COD
-- )
,packinfo as (
select 
t1.PACK_COD,
t1.PACK_DES,
t1.STGH_DES,
t1.PACK_LCH,
t1.PROD_COD,
t1.PROD_DES,
t1.PROD_DES_C,
t1.CMPS_COD,
t1.CMPS_DES,
t1.CMPS_DES_C,
t1.ATC1_COD,
t1.ATC2_COD,
t1.ATC3_COD,
t1.ATC4_COD,
t1.APP1_COD,
t1.APP2_COD,
t1.APP3_COD,
t1.BIO_DESC,
t1.GENE_ORIG_DESC,
t1.ETH_OTC_DESC,
t1.NRDL_DESC,
t1.NRDL_ENTRY_DATE,
t1.EDL_DESC,
t1.TCM_DESC,
t1.PAED_DESC,
t1.GQCE_DESC,
t1.VBP_DESC,
t1.MANU_COD,
t1.MANU_DES,
t1.MANU_DES_C,
t1.MNFL_COD,
t1.MNFL_DES,
t1.CORP_COD,
t1.CORP_DES,
t1.CORP_DES_C,
t1.BRANDTYPE,
t1.IS_AZ,
t1.AZ_MAIN,
t1.ATC1_DES,
t1.ATC1_DES_C,
t1.ATC2_DES,
t1.ATC2_DES_C,
t1.ATC3_DES,
t1.ATC3_DES_C,
t1.ATC4_DES,
t1.ATC4_DES_C,
t1.APP1_DES,
t1.APP1_DES_C,
t1.APP2_DES,
t1.APP2_DES_C,
t1.APP3_DES,
t1.APP3_DES_C,
t1.GEN_RN,
t1.DATA_SOURCE,
t1.area,
t1.h_level,
NVL(t1.dept_name,'') dept_name,
t1.reimburse,
t1.reimburse_type,
t1.prescription_source,
t1.atc,
t1.new_code,
t1.common_name,
t1.product_name,
t1.manu_des_xiehe,
t1.pack_des_xiehe,
t1.drug_delivery_route,
t1.nfc,
t2.LaunchTime,
-- ,t3.Family_Code,
-- t3.Family_Name
t1.VBP_BATCH,
t1.VBP_IMPLEMENTING_TIME
from dm.dm_td_external_packinfo_temp2 t1
left join PACK_launchtime t2
on t1.PACK_COD = t2.Pack_Code
-- left join Family t3
-- on t1.PACK_COD = t3.PACK_COD
where t1.DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
union all
select *
from dm.dm_td_external_packinfo_temp2
where DATA_SOURCE <> 'IQVIA-CHPA(Monthly)'
)



insert overwrite table dm.dm_td_external_packinfo_temp2 (
PACK_COD,
PACK_DES,
STGH_DES,
PACK_LCH,
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,
IS_AZ,
AZ_MAIN,
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,
GEN_RN,
DATA_SOURCE,
area,
h_level,
dept_name,
reimburse,
reimburse_type,
prescription_source,
atc,
new_code,
common_name,
product_name,
manu_des_xiehe,
pack_des_xiehe,
drug_delivery_route,
nfc,
LaunchTime,
-- ,Family_Code,
-- Family_Name
VBP_BATCH,
VBP_IMPLEMENTING_TIME
)
select *
from packinfo;

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

--修改时间：20240808
--修改人：FanXuJia
--修改背景：
--事实表与维度表来自于不同手工表，目前存在一部分pack_cod在事实表有销量，但是在维度表没有这个pack_cod，导致销量缺失。
--因此需要从事实表补全这部分pack_cod
with raw_data as (
--一个pack_cod对应多个科室信息，也全部显示出来
select distinct
NVL(new_code,'') new_code,
NVL(province,'') area,
max(h_level) h_level,
NVL(dept_name,'') dept_name,
max(reimburse) reimburse,
max(reimburse_type) reimburse_type,
max(prescription_source) prescription_source,
max(atc) atc,
max(common_name) common_name,
max(product_name) product_name,
max(manu_des) manu_des,
max(pack_des) pack_des,
max(drug_delivery_route) drug_delivery_route,
max(nfc) nfc,
max(iqvia_pack_code) iqvia_pack_code
from dm.dm_ext_xiehe_sales--dwd.dwd_gnd_ext_xiehe_raw_data_new
group by new_code,dept_name,area
)
,PACK_launchtime as (
select PACK.Pack_Code,max(PACK.launchtime) launchtime
from dwd.dwd_ims_td_pack PACK
group by PACK.Pack_Code
)
-- ,Family as (
-- select PACK_COD,max(Family_Code) Family_Code,max(Family_Name) Family_Name
-- from DM.DM_IMS_TD_PACK_PROPERTY
-- where MARKET = 'IMS ALL Market'
-- group by PACK_COD
-- )
,tmp_packinfo as (
select distinct 
  t1.PACK_COD,t1.CORP_COD,t1.DATA_SOURCE,nvl(t3.CORP_DES,'OTHERS') CORP_DES,nvl(t3.CORP_DES_C,'OTHERS') CORP_DES_C,
t5.area,
t5.h_level,
t5.dept_name,
t5.reimburse,
t5.reimburse_type,
t5.prescription_source,
t5.atc,
t5.new_code,
t5.common_name,
t5.product_name,
t5.manu_des manu_des_xiehe,
t5.pack_des pack_des_xiehe,
t5.drug_delivery_route,
t5.nfc
,null as launchtime
-- ,null as Family_Code
-- ,null as Family_Name
from external_sales_union t1
left join dm.dm_td_external_packinfo_temp2 t2
on t1.PACK_COD = t2.PACK_COD
and t1.DATA_SOURCE = t2.DATA_SOURCE
left join (select CORP_COD,max(CORP_DES) CORP_DES,max(CORP_DES_C) CORP_DES_C
             from dm.dm_td_external_packinfo_temp2 group by CORP_COD) t3
on t1.CORP_COD = t3.CORP_COD
-- left join pack_code_info t4
-- on t1.PACK_COD = t4.IQVIA_PACK_CODE
left join raw_data t5
on upper(t1.pack_cod) = upper(t5.iqvia_pack_code)
where t2.PACK_COD is null
and t1.DATA_SOURCE = 'XH Data(Quarterly)'
union all
select distinct 
  t1.PACK_COD,t1.CORP_COD,t1.DATA_SOURCE,nvl(t3.CORP_DES,'OTHERS') CORP_DES,nvl(t3.CORP_DES_C,'OTHERS') CORP_DES_C,
'' as area,
null as h_level,
'' as dept_name,
null as reimburse,
null as reimburse_type,
null as prescription_source,
null as atc,
'' as new_code,
null as common_name,
null as product_name,
null as manu_des_xiehe,
null as pack_des_xiehe,
null as drug_delivery_route,
null as nfc,
t4.launchtime
-- ,t5.Family_Code
-- ,t5.Family_Name
from external_sales_union t1
left join dm.dm_td_external_packinfo_temp2 t2
on t1.PACK_COD = t2.PACK_COD
and t1.DATA_SOURCE = t2.DATA_SOURCE
left join (select CORP_COD,max(CORP_DES) CORP_DES,max(CORP_DES_C) CORP_DES_C
             from dm.dm_td_external_packinfo_temp2 group by CORP_COD) t3
on t1.CORP_COD = t3.CORP_COD
left join PACK_launchtime t4
on t1.PACK_COD = t4.Pack_Code
-- left join Family t5
-- on t1.PACK_COD = t5.PACK_COD
where t2.PACK_COD is null
and t1.DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
union all
select distinct 
  t1.PACK_COD,t1.CORP_COD,t1.DATA_SOURCE,nvl(t3.CORP_DES,'OTHERS') CORP_DES,nvl(t3.CORP_DES_C,'OTHERS') CORP_DES_C,
'' as area,
null as h_level,
'' as dept_name,
null as reimburse,
null as reimburse_type,
null as prescription_source,
null as atc,
'' as new_code,
null as common_name,
null as product_name,
null as manu_des_xiehe,
null as pack_des_xiehe,
null as drug_delivery_route,
null as nfc,
null as launchtime
-- ,null as Family_Code
-- ,null as Family_Name
from external_sales_union t1
left join dm.dm_td_external_packinfo_temp2 t2
on t1.PACK_COD = t2.PACK_COD
and t1.DATA_SOURCE = t2.DATA_SOURCE
left join (select CORP_COD,max(CORP_DES) CORP_DES,max(CORP_DES_C) CORP_DES_C
             from dm.dm_td_external_packinfo_temp2 group by CORP_COD) t3
on t1.CORP_COD = t3.CORP_COD
where t2.PACK_COD is null
and t1.DATA_SOURCE not in ('XH Data(Quarterly)','IQVIA-CHPA(Monthly)')
)

insert into table dm.dm_td_external_packinfo_temp2 (
PACK_COD,
PACK_DES,
STGH_DES,
PACK_LCH,
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,
IS_AZ,
AZ_MAIN,
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,
GEN_RN,
DATA_SOURCE,
area,
h_level,
dept_name,
reimburse,
reimburse_type,
prescription_source,
atc,
new_code,
common_name,
product_name,
manu_des_xiehe,
pack_des_xiehe,
drug_delivery_route,
nfc,
LaunchTime,
-- ,Family_Code,
-- Family_Name
VBP_BATCH,
VBP_IMPLEMENTING_TIME
)
select distinct
PACK_COD,
null as PACK_DES,
null as STGH_DES,
null as PACK_LCH,
null as PROD_COD,
null as PROD_DES,
null as PROD_DES_C,
null as CMPS_COD,
null as CMPS_DES,
null as CMPS_DES_C,
null as ATC1_COD,
null as ATC2_COD,
null as ATC3_COD,
null as ATC4_COD,
null as APP1_COD,
null as APP2_COD,
null as APP3_COD,
null as BIO_DESC,
null as GENE_ORIG_DESC,
null as ETH_OTC_DESC,
null as NRDL_DESC,
null as NRDL_ENTRY_DATE,
null as EDL_DESC,
null as TCM_DESC,
null as PAED_DESC,
null as GQCE_DESC,
null as VBP_DESC,
null as MANU_COD,
null as MANU_DES,
null as MANU_DES_C,
null as MNFL_COD,
null as MNFL_DES,
CORP_COD,
CORP_DES,
CORP_DES_C,
null as BRANDTYPE,
null as IS_AZ,
null as AZ_MAIN,
null as ATC1_DES,
null as ATC1_DES_C,
null as ATC2_DES,
null as ATC2_DES_C,
null as ATC3_DES,
null as ATC3_DES_C,
null as ATC4_DES,
null as ATC4_DES_C,
null as APP1_DES,
null as APP1_DES_C,
null as APP2_DES,
null as APP2_DES_C,
null as APP3_DES,
null as APP3_DES_C,
null as GEN_RN,
DATA_SOURCE,
area,
h_level,
dept_name,
reimburse,
reimburse_type,
prescription_source,
atc,
new_code,
common_name,
product_name,
manu_des_xiehe,
pack_des_xiehe,
drug_delivery_route,
nfc,
LaunchTime,
-- ,null as Family_Code,
-- null as Family_Name
'' as VBP_BATCH,
'' as VBP_IMPLEMENTING_TIME
from tmp_packinfo


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

------------------------------------------------------------------------------------------------
--修改时间：20240923
--修改人：FanXujia
--修改内容：
--增加VBP信息
------------------------------------------------------------------------------------------------
with tmp as (
select t1.PACK_COD,
t1.PACK_DES,
t1.STGH_DES,
t1.PACK_LCH,
t1.PROD_COD,
t1.PROD_DES,
t1.PROD_DES_C,
t1.CMPS_COD,
t1.CMPS_DES,
t1.CMPS_DES_C,
t1.ATC1_COD,
t1.ATC2_COD,
t1.ATC3_COD,
t1.ATC4_COD,
t1.APP1_COD,
t1.APP2_COD,
t1.APP3_COD,
t1.BIO_DESC,
t1.GENE_ORIG_DESC,
t1.ETH_OTC_DESC,
t1.NRDL_DESC,
t1.NRDL_ENTRY_DATE,
t1.EDL_DESC,
t1.TCM_DESC,
t1.PAED_DESC,
t1.GQCE_DESC,
NVL(t1.VBP_DESC,'Non VBP') AS VBP_DESC,
t1.MANU_COD,
t1.MANU_DES,
t1.MANU_DES_C,
t1.MNFL_COD,
t1.MNFL_DES,
t1.CORP_COD,
t1.CORP_DES,
t1.CORP_DES_C,
t1.BRANDTYPE,
t1.IS_AZ,
t1.AZ_MAIN,
t1.ATC1_DES,
t1.ATC1_DES_C,
t1.ATC2_DES,
t1.ATC2_DES_C,
t1.ATC3_DES,
t1.ATC3_DES_C,
t1.ATC4_DES,
t1.ATC4_DES_C,
t1.APP1_DES,
t1.APP1_DES_C,
t1.APP2_DES,
t1.APP2_DES_C,
t1.APP3_DES,
t1.APP3_DES_C,
t1.GEN_RN,
t1.DATA_SOURCE,
t1.area,
t1.h_level,
t1.dept_name,
t1.reimburse,
t1.reimburse_type,
t1.prescription_source,
t1.atc,
t1.new_code,
t1.common_name,
t1.product_name,
t1.manu_des_xiehe,
t1.pack_des_xiehe,
t1.drug_delivery_route,
t1.nfc,
t1.LaunchTime,
NVL(t2.VBP_BATCH,'') VBP_BATCH,
NVL(t2.VBP_IMPLEMENTING_TIME,'') VBP_IMPLEMENTING_TIME
from dm.dm_td_external_packinfo_temp2 t1
left join dwd.dwd_gnd_ims_vbp_information t2
on t1.PACK_COD = t2.PACK_CODE
)

insert overwrite table dm.dm_td_external_packinfo_temp2
select * from tmp

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

-------------------------------------------------------------------------------------------------
--修改时间：20240906
--修改人：FanXujia
--修改内容:
--按主键去重
-------------------------------------------------------------------------------------------------
with tmp as (
select
PACK_COD,
max(PACK_DES) PACK_DES,
max(STGH_DES) STGH_DES,
max(PACK_LCH) PACK_LCH,
max(PROD_COD) PROD_COD,
max(PROD_DES) PROD_DES,
COALESCE(max(PROD_DES_C),'') PROD_DES_C,
max(CMPS_COD) CMPS_COD,
max(CMPS_DES) CMPS_DES,
max(CMPS_DES_C) CMPS_DES_C,
max(ATC1_COD) ATC1_COD,
max(ATC2_COD) ATC2_COD,
max(ATC3_COD) ATC3_COD,
max(ATC4_COD) ATC4_COD,
max(APP1_COD) APP1_COD,
max(APP2_COD) APP2_COD,
max(APP3_COD) APP3_COD,
max(BIO_DESC) BIO_DESC,
max(GENE_ORIG_DESC) GENE_ORIG_DESC,
max(ETH_OTC_DESC) ETH_OTC_DESC,
max(NRDL_DESC) NRDL_DESC,
max(NRDL_ENTRY_DATE) NRDL_ENTRY_DATE,
max(EDL_DESC) EDL_DESC,
max(TCM_DESC) TCM_DESC,
max(PAED_DESC) PAED_DESC,
max(GQCE_DESC) GQCE_DESC,
max(VBP_DESC) VBP_DESC,
max(MANU_COD) MANU_COD,
max(MANU_DES) MANU_DES,
max(MANU_DES_C) MANU_DES_C,
max(MNFL_COD) MNFL_COD,
max(MNFL_DES) MNFL_DES,
max(CORP_COD) CORP_COD,
max(CORP_DES) CORP_DES,
max(CORP_DES_C) CORP_DES_C,
max(BRANDTYPE) BRANDTYPE,
max(IS_AZ) IS_AZ,
max(AZ_MAIN) AZ_MAIN,
max(ATC1_DES) ATC1_DES,
max(ATC1_DES_C) ATC1_DES_C,
max(ATC2_DES) ATC2_DES,
max(ATC2_DES_C) ATC2_DES_C,
max(ATC3_DES) ATC3_DES,
max(ATC3_DES_C) ATC3_DES_C,
max(ATC4_DES) ATC4_DES,
max(ATC4_DES_C) ATC4_DES_C,
max(APP1_DES) APP1_DES,
max(APP1_DES_C) APP1_DES_C,
max(APP2_DES) APP2_DES,
max(APP2_DES_C) APP2_DES_C,
max(APP3_DES) APP3_DES,
max(APP3_DES_C) APP3_DES_C,
max(GEN_RN) GEN_RN,
DATA_SOURCE,
area,
max(h_level) h_level,
dept_name,
max(reimburse) reimburse,
max(reimburse_type) reimburse_type,
max(prescription_source) prescription_source,
max(atc) atc,
new_code,
max(common_name) common_name,
max(product_name) product_name,
max(manu_des_xiehe) manu_des_xiehe,
max(pack_des_xiehe) pack_des_xiehe,
max(drug_delivery_route) drug_delivery_route,
max(nfc) nfc,
max(LaunchTime) LaunchTime,
max(VBP_BATCH) VBP_BATCH,
max(VBP_IMPLEMENTING_TIME) VBP_IMPLEMENTING_TIME
from dm.dm_td_external_packinfo_temp2
group by DATA_SOURCE,PACK_COD,dept_name,new_code,area
)

insert overwrite dm.dm_td_external_packinfo_temp2
select * from tmp;

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

------------------------------------------------------------------------------------------
--修改时间：20241104
--修改人：Fanxujia
--修改内容：
--CHPA有个分子式英文名：TRADITIONAL CHINESE MEDICINE，对应的中文名是取自另一个表tblProdCN的namec字段，每个产品的中文名目前是不同的。
--用户想要让中文名统一显示成“传统中药”
--因此通过hardcoding的方式，把中文名修改成传统中药。
--另外，本改动不局限于CHPA渠道，全部渠道一起修改。
-------------------------------------------------------------------------------------------
update dm.dm_td_external_packinfo_temp2
set CMPS_DES_C = '传统中药'
where upper(CMPS_DES) = 'TRADITIONAL CHINESE MEDICINE'
;

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

--------------------------------------------------------------------------------
--修改时间：20241025
--修改人：Fanxujia
--修改内容：
--外部数据报告优化，将PBI的逻辑搬到DBR
--------------------------------------------------------------------------------
insert overwrite table dm.dm_td_external_packinfo
(
PACK_COD,
PACK_DES,
PACK_LCH,
PROD_COD,
PROD_DES,
PROD_DES_C,
CMPS_COD,
CMPS_DES,
CMPS_DES_C,
VBP_BATCH,
VBP_IMPLEMENTING_TIME,
STGH_DES,
BIO_DESC,
ETH_OTC_DESC,
NRDL_DESC,
NRDL_ENTRY_DATE,
EDL_DESC,
TCM_DESC,
PAED_DESC,
GQCE_DESC,
MANU_COD,
MANU_DES,
MNFL_COD,
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,
APP1_COD,
APP1_DES,
APP1_DES_C,
APP2_COD,
APP2_DES,
APP2_DES_C,
APP3_COD,
APP3_DES,
APP3_DES_C,
GENE_ORIG_DESC,
VBP_DESC,
CORP_COD,
GEN_RN,
CORP_DES,
CORP_DES_C,
PACK_RN,
PROD_RN,
CMPS_RN,
DATA_SOURCE,
ATC,
NEW_CODE,
COMMON_NAME,
PRODUCT_NAME,
MANU_DES_XIEHE,
PACK_DES_XIEHE,
DRUG_DELIVERY_ROUTE,
NFC,
CORP_TYPE,
LAUNCHTIME,
VBP_BRAND
)

with CHPA_PACK_SALES as (
  SELECT
    upper(A.PACK_DES) PACK_DES,
    upper(A.PROD_DES_C) PROD_DES_C,
    upper(A.CORP_DES_C) CORP_DES_C,
    SUM(B.SALES_VALUE_CAL) SALES_VALUE_CAL
  FROM
    DM.dm_td_external_packinfo_temp2 A
    INNER JOIN external_sales_union B ON A.PACK_COD = B.PACK_COD
    and A.dept_name = B.dept_name
    and A.new_code = B.new_code
    and A.area = B.area
    and A.DATA_SOURCE = B.DATA_SOURCE
  WHERE
    B.YYYYMM >= (
      SELECT
        LEFT(MAX(YYYYMM), 4) * 100 + 1
      FROM
        external_sales_union
      WHERE
        DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
    )
    AND B.DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
  GROUP BY
    upper(A.PACK_DES),
    upper(A.CORP_DES_C),
    upper(A.PROD_DES_C)
)
,pack_N as (
  SELECT
    PACK_DES,
    PROD_DES_C,
    CORP_DES_C,
    row_number() OVER(
      PARTITION BY 1
      ORDER BY
        (
          CASE
            WHEN PACK_DES = ''
            OR PROD_DES_C = ''
            OR CORP_DES_C = '' THEN 2
            ELSE 1
          END
        ) ASC,
        sum(SALES_VALUE_CAL) OVER(PARTITION BY PROD_DES_C, CORP_DES_C) DESC,
        SALES_VALUE_CAL DESC
    ) RN
  FROM
    CHPA_PACK_SALES
)

,CHPA_BRAND_SALES as (
  SELECT
    upper(A.PROD_DES_C) PROD_DES_C,
    upper(A.CORP_DES_C) CORP_DES_C,
    SUM(B.SALES_VALUE_CAL) SALES_VALUE_CAL
  FROM
    DM.dm_td_external_packinfo_temp2 A
    INNER JOIN external_sales_union B ON A.PACK_COD = B.PACK_COD
    and A.dept_name = B.dept_name
    and A.new_code = B.new_code
    and A.area = B.area
    and A.DATA_SOURCE = B.DATA_SOURCE
  WHERE
    B.YYYYMM >= (
      SELECT
        LEFT(MAX(YYYYMM), 4) * 100 + 1
      FROM
        external_sales_union
      WHERE
        DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
    )
    AND B.DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
  GROUP BY
    upper(A.CORP_DES_C),
    upper(A.PROD_DES_C)
)
,brand_N as (
  SELECT
    PROD_DES_C,
    CORP_DES_C,
    row_number() OVER(
      PARTITION BY 1
      ORDER BY
        (
          CASE
            WHEN PROD_DES_C = ''
            OR CORP_DES_C = '' THEN 2
            ELSE 1
          END
        ) ASC,
        SALES_VALUE_CAL DESC
    ) RN
  FROM
    CHPA_BRAND_SALES
)

,CHPA_MOLE_SALES as (
  SELECT
    upper(A.CMPS_DES) CMPS_DES,
    upper(A.CMPS_DES_C) CMPS_DES_C,
    SUM(B.SALES_VALUE_CAL) SALES_VALUE_CAL
  FROM
    DM.dm_td_external_packinfo_temp2 A
    INNER JOIN external_sales_union B ON A.PACK_COD = B.PACK_COD
    and A.dept_name = B.dept_name
    and A.new_code = B.new_code
    and A.area = B.area
    and A.DATA_SOURCE = B.DATA_SOURCE
  WHERE
    B.YYYYMM >= (
      SELECT
        LEFT(MAX(YYYYMM), 4) * 100 + 1
      FROM
        external_sales_union
      WHERE
        DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
    )
    AND B.DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
  GROUP BY
    upper(A.CMPS_DES),
    upper(A.CMPS_DES_C)
)
,mole_N as (
  SELECT
    CMPS_DES,
    CMPS_DES_C,
    row_number() OVER(
      PARTITION BY 1
      ORDER BY
        (
          CASE
            WHEN CMPS_DES = ''
            OR CMPS_DES_C = '' THEN 2
            ELSE 1
          END
        ) ASC,
        SALES_VALUE_CAL DESC
    ) RN
  FROM
    CHPA_MOLE_SALES
)

------------------------------------------------------------------------------
--修改时间：20241209
--修改人：Fanxujia
--修改内容：
--调整VBP_BRAND的逻辑
--一个Brand对应多个pack，每个pack会有不同的VBP_DESC
--有以下几种情况
--1：一个Brand下面的所有pack的VBP_DESC都一样，VBP_BRAND = VBP_DESC的值
--2：一个Brand下面的VBP_DESC存在VBP-IN、VBP-OUT两种情况，VBP_BRAND = VBP-IN/OUT
--3：一个Brand下面的VBP_DESC存在VBP-IN、Non VBP两种情况，VBP_BRAND = VBP-IN/Non VBP
--4：一个Brand下面的VBP_DESC存在VBP-OUT、Non VBP两种情况，VBP_BRAND = VBP-OUT/Non VBP
--5：一个Brand下面的VBP_DESC存在VBP-IN、VBP-OUT、Non VBP三种情况，VBP_BRAND = VBP-IN/OUT/Non VBP
------------------------------------------------------------------------------
-- ,vbp_brand as (
-- select PROD_DES_C,DATA_SOURCE,max(VBP_DESC) VBP_BRAND
-- from DM.dm_td_external_packinfo_temp2
-- group by PROD_DES_C,DATA_SOURCE
-- )
,vbp_brand_1 as (
select distinct 
PROD_DES_C,
DATA_SOURCE,
case when VBP_DESC = 'VBP-IN' then 1
     when VBP_DESC = 'VBP-OUT' then 2
     when VBP_DESC = 'Non VBP' then 4
     else 99
end as VBP_FLAG
from dm.dm_td_external_packinfo_temp2
)
,vbp_brand as (
select 
PROD_DES_C,
DATA_SOURCE,
case
--单独一个VBP-IN（1）
     when sum(VBP_FLAG) = 1 then 'VBP-IN'
--单独一个VBP-OUT（2）
     when sum(VBP_FLAG) = 2 then 'VBP-OUT'
--单独一个Non VBP（4）
     when sum(VBP_FLAG) = 4 then 'Non VBP'
--同时存在VBP-IN（1） + VBP-OUT（2） = 3
     when sum(VBP_FLAG) = 3 then 'VBP-IN/OUT'
--同时存在VBP-IN（1） + Non VBP（4） = 5
     when sum(VBP_FLAG) = 5 then 'VBP-IN/Non VBP'
--同时存在VBP-OUT（2） + Non VBP（4） = 6
     when sum(VBP_FLAG) = 6 then 'VBP-OUT/Non VBP'
--同时存在VBP-IN（1） + VBP-OUT（2） + Non VBP（4） = 7
     when sum(VBP_FLAG) = 7 then 'VBP-IN/OUT/Non VBP'
     else ''
end as VBP_BRAND
from vbp_brand_1
group by PROD_DES_C,DATA_SOURCE
)


SELECT
  A.PACK_COD,
  upper(max(A.PACK_DES)) PACK_DES,
  max(A.PACK_LCH) PACK_LCH,
  max(A.PROD_COD) PROD_COD,
  max(A.PROD_DES) PROD_DES,
  upper(max(A.PROD_DES_C)) PROD_DES_C,
  max(A.CMPS_COD) CMPS_COD,
  upper(max(A.CMPS_DES)) CMPS_DES,
  upper(max(A.CMPS_DES_C)) CMPS_DES_C,
  max(A.VBP_BATCH) VBP_BATCH,
  max(A.VBP_IMPLEMENTING_TIME) VBP_IMPLEMENTING_TIME,
  max(A.STGH_DES) STGH_DES,
  max(A.BIO_DESC) BIO_DESC,
  max(A.ETH_OTC_DESC) ETH_OTC_DESC,
  max(A.NRDL_DESC) NRDL_DESC,
  max(A.NRDL_ENTRY_DATE) NRDL_ENTRY_DATE,
  max(A.EDL_DESC) EDL_DESC,
  max(A.TCM_DESC) TCM_DESC,
  max(A.PAED_DESC) PAED_DESC,
  max(A.GQCE_DESC) GQCE_DESC,
  max(A.MANU_COD) MANU_COD,
  max(A.MANU_DES) MANU_DES,
  max(A.MNFL_COD) MNFL_COD,
  max(A.ATC1_COD) ATC1_COD,
  max(A.ATC1_DES) ATC1_DES,
  max(A.ATC1_DES_C) ATC1_DES_C,
  max(A.ATC2_COD) ATC2_COD,
  max(A.ATC2_DES) ATC2_DES,
  max(A.ATC2_DES_C) ATC2_DES_C,
  max(A.ATC3_COD) ATC3_COD,
  max(A.ATC3_DES) ATC3_DES,
  max(A.ATC3_DES_C) ATC3_DES_C,
  max(A.ATC4_COD) ATC4_COD,
  max(A.ATC4_DES) ATC4_DES,
  max(A.ATC4_DES_C) ATC4_DES_C,
  max(A.APP1_COD) APP1_COD,
  max(A.APP1_DES) APP1_DES,
  max(A.APP1_DES_C) APP1_DES_C,
  max(A.APP2_COD) APP2_COD,
  max(A.APP2_DES) APP2_DES,
  max(A.APP2_DES_C) APP2_DES_C,
  max(A.APP3_COD) APP3_COD,
  max(A.APP3_DES) APP3_DES,
  max(A.APP3_DES_C) APP3_DES_C,
  max(A.GENE_ORIG_DESC) GENE_ORIG_DESC,
  max(A.VBP_DESC) VBP_DESC,
  max(A.CORP_COD) CORP_COD,
  max(A.GEN_RN) GEN_RN,
  max(A.CORP_DES) CORP_DES,
  upper(max(A.CORP_DES_C)) CORP_DES_C,
  NVL(max(PN.RN), 9999999) PACK_RN,
  NVL(max(BN.RN), 9999999) PROD_RN,
  NVL(max(MN.RN), 9999999) CMPS_RN,
  A.DATA_SOURCE,
  max(A.ATC) ATC,
  A.NEW_CODE,
  max(A.COMMON_NAME) COMMON_NAME,
  max(A.PRODUCT_NAME) PRODUCT_NAME,
  max(A.MANU_DES_XIEHE) MANU_DES_XIEHE,
  max(A.PACK_DES_XIEHE) PACK_DES_XIEHE,
  max(A.DRUG_DELIVERY_ROUTE) DRUG_DELIVERY_ROUTE,
  max(A.NFC) NFC,
  case when max(upper(A.MNFL_DES)) = 'LOCAL' then 'LOCAL'
       else 'MNC'
  end as CORP_TYPE,
  max(A.LAUNCHTIME) LAUNCHTIME,
  max(VBP.VBP_BRAND) VBP_BRAND
FROM
  DM.dm_td_external_packinfo_temp2 A
  LEFT JOIN pack_N PN ON upper(A.PACK_DES) = PN.PACK_DES
  AND upper(A.PROD_DES_C) = PN.PROD_DES_C
  AND upper(A.CORP_DES_C) = PN.CORP_DES_C
  LEFT JOIN brand_N BN ON upper(A.PROD_DES_C) = BN.PROD_DES_C
  AND upper(A.CORP_DES_C) = BN.CORP_DES_C
  LEFT JOIN mole_N MN ON upper(A.CMPS_DES) = MN.CMPS_DES
  AND upper(A.CMPS_DES_C) = MN.CMPS_DES_C
  left join vbp_brand VBP on VBP.PROD_DES_C = A.PROD_DES_C
  and VBP.DATA_SOURCE = A.DATA_SOURCE
group by A.PACK_COD,A.NEW_CODE,A.DATA_SOURCE

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

-- DBTITLE 1,找出多个中文名或英文名的PROD
insert overwrite table dm.dm_td_exchange_packinfo_m_prod
with 
base_table as (
  select 
    PROD_DES_C
    ,PROD_COD
    ,CMPS_COD
    ,CMPS_DES_C
    ,DATA_SOURCE 
  from DM.DM_TD_EXTERNAL_PACKINFO
  where PROD_COD is not null 
  group by 1,2,3,4,5
)
select distinct
  t1.prod_cod,t1.cmps_cod,t1.data_source 
from  base_table t1
  left join (select PROD_DES_C,DATA_SOURCE from base_table 
  group by 1,2
  having count(distinct PROD_COD)>1
  ) t2
on t1.prod_des_c = t2.prod_des_c and t1.data_source = t2.data_source
left join (select CMPS_DES_C,DATA_SOURCE from base_table
  group by 1,2
  having count(distinct cmps_cod)>1
  ) t3
on t1.CMPS_DES_C = t3.CMPS_DES_C and t1.data_source = t3.data_source
where t2.prod_des_c is not null or t3.cmps_des_c is not null