-- Databricks notebook source
-- 源表：DWS.DWS_IMS_TD_ATC_CN，DWS.DWS_IMS_TD_NFC_CN，DWS.DWS_IMS_TD_CORP_CN，DWS.DWS_IMS_TD_MANU_CN，DWS.DWS_IMS_TD_PROD_CN，DWD.DWD_IMS_TD_MOLECULE，DWD.DWD_GND_IMS_TBLMARKET_TA_MAP，DWS.DWS_IMS_TD_MARKET，DWD.DWD_IMS_TD_PACK_PROPERTY，DWD.DWD_GND_DEPT_PACK_PROPERTY，DWD.DWD_GND_IMS_TBLMARKET，DWD.DWD_GND_IMS_TBLKEYCOMPETITOR，DWD.DWD_GND_TBLCLASS，TMP.TMP_IMS_TF_FACT_SALES，DWD.DWD_GND_IMS_TBLBRANDRATIO，DWS.DWS_IMS_TD_PACK_PROPERTY，DWS.DWS_IMS_TD_MARKET，TMP.TMP_IMS_TD_MARKET_RATIO1
-- 目标表：DWS.DWS_IMS_TD_PACK_PROPERTY，DM.DM_IMS_TD_PACK_PROPERTY，DWS.DWS_IMS_TD_MARKET，DWS.DWS_IMS_TD_MARKET_RATIO，TMP.TMP_IMS_TD_MARKET_RATIO1

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

-- DBTITLE 1,DIM_ATC
CREATE OR REPLACE TEMPORARY VIEW DIM_ATC AS
SELECT DISTINCT
  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
FROM
  DWS.DWS_IMS_TD_ATC_CN

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

-- DBTITLE 1,DIM_NFC
CREATE OR REPLACE TEMPORARY VIEW DIM_NFC AS
SELECT DISTINCT
  APP1_COD,
  APP1_DES,
  APP1_DES_C,
  APP2_COD,
  APP2_DES,
  APP2_DES_C,
  APP3_COD,
  APP3_DES,
  APP3_DES_C
FROM
  DWS.DWS_IMS_TD_NFC_CN

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

-- DBTITLE 1,DIM_CORP
  CREATE
  OR REPLACE TEMPORARY VIEW DIM_CORP AS
SELECT DISTINCT
  CORP_COD,
  CORP_DES_CN
FROM
  DWS.DWS_IMS_TD_CORP_CN

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

-- DBTITLE 1,DIM_MANU
   CREATE
  OR REPLACE TEMPORARY VIEW DIM_MANU AS
SELECT DISTINCT
  MANU_COD,
  MANU_DES_CN
FROM
  DWS.DWS_IMS_TD_MANU_CN

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

-- DBTITLE 1,DIM_PROD
  CREATE
  OR REPLACE TEMPORARY VIEW DIM_PROD AS
SELECT DISTINCT
  PROD_COD,
  PROD_DES,
  PROD_DES_C,
  CMPS_DES_C
FROM
  DWS.DWS_IMS_TD_PROD_CN

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

-- DBTITLE 1,DIM_MOLECULE
  CREATE
  OR REPLACE TEMPORARY VIEW DIM_MOLECULE AS
SELECT DISTINCT
  MOLECULE_NAME,
  FAMILY_CODE,
  FAMILY_NAME
FROM
  DWD.DWD_IMS_TD_MOLECULE

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

-- DBTITLE 1,DIM_MARKET_TA_MAPPING
  CREATE
  OR REPLACE TEMPORARY VIEW DIM_MARKET_TA_MAPPING AS
SELECT DISTINCT
  MARKET,
  TA
FROM
  DWD.DWD_GND_IMS_TBLMARKET_TA_MAP

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

-- DBTITLE 1,DIM_MARKET
  CREATE
  OR REPLACE TEMPORARY VIEW DIM_MARKET AS
SELECT DISTINCT
  PACK_COD,
  PROD_COD,
  MARKET,
  KEY_COMPETITOR
FROM
  DWS.DWS_IMS_TD_MARKET

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

-- DBTITLE 1,DIM_PACK_PROPERTY
  CREATE
  OR REPLACE TEMPORARY VIEW DIM_PACK_PROPERTY AS
SELECT DISTINCT
  PROD_COD
FROM
  DWD.DWD_IMS_TD_PACK_PROPERTY
WHERE
  CORP_COD = 'A5Z'

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

-- CREATE or replace TABLE catalog_e2_ngbi_qas.dws.dws_ims_td_pack_property (
--   PACK_COD STRING,
--   PACK_DES STRING,
--   STGH_DES STRING,
--   PACK_LCH STRING,
--   Family_Code STRING,
--   Family_Name STRING,
--   PROD_COD STRING,
--   PROD_DES STRING,
--   PROD_DES_C STRING,
--   CMPS_COD STRING,
--   CMPS_DES STRING,
--   CMPS_DES_C STRING,
--   ATC1_COD STRING,
--   ATC2_COD STRING,
--   ATC3_COD STRING,
--   ATC4_COD STRING,
--   APP1_COD STRING,
--   APP2_COD STRING,
--   APP3_COD STRING,
--   BIO_DESC STRING,
--   GENE_ORIG_DESC STRING,
--   ETH_OTC_DESC STRING,
--   NRDL_DESC STRING,
--   NRDL_Entry_Date STRING,
--   EDL_DESC STRING,
--   TCM_DESC STRING,
--   PAED_DESC STRING,
--   GQCE_DESC STRING,
--   VBP_DESC_V STRING,
--   VBP_DESC STRING,
--   MANU_COD STRING,
--   MANU_DES STRING,
--   MANU_DES_C STRING,
--   MNFL_COD STRING,
--   MNFL_DES STRING,
--   CORP_COD STRING,
--   CORP_DES STRING,
--   CORP_DES_C STRING,
--   BrandType STRING,
--   IS_AZ STRING,
--   AZ_MAIN STRING,
--   atc1_des STRING,
--   atc1_des_c STRING,
--   atc2_des STRING,
--   atc2_des_c STRING,
--   atc3_des STRING,
--   atc3_des_c STRING,
--   atc4_des STRING,
--   atc4_des_c STRING,
--   app1_des STRING,
--   app1_des_c STRING,
--   app2_des STRING,
--   app2_des_c STRING,
--   app3_des STRING,
--   app3_des_c STRING)
-- USING delta
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DWS/dws_ims_td_pack_property'

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

