-- Databricks notebook source
-- county 自己的维度表 dm_ext_county_td_pack_property
insert overwrite table dm.dm_ext_county_td_pack_property --------------------------------------------county事实表中与CHPA匹配到的维度信息--------------------------------------------
  with county_matched_value as (
    select
      concat(t2.MARKET,t1.PACK_COD) as MARKET_PACK_KEY,
      t1.PACK_COD,
      t1.PACK_DES,
      t1.STGH_DES,
      t1.PACK_LCH,
      t1.Family_Code,
      t1.Family_Name,
      t1.PROD_COD,
      t1.PROD_DES,
      t1.PROD_DES_C,
      t1.CMPS_COD,
      t1.CMPS_DES,
      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.MNFL_COD,
      t1.MNFL_DES,
      t1.CORP_COD,
      t1.CORP_DES,
      t1.CORP_DES_C,
      t1.BrandType,
      t2.MARKET,
      t2.KEY_COMPETITOR,
      t1.IS_AZ,
      t1.AZ_MAIN,
      null AZ_Related,
      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,
      t2.Class,
      t1.ETL_INSERT_DT,
      t1.ETL_UPDATE_DT
    from
      dm.dm_ims_td_pack_property t1
left join DM.DM_TD_EXT_CHPA_MARKET_PACK_MAPPING t2 on t1.PACK_COD = t2.PACK_CODE
--事实表中有yyyymm字段，故要去重，防止数据发散
          
        inner join (
          select distinct
            PACK_COD
          from
            dws.dws_ext_county_tf_sales
        ) t3 on t1.PACK_COD = t3.PACK_COD
  ),
  -------------------------------county事实表中与CHPA没有匹配到的维度信息取county维度表里的维度信息-------------------------------
  --获取county表与CHPA没有匹配到的MARKET_PACK_KEY
  county_notmatched_key as (select
      t3.PACK_COD
    from
      (
        select distinct
          PACK_COD
        from
          dws.dws_ext_county_tf_sales
      ) t3
    where
      not exists (
        select
          1
        from
          dm.dm_ims_td_pack_property t4
        where
          t3.PACK_COD = t4.PACK_COD
      )
  ),
  county_notmatched_value as (--获取county维度表里的维度信息
    select
    distinct
      t6.MARKET_PACK_KEY,
      t6.PACK_COD,
      t6.PACK_DES,
      t6.STGH_DES,
      t6.PACK_LCH,
      t6.Family_Code,
      t6.Family_Name,
      t6.PROD_COD,
      t6.PROD_DES,
      t6.PROD_DES_C,
      t6.CMPS_COD,
      t6.CMPS_DES,
      t6.ATC1_COD,
      t6.ATC2_COD,
      t6.ATC3_COD,
      t6.ATC4_COD,
      t6.APP1_COD,
      t6.APP2_COD,
      t6.APP3_COD,
      t6.BIO_DESC,
      t6.GENE_ORIG_DESC,
      t6.ETH_OTC_DESC,
      t6.NRDL_DESC,
      t6.NRDL_Entry_Date,
      t6.EDL_DESC,
      t6.TCM_DESC,
      t6.PAED_DESC,
      t6.GQCE_DESC,
      t6.VBP_DESC,
      t6.MANU_COD,
      t6.MANU_DES,
      t6.MNFL_COD,
      t6.MNFL_DES,
      t6.CORP_COD,
      t6.CORP_DES,
      t6.CORP_DES_C,
      t6.BrandType,
      t6.MARKET,
      t6.KEY_COMPETITOR,
      t6.IS_AZ,
      t6.AZ_MAIN,
      t6.AZ_Related,
      t6.atc1_des,
      t6.atc1_des_c,
      t6.atc2_des,
      t6.atc2_des_c,
      t6.atc3_des,
      t6.atc3_des_c,
      t6.atc4_des,
      t6.atc4_des_c,
      t6.app1_des,
      t6.app1_des_c,
      t6.app2_des,
      t6.app2_des_c,
      t6.app3_des,
      t6.app3_des_c,
      t6.Class,
      from_utc_timestamp(current_timestamp, 'UTC+8') ETL_INSERT_DT,
      from_utc_timestamp(current_timestamp, 'UTC+8') ETL_UPDATE_DT
    from
      county_notmatched_key t5
        left join dm.dm_ims_county_td_pack_property t6 --此表有重复数据（etl时间不同）
          on t5.PACK_COD = t6.PACK_COD
  ),
  county_total_pack_property as (select
      *
    from
      county_matched_value
    union
    select
      *
    from
      county_notmatched_value
  )
  select
    MARKET_PACK_KEY,
    PACK_COD,
    PACK_DES,
    STGH_DES,
    PACK_LCH,
    Family_Code,
    Family_Name,
    PROD_COD,
    PROD_DES,
    PROD_DES_C,
    CMPS_COD,
    CMPS_DES,
    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,
    MNFL_COD,
    MNFL_DES,
    CORP_COD,
    CORP_DES,
    CORP_DES_C,
    BrandType,
    MARKET,
    upper(NVL(KEY_COMPETITOR, 'Others')) as KEY_COMPETITOR,
    IS_AZ,
    AZ_MAIN,
    AZ_Related,
    atc1_des,
    atc1_des_c,
    atc2_des,
    atc2_des_c,
    atc3_des,
    atc3_des_c,
    atc4_des,
    atc4_des_c,
    app1_des,
    app1_des_c,
    app2_des,
    app2_des_c,
    app3_des,
    app3_des_c,
    Class,
    ETL_INSERT_DT,
    ETL_UPDATE_DT
  from
    county_total_pack_property