-- DBTITLE 1,overwrite dws_ims_td_pack_property
--pack 属性表逻辑
insert overwrite table dws.dws_ims_td_pack_property
select distinct
  -- case
  --   when dm.Market is null then concat ('Non Market_', dp.PACK_COD)
  --   else concat (dm.Market, '_', dp.PACK_COD)
  -- end as MARKET_PACK_KEY,
  dp.PACK_COD,
  dp.PACK_DES,
  dp.STGH_DES,
  dp.PACK_LCH,
  dm1.Family_Code,
  dm1.Family_Name,
  dp.PROD_COD,
  dp1.PROD_DES,
  dp1.PROD_DES_C,
  dp.CMPS_COD,
  dp.CMPS_DES,
  dp1.CMPS_DES_C as CMPS_DES_C, --20240530正式新增字段
  dp.ATC1_COD,
  dp.ATC2_COD,
  dp.ATC3_COD,
  dp.ATC4_COD,
  dp.APP1_COD,
  dp.APP2_COD,
  dp.APP3_COD,
  dp.BIO_DESC,
  dp.GENE_ORIG_DESC,
  dp.ETH_OTC_DESC,
  dp.NRDL_DESC,
  dp.NRDL_Entry_Date,
  dp.EDL_DESC,
  dp.TCM_DESC,
  dp.PAED_DESC,
  dp.GQCE_DESC,
  dp.VBP_DESC as VBP_DESC_V, --20240530正式新增字段
  CASE
    WHEN dp.VBP_DESC IN ('VBP-IN', 'VBP-N/A') THEN 'VBP'
    ELSE 'Non VBP'
  END VBP_DESC,
  dp.MANU_COD,
  dp.MANU_DES,
  manu.MANU_DES_CN as MANU_DES_C, --20240530正式新增字段
  case
    when dp.MNFL_COD = 'L' then 'L'
    else 'M'
  end MNFL_COD,
  case
    when dp.MNFL_DES = 'Local' then 'LOCAL'
    else 'MNC'
  end MNFL_DES,
  dp.CORP_COD,
  dp.CORP_DES,
  crop.CORP_DES_CN as CORP_DES_C, --20240530正式新增字段
  dp.BrandType,
  -- case
  --   when dm.Market is null then 'Non Market'
  --   else dm.Market
  -- end as MARKET,
  -- NVL (dm.KEY_COMPETITOR, 'Others') KEY_COMPETITOR,
  case
    when ta.PROD_COD is not null then 'Y'
    else 'N'
  end as IS_AZ,
  null as AZ_MAIN,
  -- case
  --   when dmt.TA is null then 'Others MKT'
  --   else 'AZ Related MKT'
  -- end as AZ_Related,
  t2.atc1_des,
  t2.atc1_des_c,
  t3.atc2_des,
  t3.atc2_des_c,
  t4.atc3_des,
  t4.atc3_des_c,
  t5.atc4_des,
  t5.atc4_des_c,
  n1.app1_des,
  n1.app1_des_c,
  n2.app2_des,
  n2.app2_des_c,
  n3.app3_des,
  n3.app3_des_c
from
  dwd.dwd_ims_td_pack_property dp
  -- left join DIM_MARKET AS dm on dp.PACK_COD = dm.PACK_COD
  -- and dp.PROD_COD = dm.PROD_COD
  --将AZ在卖的产品全部给打上AZ标签，包括其他公司在卖的产品
  left join DIM_PACK_PROPERTY AS ta on dp.prod_cod = ta.prod_cod
  -- left join DIM_MARKET_TA_MAPPING AS dmt on dmt.Market = dm.Market
  left join DIM_MOLECULE AS dm1 on dp.CMPS_DES = dm1.Molecule_Name
  left join DIM_PROD dp1 on dp1.PROD_COD = dp.PROD_COD
  left join (
    SELECT DISTINCT
      ATC1_COD,
      ATC1_DES,
      ATC1_DES_C
    FROM
      DIM_ATC
  ) AS t2 on dp.atc1_cod = t2.atc1_cod
  left join (
    SELECT DISTINCT
      ATC2_COD,
      ATC2_DES,
      ATC2_DES_C
    FROM
      DIM_ATC
  ) AS t3 on dp.atc2_cod = t3.atc2_cod
  left join (
    SELECT DISTINCT
      ATC3_COD,
      ATC3_DES,
      ATC3_DES_C
    FROM
      DIM_ATC
  ) AS t4 on dp.atc3_cod = t4.atc3_cod
  left join (
    SELECT DISTINCT
      ATC4_COD,
      ATC4_DES,
      ATC4_DES_C
    FROM
      DIM_ATC
  ) t5 on dp.atc4_cod = t5.atc4_cod
  left join (
    SELECT DISTINCT
      APP1_COD,
      APP1_DES,
      APP1_DES_C
    FROM
      DIM_NFC
  ) AS n1 on dp.app1_cod = n1.app1_cod
  left join (
    SELECT DISTINCT
      APP2_COD,
      APP2_DES,
      APP2_DES_C
    FROM
      DIM_NFC
  ) AS n2 on dp.app2_cod = n2.app2_cod
  left join (
    SELECT DISTINCT
      APP3_COD,
      APP3_DES,
      APP3_DES_C
    FROM
      DIM_NFC
  ) AS n3 on dp.app3_cod = n3.app3_cod
  left join DIM_CORP AS crop on dp.CORP_COD = crop.CORP_COD --20240530正式新增字段CORP_DES_C
  left join DIM_MANU AS manu on dp.MANU_COD = manu.MANU_COD --20240530正式新增字段MANU_DES_C
;

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

-- MAGIC %md
-- MAGIC # FB数据

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

-- DBTITLE 1,FB_RAW_DATA
CREATE
OR REPLACE TEMPORARY VIEW FB_RAW_DATA AS
select DISTINCT
  IQVIA_PACK_CODE as PACK_COD,
  IQVIA_PROD_CODE as PROD_COD,
  PACK_COD as FB_PACK_COD,
  APP1_COD,
  APP1_DES,
  APP1_DES_C,
  APP2_COD,
  APP2_DES,
  APP2_DES_C,
  APP3_COD,
  APP3_DES,
  APP3_DES_C,
  ATC1_COD,
  ATC1_DES,
  ATC1_DES_C,
  ATC2_COD,
  ATC2_DES,
  ATC2_DES_C,
  ATC3_COD,
  ATC3_DES,
  ATC3_DES_C,
  ATC4_COD,
  ATC4_DES,
  ATC4_DES_C,
  BIO_DESC,
  CMPS_COD,
  CMPS_DES,
  CMPS_DES_C,
  CORP_COD,
  CORP_DES,
  CORP_DES_C,
  EDL_DESC,
  ETH_OTC_DESC,
  GENE_ORIG_DESC,
  GQCE_DESC,
  MANU_COD,
  MANU_DES,
  MANU_DES_C,
  MNFL_COD,
  MNFL_DES,
  NRDL_DESC,
  PACK_DES,
  STGH_DES,
  PACK_LCH,
  PAED_DESC,
  PROD_DES,
  PROD_DES_C,
  TCM_DESC,
  VBP_DESC,
  COUNTINGUNIT,
  NULL as NRDL_Entry_Date,
  null as BrandType
  ,null as Is_AZ
  ,null as AZ_MAIN
  ,null as AZ_Related
  ,null as Family_Code
  ,null as Family_Name
from
  dwd.dwd_gnd_dept_pack_property

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

-- DBTITLE 1,FB_MARKET
--   CREATE
--   OR REPLACE TEMPORARY VIEW FB_MARKET AS
-- -- select distinct
--   -- CONCAT (t2.market, '_', t1.PACK_COD) AS MARKET_PACK_KEY,
--   -- t2.market,
--   -- CASE
--   --   WHEN t2.extend_market_ratio IS NULL THEN '1'
--   --   ELSE t2.extend_market_ratio
--   -- END AS Market_Ratio,
-- --   t1.*
-- -- from
-- --   (
--     select
--       t1.iqvia_pack_code as PACK_COD,
--       t1.PACK_DES,
--       t1.STGH_DES,
--       t1.PACK_LCH,
--       null as Family_Code,
--       null as Family_Name,
--       t1.iqvia_prod_code as 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,
--       null as 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,
--       null as BrandType,
--       null as IS_AZ,
--       null as AZ_MAIN,
--       null as 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,
--       countingunit,
--       null as ETL_INSERT_DT,
--       null as ETL_UPDATE_DT
--     from
--       FB_RAW_DATA t1
--   -- ) t1
-- --   left join (
-- --     select
-- --       *
-- --     from
-- --       dwd.dwd_gnd_ims_tblmarket
-- --     WHERE
-- --       Extend_Market IS NULL
-- --       AND (
-- --         NOT_IN_FLAG IS NULL
-- --         or NOT_IN_FLAG = '1'
-- --       )
-- --   ) t2 on COALESCE(t1.ATC1_COD, '') = case
-- --     when t2.ATC1_Code is null then COALESCE(t1.ATC1_COD, '')
-- --     else t2.ATC1_Code
-- --   end
-- --   and COALESCE(t1.ATC2_COD, '') = case
-- --     when t2.ATC2_Code is null then COALESCE(t1.ATC2_COD, '')
-- --     else t2.ATC2_Code
-- --   end
-- --   and COALESCE(t1.ATC3_COD, '') = case
-- --     when t2.ATC3_Code is null then COALESCE(t1.ATC3_COD, '')
-- --     else t2.ATC3_Code
-- --   end
-- --   and COALESCE(t1.ATC4_COD, '') = case
-- --     when t2.ATC4_Code is null then COALESCE(t1.ATC4_COD, '')
-- --     else t2.ATC4_Code
-- --   end
-- --   and COALESCE(t1.APP1_COD, '') = case
-- --     when t2.NFC1_Code is null then COALESCE(t1.APP1_COD, '')
-- --     else t2.NFC1_Code
-- --   end
-- --   and COALESCE(t1.APP2_COD, '') = case
-- --     when t2.NFC2_Code is null then COALESCE(t1.APP2_COD, '')
-- --     else t2.NFC2_Code
-- --   end
-- --   and COALESCE(t1.APP3_COD, '') = case
-- --     when t2.NFC3_Code is null then COALESCE(t1.APP3_COD, '')
-- --     else t2.NFC3_Code
-- --   end
-- --   and COALESCE(t1.CORP_COD, '') = case
-- --     when t2.corporation_code is null then COALESCE(t1.CORP_COD, '')
-- --     else t2.corporation_code
-- --   end
-- --   and COALESCE(t1.MANU_COD, '') = case
-- --     when t2.Manufacturer_Code is null then COALESCE(t1.MANU_COD, '')
-- --     else t2.Manufacturer_Code
-- --   end
-- --   and COALESCE(t1.PROD_COD, '') = case
-- --     when t2.Product_Code is null then COALESCE(t1.PROD_COD, '')
-- --     else t2.Product_Code
-- --   end
-- --   and COALESCE(t1.PACK_COD, '') = case
-- --     when t2.Pack_Code is null then COALESCE(t1.PACK_COD, '')
-- --     else t2.Pack_Code
-- --   end
-- --   and COALESCE(t1.STGH_DES, '') = case
-- --     when t2.Strength is null then COALESCE(t1.STGH_DES, '')
-- --     else t2.Strength
-- --   end
-- --   and COALESCE(t1.CMPS_COD, '') = case
-- --     when t2.Molecule_Code is null then COALESCE(t1.CMPS_COD, '')
-- --     else t2.Molecule_Code
-- --   end
-- --   left join (
-- --     select
-- --       *
-- --     from
-- --       dwd.dwd_gnd_ims_tblmarket
-- --     WHERE
-- --       Extend_Market IS NULL
-- --       AND NOT_IN_FLAG = '0'
-- --   ) t3 on COALESCE(t1.ATC1_COD, '') = case
-- --     when t3.ATC1_Code is null then COALESCE(t1.ATC1_COD, '')
-- --     else t3.ATC1_Code
-- --   end
-- --   and COALESCE(t1.ATC2_COD, '') = case
-- --     when t3.ATC2_Code is null then COALESCE(t1.ATC2_COD, '')
-- --     else t3.ATC2_Code
-- --   end
-- --   and COALESCE(t1.ATC3_COD, '') = case
-- --     when t3.ATC3_Code is null then COALESCE(t1.ATC3_COD, '')
-- --     else t3.ATC3_Code
-- --   end
-- --   and COALESCE(t1.ATC4_COD, '') = case
-- --     when t3.ATC4_Code is null then COALESCE(t1.ATC4_COD, '')
-- --     else t3.ATC4_Code
-- --   end
-- --   and COALESCE(t1.APP1_COD, '') = case
-- --     when t3.NFC1_Code is null then COALESCE(t1.APP1_COD, '')
-- --     else t3.NFC1_Code
-- --   end
-- --   and COALESCE(t1.APP2_COD, '') = case
-- --     when t3.NFC2_Code is null then COALESCE(t1.APP2_COD, '')
-- --     else t3.NFC2_Code
-- --   end
-- --   and COALESCE(t1.APP3_COD, '') = case
-- --     when t3.NFC3_Code is null then COALESCE(t1.APP3_COD, '')
-- --     else t3.NFC3_Code
-- --   end
-- --   and COALESCE(t1.CORP_COD, '') = case
-- --     when t3.corporation_code is null then COALESCE(t1.CORP_COD, '')
-- --     else t3.corporation_code
-- --   end
-- --   and COALESCE(t1.MANU_COD, '') = case
-- --     when t3.Manufacturer_Code is null then COALESCE(t1.MANU_COD, '')
-- --     else t3.Manufacturer_Code
-- --   end
-- --   and COALESCE(t1.PROD_COD, '') = case
-- --     when t3.Product_Code is null then COALESCE(t1.PROD_COD, '')
-- --     else t3.Product_Code
-- --   end
-- --   and COALESCE(t1.PACK_COD, '') = case
-- --     when t3.Pack_Code is null then COALESCE(t1.PACK_COD, '')
-- --     else t3.Pack_Code
-- --   end
-- --   and COALESCE(t1.STGH_DES, '') = case
-- --     when t3.Strength is null then COALESCE(t1.STGH_DES, '')
-- --     else t3.Strength
-- --   end
-- --   and COALESCE(t1.CMPS_COD, '') = case
-- --     when t3.Molecule_Code is null then COALESCE(t1.CMPS_COD, '')
-- --     else t3.Molecule_Code
-- --   end
-- --   and t2.market = t3.market
-- -- where
-- --   t2.market is not null
-- --   and t3.market is null

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

-- DBTITLE 1,FB_COMPETITOR
--   CREATE
--   OR REPLACE TEMPORARY VIEW FB_COMPETITOR AS
--   --可以按照MARKET_PACK_KEY去重
-- select distinct
--   upper(t2.keycompetitor) as Key_Competitor,
--   t1.*,
--   T2.NO1
-- from
--   FB_MARKET t1
--   left join (
--     select
--       ROW_NUMBER() OVER (
--         ORDER BY
--           CASE
--             WHEN ATC1_Code IS NOT NULL THEN 1
--             WHEN ATC2_Code IS NOT NULL THEN 2
--             WHEN ATC3_Code IS NOT NULL THEN 3
--             WHEN ATC4_Code IS NOT NULL THEN 4
--             WHEN Molecule_Code IS NOT NULL THEN 5
--             WHEN Product_Code IS NOT NULL THEN 6
--             WHEN Pack_Code IS NOT NULL THEN 7
--             ELSE 999
--           END,
--           CASE
--             WHEN NFC1_CODE IS NULL THEN 0
--             ELSE 1
--           END,
--           CASE
--             WHEN NFC2_CODE IS NULL THEN 0
--             ELSE 1
--           END,
--           CASE
--             WHEN NFC2_CODE IS NULL THEN 0
--             ELSE 1
--           END,
--           CASE
--             WHEN NFC3_CODE IS NULL THEN 0
--             ELSE 1
--           END
--       ) AS no1,
--       *
--     from
--       dwd.dwd_gnd_ims_tblkeycompetitor
--   ) t2 on COALESCE(t1.ATC1_COD, '') = case
--     when t2.ATC1_Code is null then COALESCE(t1.ATC1_COD, '')
--     else t2.ATC1_Code
--   end
--   and COALESCE(t1.ATC2_COD, '') = case
--     when t2.ATC2_Code is null then COALESCE(t1.ATC2_COD, '')
--     else t2.ATC2_Code
--   end
--   and COALESCE(t1.ATC3_COD, '') = case
--     when t2.ATC3_Code is null then COALESCE(t1.ATC3_COD, '')
--     else t2.ATC3_Code
--   end
--   and COALESCE(t1.ATC4_COD, '') = case
--     when t2.ATC4_Code is null then COALESCE(t1.ATC4_COD, '')
--     else t2.ATC4_Code
--   end
--   and COALESCE(t1.APP1_COD, '') = case
--     when t2.NFC1_Code is null then COALESCE(t1.APP1_COD, '')
--     else t2.NFC1_Code
--   end
--   and COALESCE(t1.APP2_COD, '') = case
--     when t2.NFC2_Code is null then COALESCE(t1.APP2_COD, '')
--     else t2.NFC2_Code
--   end
--   and COALESCE(t1.APP3_COD, '') = case
--     when t2.NFC3_Code is null then COALESCE(t1.APP3_COD, '')
--     else t2.NFC3_Code
--   end
--   and COALESCE(t1.CORP_COD, '') = case
--     when t2.corporation_code is null then COALESCE(t1.CORP_COD, '')
--     else t2.corporation_code
--   end
--   and COALESCE(t1.MANU_COD, '') = case
--     when t2.Manufacturer_Code is null then COALESCE(t1.MANU_COD, '')
--     else t2.Manufacturer_Code
--   end
--   and COALESCE(t1.PROD_COD, '') = case
--     when t2.Product_Code is null then COALESCE(t1.PROD_COD, '')
--     else t2.Product_Code
--   end
--   and COALESCE(t1.PACK_COD, '') = case
--     when t2.Pack_Code is null then COALESCE(t1.PACK_COD, '')
--     else t2.Pack_Code
--   end
--   and COALESCE(t1.STGH_DES, '') = case
--     when t2.Strength is null then COALESCE(t1.STGH_DES, '')
--     else t2.Strength
--   end
--   and COALESCE(t1.CMPS_COD, '') = case
--     when t2.Molecule_Code is null then COALESCE(t1.CMPS_COD, '')
--     else t2.Molecule_Code
--   end
--   and COALESCE(t1.market, '') = case
--     when t2.market is null then COALESCE(t1.market, '')
--     else t2.market
--   end

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

-- DBTITLE 1,FB_COMPETITOR_FILTER
--   -- 20260206 这里增加筛选条件，因为后面发现数据有重复
--   -- 和Chenwu沟通后，认为需要把no1这个开窗排序字段用上，而且在其他数据源，也需要用到
--   CREATE
--   OR REPLACE TEMPORARY VIEW FB_COMPETITOR_FILTER AS
-- select
--   *
-- from
--   (
--     select
--       *,
--       row_number() over (
--         partition by
--           PACK_COD,
--           MARKET
--         order by
--           no1 desc
--       ) as new_order
--     FROM
--       FB_COMPETITOR
--   ) as new
-- WHERE
--   new.new_order = 1

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

-- DBTITLE 1,FB_CLASS
--   CREATE
--   OR REPLACE TEMPORARY VIEW FB_CLASS AS
-- select
--   t1.*,
--   t3.class
-- from
--   FB_COMPETITOR_FILTER t1
--   left join dwd.dwd_gnd_tblclass t3 on t1.market = ifnull (t3.market, t1.market)
--   and t1.pack_cod = ifnull (t3.pack_code, t1.pack_cod)
--   and t1.prod_cod = ifnull (t3.product_code, t1.prod_cod)
--   and t1.cmps_cod = ifnull (t3.molecule_code, t1.cmps_cod)
--   and t1.corp_cod = ifnull (t3.Corporation_code, t1.corp_cod)
--   and t1.manu_cod = ifnull (t3.Manufacturer_Code, t1.manu_cod)
--   and t1.ATC1_COD = ifnull (t3.ATC1_Code, t1.ATC1_COD)
--   and t1.ATC2_COD = ifnull (t3.ATC2_Code, t1.ATC2_COD)
--   and t1.ATC3_COD = ifnull (t3.ATC3_Code, t1.ATC3_COD)
--   and t1.ATC4_COD = ifnull (t3.ATC4_Code, t1.ATC4_COD)
--   and t1.APP1_COD = ifnull (t3.NFC1_Code, t1.APP1_COD)
--   and t1.APP2_COD = ifnull (t3.NFC2_Code, t1.APP2_COD)
--   and t1.APP3_COD = ifnull (t3.NFC3_Code, t1.APP3_COD)
--   and t1.STGH_DES = ifnull (t3.Strength, t1.STGH_DES)

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

-- DBTITLE 1,CHPA_UNION_FB_DATA
-- 20260201  新增
CREATE
OR REPLACE TEMPORARY VIEW CHPA_UNION_FB_DATA AS
select distinct
  -- t1.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.CMPS_DES_C as 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_V as VBP_DESC_V,
  t1.VBP_DESC,
  t1.MANU_COD,
  t1.MANU_DES,
  t1.MANU_DES_C as MANU_DES_C,
  t1.MNFL_COD,
  t1.MNFL_DES,
  t1.CORP_COD,
  case
    when t1.CORP_DES like '% GROUP%' then replace (t1.CORP_DES, ' GROUP', '')
    else t1.CORP_DES
  end CORP_DES,
  t1.CORP_DES_C as CORP_DES_C,
  t1.BrandType,
  -- t1.MARKET,
  -- upper(t1.KEY_COMPETITOR) as KEY_COMPETITOR,
  t1.AZ_MAIN,
  -- t1.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
  -- ,t3.class
from
  dws.dws_ims_td_pack_property t1
  -- left join dwd.dwd_gnd_tblclass t3 on t1.market = ifnull (t3.market, t1.market)
  -- and t1.pack_cod = ifnull (t3.pack_code, t1.pack_cod)
  -- and t1.prod_cod = ifnull (t3.product_code, t1.prod_cod)
  -- and t1.cmps_cod = ifnull (t3.molecule_code, t1.cmps_cod)
  -- and t1.corp_cod = ifnull (t3.Corporation_code, t1.corp_cod)
  -- and t1.manu_cod = ifnull (t3.Manufacturer_Code, t1.manu_cod)
  -- and t1.ATC1_COD = ifnull (t3.ATC1_Code, t1.ATC1_COD)
  -- and t1.ATC2_COD = ifnull (t3.ATC2_Code, t1.ATC2_COD)
  -- and t1.ATC3_COD = ifnull (t3.ATC3_Code, t1.ATC3_COD)
  -- and t1.ATC4_COD = ifnull (t3.ATC4_Code, t1.ATC4_COD)
  -- and t1.APP1_COD = ifnull (t3.NFC1_Code, t1.APP1_COD)
  -- and t1.APP2_COD = ifnull (t3.NFC2_Code, t1.APP2_COD)
  -- and t1.APP3_COD = ifnull (t3.NFC3_Code, t1.APP3_COD)
  -- and t1.STGH_DES = ifnull (t3.Strength, t1.STGH_DES)
UNION ALL
select DISTINCT
  -- MARKET_PACK_KEY,
  PACK_COD,
  PACK_DES,
  STGH_DES,
  PACK_LCH,
  Family_Code,
  Family_Name,
  PROD_COD,
  PROD_DES,
  PROD_DES_C,
  CMPS_COD,
  CMPS_DES,
  CMPS_DES_C,
  ATC1_COD,
  ATC2_COD,
  ATC3_COD,
  ATC4_COD,
  APP1_COD,
  APP2_COD,
  APP3_COD,
  BIO_DESC,
  GENE_ORIG_DESC,
  ETH_OTC_DESC,
  NRDL_DESC,
  NRDL_Entry_Date,
  EDL_DESC,
  TCM_DESC,
  PAED_DESC,
  GQCE_DESC,
  VBP_DESC AS VBP_DESC_V,
  VBP_DESC,
  MANU_COD,
  MANU_DES,
  MANU_DES_C,
  MNFL_COD,
  MNFL_DES,
  CORP_COD,
  CORP_DES,
  CORP_DES_C,
  BrandType,
  -- MARKET,
  -- KEY_COMPETITOR,
  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
from
  FB_RAW_DATA as t
where
  not EXISTS (
    select
      0
    from
      dws.dws_ims_td_pack_property
    where
      PACK_COD = t.pack_cod
  )

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

-- CREATE OR REPLACE TABLE catalog_e2_ngbi_qas.dm.dm_ims_td_pack_property (
--   PACK_COD STRING,
--   PACK_DES STRING,
--   STGH_DES STRING,
--   PACK_LCH STRING,
--   Family_Code STRING,
--   Family_Name STRING,
--   PROD_COD STRING,
--   PROD_DES STRING,
--   PROD_DES_C STRING,
--   CMPS_COD STRING,
--   CMPS_DES STRING,
--   CMPS_DES_C STRING,
--   ATC1_COD STRING,
--   ATC2_COD STRING,
--   ATC3_COD STRING,
--   ATC4_COD STRING,
--   APP1_COD STRING,
--   APP2_COD STRING,
--   APP3_COD STRING,
--   BIO_DESC STRING,
--   GENE_ORIG_DESC STRING,
--   ETH_OTC_DESC STRING,
--   NRDL_DESC STRING,
--   NRDL_Entry_Date STRING,
--   EDL_DESC STRING,
--   TCM_DESC STRING,
--   PAED_DESC STRING,
--   GQCE_DESC STRING,
--   VBP_DESC_V STRING,
--   VBP_DESC STRING,
--   MANU_COD STRING,
--   MANU_DES STRING,
--   MANU_DES_C STRING,
--   MNFL_COD STRING,
--   MNFL_DES STRING,
--   CORP_COD STRING,
--   CORP_DES STRING,
--   CORP_DES_C STRING,
--   BrandType STRING,
--   IS_AZ STRING,
--   AZ_MAIN STRING,
--   atc1_des STRING,
--   atc1_des_c STRING,
--   atc2_des STRING,
--   atc2_des_c STRING,
--   atc3_des STRING,
--   atc3_des_c STRING,
--   atc4_des STRING,
--   atc4_des_c STRING,
--   app1_des STRING,
--   app1_des_c STRING,
--   app2_des STRING,
--   app2_des_c STRING,
--   app3_des STRING,
--   app3_des_c STRING,
--   ETL_INSERT_DT TIMESTAMP,
--   ETL_UPDATE_DT TIMESTAMP)
-- USING delta
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_ims_td_pack_property'

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

-- DBTITLE 1,overwrite dm.dm_ims_td_pack_property
insert overwrite table dm.dm_ims_td_pack_property
-- create or replace table dm.dm_ims_td_pack_property
select distinct
  -- t1.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.CMPS_DES_C as CMPS_DES_C, --20240530正式新增字段
  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_V as VBP_DESC_V, --20240530正式新增字段
  t1.VBP_DESC,
  t1.MANU_COD,
  t1.MANU_DES,
  t1.MANU_DES_C as MANU_DES_C, --20240530正式新增字段
  t1.MNFL_COD,
  t1.MNFL_DES,
  t1.CORP_COD,
  case
    when t1.CORP_DES like '% GROUP%' then replace (t1.CORP_DES, ' GROUP', '')
    else t1.CORP_DES
  end CORP_DES,
  t1.CORP_DES_C as CORP_DES_C, --20240530正式新增字段
  t1.BrandType,
  -- t1.MARKET,
  --六大市场即AZ 市场的key competitor=All Brands（PROD_DES）
  -------------------------------------------------------------------
  -- upper(t1.KEY_COMPETITOR) as KEY_COMPETITOR,
  ifnull (t2.IS_AZ, 'N') as IS_AZ,
  t1.AZ_MAIN,
  -- t1.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,
  -- ifnull (t1.class, 'Others') as Class,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
from
  CHPA_UNION_FB_DATA t1
  left join (
    select distinct
      prod_cod,
      prod_des,
      'Y' as IS_AZ
    from
      dws.dws_ims_td_pack_property
    where
      corp_cod = 'A5Z'
  ) t2 on t1.prod_des = t2.prod_des
-- where
--   t1.market <> 'Non Market'
--   --IMS ALL Market pack表属性补全
-- union all
-- select distinct
--   concat ('IMS ALL 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.CMPS_DES_C as CMPS_DES_C, --20240530正式新增字段
--   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_V as VBP_DESC_V, --20240530正式新增字段
--   t1.VBP_DESC,
--   t1.MANU_COD,
--   t1.MANU_DES,
--   t1.MANU_DES_C as MANU_DES_C, --20240530正式新增字段
--   t1.MNFL_COD,
--   t1.MNFL_DES,
--   t1.CORP_COD,
--   case
--     when t1.CORP_DES like '% GROUP%' then replace (t1.CORP_DES, ' GROUP', '')
--     else t1.CORP_DES
--   end CORP_DES,
--   t1.CORP_DES_C as CORP_DES_C, --20240530正式新增字段
--   t1.BrandType,
--   'IMS ALL Market' as MARKET,
--   upper(t1.PROD_DES) as KEY_COMPETITOR,
--   ifnull (t2.IS_AZ, 'N') as IS_AZ,
--   null as AZ_MAIN,
--   null as 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,
--   'Others' as Class,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
-- from
--   CHPA_UNION_FB_DATA t1
--   left join (
--     select distinct
--       prod_cod,
--       prod_des,
--       'Y' as IS_AZ
--     from
--       dws.dws_ims_td_pack_property
--     where
--       corp_cod = 'A5Z'
--   ) t2 on t1.prod_des = t2.prod_des

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

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

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

-- MAGIC %md
-- MAGIC # market ratio PDOT CU 

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

-- DBTITLE 1,UPDATE  dws.dws_ims_td_market
-- -- 补充 法伯部分的数据
-- insert into
--   table dws.dws_ims_td_market
-- with
--   FB_MARKET_RATIO as (
--     select distinct
--       market,
--       PACK_COD,
--       PACK_DES,
--       STGH_DES,
--       PACK_LCH,
--       PROD_COD,
--       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,
--       BrandType,
--       null as bu,
--       Market_Ratio,
--       Key_Competitor,
--       from_utc_timestamp (current_timestamp(), 'UTC+8'),
--       from_utc_timestamp (current_timestamp(), 'UTC+8')
--     from
--     from
--       FB_CLASS as t
--     where
--       not EXISTS (
--         select
--           0
--         from
--           dws.dws_ims_td_pack_property
--         where
--           PACK_COD = t.pack_cod
--       )
--   )
-- select
--   *
-- from
--   FB_MARKET_RATIO

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

-- DBTITLE 1,overwrite dws.dws_ims_td_market_ratio
--   --由于360中折算规则更加灵活，所以将市场的Ratio打开，分别对应三个不同的KPI字段
--   --此处会关联YM字段，原因是需要按照市场进行折算
--   --市场维度信息 挂上360中的折算规则 先用日期表做笛卡儿积，保证不同时间的规则可以关联上
--   insert overwrite table dws.dws_ims_td_market_ratio (
--     YM,
--     MARKET,
--     PACK_COD,
--     PACK_DES,
--     STGH_DES,
--     PACK_LCH,
--     PROD_COD,
--     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,
--     BrandType,
--     BU,
--     Key_Competitor,
--     Value_Market_Ratio,
--     Unit_Market_Ratio,
--     CountingUnit_Market_Ratio,
--     PDOT_Market_Ratio,
--     ETL_INSERT_DT,
--     ETL_UPDATE_DT
--   )
-- SELECT DISTINCT
--   YM.YM,
--   Market.MARKET,
--   Market.PACK_COD,
--   Market.PACK_DES,
--   Market.STGH_DES,
--   Market.PACK_LCH,
--   Market.PROD_COD,
--   Market.CMPS_COD,
--   Market.CMPS_DES,
--   Market.ATC1_COD,
--   Market.ATC2_COD,
--   Market.ATC3_COD,
--   Market.ATC4_COD,
--   Market.APP1_COD,
--   Market.APP2_COD,
--   Market.APP3_COD,
--   Market.BIO_DESC,
--   Market.GENE_ORIG_DESC,
--   Market.ETH_OTC_DESC,
--   Market.NRDL_DESC,
--   Market.NRDL_Entry_Date,
--   Market.EDL_DESC,
--   Market.TCM_DESC,
--   Market.PAED_DESC,
--   Market.GQCE_DESC,
--   Market.VBP_DESC,
--   Market.MANU_COD,
--   Market.MANU_DES,
--   Market.MNFL_COD,
--   Market.MNFL_DES,
--   Market.CORP_COD,
--   Market.CORP_DES,
--   Market.BrandType,
--   Market.BU,
--   Market.Key_Competitor,
--   Market.Market_Ratio AS Value_Market_Ratio,
--   Market.Market_Ratio AS Unit_Market_Ratio,
--   Market.Market_Ratio AS CountingUnit_Market_Ratio,
--   Market.Market_Ratio AS PDOT_Market_Ratio,
--   from_utc_timestamp (current_timestamp(), 'UTC+8'),
--   from_utc_timestamp (current_timestamp(), 'UTC+8')
-- FROM
--   dws.dws_ims_td_market Market
--   CROSS JOIN (
--     SELECT DISTINCT
--       cast(YM as int) as YM
--     FROM
--       tmp.tmp_ims_tf_fact_sales
--   ) YM
--   -- WHERE YM.YM >= (SELECT MIN(cast(YM as int)) FROM dws.dws_ims_td_date)
-- ;

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

-- DBTITLE 1,overwrite tmp.tmp_ims_td_market_ratio1
-- -----------------------------------------------------------------------------------------------------------------------------
-- --修改时间：20240820
-- --修改人：FanXujia
-- --修改内容：
-- --market_ratio的逻辑调整
-- --修改前：market_ratio(tblmarket) * ratio(tblbrandratio_pack) * ratio(tblbrandratio_MOLECULE) ，三者相乘得到最终的ratio
-- --修改后：market_ratio(tblmarket) * ratio(优先取pack级别的ratio，没有pack级别的就取molecule级别的ratio)，两者相乘得到最终的ratio
-- -----------------------------------------------------------------------------------------------------------------------------
-- ---按照折算的Type，分三个类型的Ratio进行折算 VALUE,UNIT,COUNTINGUNIT
-- insert overwrite table tmp.tmp_ims_td_market_ratio1
-- select distinct
--   t1.YM,
--   t1.MARKET,
--   t1.PACK_COD,
--   t1.PACK_DES,
--   t1.STGH_DES,
--   t1.PACK_LCH,
--   t1.PROD_COD,
--   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.BrandType,
--   t1.BU,
--   t1.Key_Competitor,
--   case
--     when t2.ratio is not null then t1.Value_Market_Ratio * t2.ratio
--     else t1.Value_Market_Ratio * nvl (t5.ratio, 1)
--   end as Value_Market_Ratio,
--   case
--     when t3.ratio is not null then t1.Unit_Market_Ratio * t3.ratio
--     else t1.Unit_Market_Ratio * nvl (t6.ratio, 1)
--   end as Unit_Market_Ratio,
--   case
--     when t4.ratio is not null then t1.CountingUnit_Market_Ratio * t4.ratio
--     else t1.CountingUnit_Market_Ratio * nvl (t7.ratio, 1)
--   end as CountingUnit_Market_Ratio,
--   t1.PDOT_Market_Ratio,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') as ETL_INSERT_DT,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') as ETL_UPDATE_DT
-- from
--   dws.dws_ims_td_market_ratio t1
--   left join ( --VALUE--PACK
--     select distinct
--       market,
--       cmps_cod,
--       cmps_des,
--       pack_cod,
--       pack_des,
--       level,
--       type,
--       cast(ratio as float) as ratio,
--       cast(starttime as int) starttime,
--       cast(endtime as int) endtime
--     from
--       dwd.dwd_gnd_ims_tblbrandratio
--     where
--       UPPER(TYPE) = 'VALUE'
--       and UPPER(Level) = 'PACK'
--   ) t2 on t1.market = t2.market
--   and t1.PACK_COD = t2.PACK_COD
--   and t1.YM >= t2.starttime
--   and t1.YM <= t2.endtime
--   left join ( --UNIT--PACK
--     select distinct
--       market,
--       cmps_cod,
--       cmps_des,
--       pack_cod,
--       pack_des,
--       level,
--       type,
--       cast(ratio as float) as ratio,
--       cast(starttime as int) starttime,
--       cast(endtime as int) endtime
--     from
--       dwd.dwd_gnd_ims_tblbrandratio
--     where
--       UPPER(TYPE) = 'UNIT'
--       and UPPER(Level) = 'PACK'
--   ) t3 on t1.market = t3.market
--   and t1.PACK_COD = t3.PACK_COD
--   and t1.YM >= t3.starttime
--   and t1.YM <= t3.endtime
--   left join ( --COUNTINGUNIT--PACK
--     select distinct
--       market,
--       cmps_cod,
--       cmps_des,
--       pack_cod,
--       pack_des,
--       level,
--       type,
--       cast(ratio as float) as ratio,
--       cast(starttime as int) starttime,
--       cast(endtime as int) endtime
--     from
--       dwd.dwd_gnd_ims_tblbrandratio
--     where
--       UPPER(TYPE) = 'COUNTINGUNIT'
--       and UPPER(Level) = 'PACK'
--   ) t4 on t1.market = t4.market
--   and t1.PACK_COD = t4.PACK_COD
--   and t1.YM >= t4.starttime
--   and t1.YM <= t4.endtime
--   left join ( --VALUE--MOLECULE
--     select distinct
--       market,
--       cmps_cod,
--       cmps_des,
--       pack_cod,
--       pack_des,
--       level,
--       type,
--       cast(ratio as float) as ratio,
--       cast(starttime as int) starttime,
--       cast(endtime as int) endtime
--     from
--       dwd.dwd_gnd_ims_tblbrandratio
--     where
--       UPPER(TYPE) = 'VALUE'
--       and UPPER(Level) = 'MOLECULE'
--   ) t5 on t1.market = t5.market
--   and t1.CMPS_COD = t5.CMPS_COD
--   and t1.YM >= t5.starttime
--   and t1.YM <= t5.endtime
--   left join ( --UNIT--MOLECULE
--     select distinct
--       market,
--       cmps_cod,
--       cmps_des,
--       pack_cod,
--       pack_des,
--       level,
--       type,
--       cast(ratio as float) as ratio,
--       cast(starttime as int) starttime,
--       cast(endtime as int) endtime
--     from
--       dwd.dwd_gnd_ims_tblbrandratio
--     where
--       UPPER(TYPE) = 'UNIT'
--       and UPPER(Level) = 'MOLECULE'
--   ) t6 on t1.market = t6.market
--   and t1.CMPS_COD = t6.CMPS_COD
--   and t1.YM >= t6.starttime
--   and t1.YM <= t6.endtime
--   left join ( --COUNTINGUNIT--MOLECULE
--     select distinct
--       market,
--       cmps_cod,
--       cmps_des,
--       pack_cod,
--       pack_des,
--       level,
--       type,
--       cast(ratio as float) as ratio,
--       cast(starttime as int) starttime,
--       cast(endtime as int) endtime
--     from
--       dwd.dwd_gnd_ims_tblbrandratio
--     where
--       UPPER(TYPE) = 'COUNTINGUNIT'
--       and UPPER(Level) = 'MOLECULE'
--   ) t7 on t1.market = t7.market
--   and t1.CMPS_COD = t7.CMPS_COD
--   and t1.YM >= t7.starttime
--   and t1.YM <= t7.endtime;

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

-- DBTITLE 1,overwrite dws.dws_ims_td_market_ratio
-- --看市场维度需要用到的Ratio 写入 DWS 备用
-- insert overwrite table dws.dws_ims_td_market_ratio (
--   YM,
--   MARKET,
--   PACK_COD,
--   PACK_DES,
--   STGH_DES,
--   PACK_LCH,
--   PROD_COD,
--   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,
--   BrandType,
--   BU,
--   Key_Competitor,
--   Value_Market_Ratio,
--   Unit_Market_Ratio,
--   CountingUnit_Market_Ratio,
--   PDOT_Market_Ratio,
--   ETL_INSERT_DT,
--   ETL_UPDATE_DT
-- )
-- select
--   t1.YM,
--   t1.MARKET,
--   t1.PACK_COD,
--   t1.PACK_DES,
--   t1.STGH_DES,
--   t1.PACK_LCH,
--   t1.PROD_COD,
--   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.BrandType,
--   t1.BU,
--   t1.Key_Competitor,
--   Value_Market_Ratio,
--   Unit_Market_Ratio,
--   CountingUnit_Market_Ratio,
--   PDOT_Market_Ratio,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') as ETL_INSERT_DT,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') as ETL_UPDATE_DT
-- from
--   tmp.tmp_ims_td_market_ratio1 t1