-- Databricks notebook source
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_RETAIL_PACK2MARKET (
--   MARKET STRING,
--   PACK_CODE STRING,
--   PACK_DESC STRING,
--   STGH_DESC STRING,
--   PACK_LCH STRING,
--   PROD_CODE STRING,
--   CMPS_CODE STRING,
--   CMPS_DESC STRING,
--   ATC1_CODE STRING,
--   ATC2_CODE STRING,
--   ATC3_CODE STRING,
--   ATC4_CODE STRING,
--   APP1_CODE STRING,
--   APP2_CODE STRING,
--   APP3_CODE 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 STRING,
--   MANU_CODE STRING,
--   MANU_DESC STRING,
--   MNFL_CODE STRING,
--   MNFL_DESC STRING,
--   CORP_CODE STRING,
--   CORP_DESC STRING,
--   BRANDTYPE STRING,
--   BU STRING,
--   STARTTIME STRING,
--   ENDTIME STRING,
--   MARKET_RATIO STRING,
--   ETL_INSERT_DT TIMESTAMP,
--   ETL_UPDATE_DT TIMESTAMP)
-- USING delta
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_retail_pack2market';
-- -- 上面是生产环境location，下面是测试环境location
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_retail_pack2market';

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

CREATE OR REPLACE TEMPORARY VIEW VIEW_EXT_RETAIL
AS
-- 包含规则
SELECT DISTINCT
  T2.MARKET,
  T1.PACK_CODE,
  T1.PACK_DESC,
  T1.STGH_DESC,
  T1.PACK_LCH,
  T1.PROD_CODE,
  T1.CMPS_CODE,
  T1.CMPS_DESC,
  T1.ATC1_CODE,
  T1.ATC2_CODE,
  T1.ATC3_CODE,
  T1.ATC4_CODE,
  T1.APP1_CODE,
  T1.APP2_CODE,
  T1.APP3_CODE,
  T1.BIO_DESC,
  T1.GENE_ORIG_DESC AS GENE_ORIG,
  T1.ETH_OTC_DESC AS ETH_OTC_D,
  T1.NRDL_DESC,
  T1.NRDL_ENTRY_DATE AS NRDL_ENTR,
  T1.EDL_DESC,
  T1.TCM_DESC,
  T1.PAED_DESC,
  T1.GQCE_DESC,
  T1.VBP_DESC,
  T1.MANU_CODE,
  T1.MANU_DESC,
  T1.MNFL_CODE,
  T1.MNFL_DESC,
  T1.CORP_CODE,
  T1.CORP_DESC,
  T1.BRANDTYPE,
  T2.BU,
  T2.STARTTIME,
  T2.ENDTIME,
  COALESCE(T2.EXTEND_MARKET_RATIO, '1') AS MARKET_RATIO
FROM DM.DM_TD_EXT_RETAIL_PACK_PROPERTY AS T1
LEFT JOIN (
  SELECT 
    MARKET,
    BU,
    STARTTIME,
    ENDTIME,
    EXTEND_MARKET_RATIO,
    ATC1_CODE,
    ATC2_CODE,
    ATC3_CODE,
    ATC4_CODE,
    NFC1_CODE,
    NFC2_CODE,
    NFC3_CODE,
    CORPORATION_CODE,
    MANUFACTURER_CODE,
    PRODUCT_CODE,
    PACK_CODE,
    STRENGTH,
    MOLECULE_CODE
  FROM DWD.dwd_gnd_ext_retail_tblmarket_bymonth
  WHERE EXTEND_MARKET IS NULL AND (NOT_IN_FLAG IS NULL OR NOT_IN_FLAG = '1')
) AS T2
  ON (T2.ATC1_CODE IS NULL OR T1.ATC1_CODE = T2.ATC1_CODE)
  AND (T2.ATC2_CODE IS NULL OR T1.ATC2_CODE = T2.ATC2_CODE)
  AND (T2.ATC3_CODE IS NULL OR T1.ATC3_CODE = T2.ATC3_CODE)
  AND (T2.ATC4_CODE IS NULL OR T1.ATC4_CODE = T2.ATC4_CODE)
  AND (T2.NFC1_CODE IS NULL OR T1.APP1_CODE = T2.NFC1_CODE)
  AND (T2.NFC2_CODE IS NULL OR T1.APP2_CODE = T2.NFC2_CODE)
  AND (T2.NFC3_CODE IS NULL OR T1.APP3_CODE = T2.NFC3_CODE)
  AND (T2.CORPORATION_CODE IS NULL OR T1.CORP_CODE = T2.CORPORATION_CODE)
  AND (T2.MANUFACTURER_CODE IS NULL OR T1.MANU_CODE = T2.MANUFACTURER_CODE)
  AND (T2.PRODUCT_CODE IS NULL OR T1.PROD_CODE = T2.PRODUCT_CODE)
  AND (T2.PACK_CODE IS NULL OR T1.PACK_CODE = T2.PACK_CODE)
  AND (T2.STRENGTH IS NULL OR T1.STGH_DESC = T2.STRENGTH)
  AND (T2.MOLECULE_CODE IS NULL OR T1.CMPS_CODE = T2.MOLECULE_CODE)
WHERE T2.MARKET IS NOT NULL

EXCEPT

-- 排除规则
SELECT DISTINCT
  T2.MARKET,
  T1.PACK_CODE,
  T1.PACK_DESC,
  T1.STGH_DESC,
  T1.PACK_LCH,
  T1.PROD_CODE,
  T1.CMPS_CODE,
  T1.CMPS_DESC,
  T1.ATC1_CODE,
  T1.ATC2_CODE,
  T1.ATC3_CODE,
  T1.ATC4_CODE,
  T1.APP1_CODE,
  T1.APP2_CODE,
  T1.APP3_CODE,
  T1.BIO_DESC,
  T1.GENE_ORIG_DESC AS GENE_ORIG,
  T1.ETH_OTC_DESC AS ETH_OTC_D,
  T1.NRDL_DESC,
  T1.NRDL_ENTRY_DATE AS NRDL_ENTR,
  T1.EDL_DESC,
  T1.TCM_DESC,
  T1.PAED_DESC,
  T1.GQCE_DESC,
  T1.VBP_DESC,
  T1.MANU_CODE,
  T1.MANU_DESC,
  T1.MNFL_CODE,
  T1.MNFL_DESC,
  T1.CORP_CODE,
  T1.CORP_DESC,
  T1.BRANDTYPE,
  T2.BU,
  T2.STARTTIME,
  T2.ENDTIME,
  COALESCE(T2.EXTEND_MARKET_RATIO, '1') AS MARKET_RATIO
FROM DM.DM_TD_EXT_RETAIL_PACK_PROPERTY AS T1
LEFT JOIN (
  SELECT 
    MARKET,
    BU,
    STARTTIME,
    ENDTIME,
    EXTEND_MARKET_RATIO,
    ATC1_CODE,
    ATC2_CODE,
    ATC3_CODE,
    ATC4_CODE,
    NFC1_CODE,
    NFC2_CODE,
    NFC3_CODE,
    CORPORATION_CODE,
    MANUFACTURER_CODE,
    PRODUCT_CODE,
    PACK_CODE,
    STRENGTH,
    MOLECULE_CODE  
  FROM DWD.dwd_gnd_ext_retail_tblmarket_bymonth
  WHERE EXTEND_MARKET IS NULL AND NOT_IN_FLAG = '0'
) AS T2
  ON (T2.ATC1_CODE IS NULL OR T1.ATC1_CODE = T2.ATC1_CODE)
  AND (T2.ATC2_CODE IS NULL OR T1.ATC2_CODE = T2.ATC2_CODE)
  AND (T2.ATC3_CODE IS NULL OR T1.ATC3_CODE = T2.ATC3_CODE)
  AND (T2.ATC4_CODE IS NULL OR T1.ATC4_CODE = T2.ATC4_CODE)
  AND (T2.NFC1_CODE IS NULL OR T1.APP1_CODE = T2.NFC1_CODE)
  AND (T2.NFC2_CODE IS NULL OR T1.APP2_CODE = T2.NFC2_CODE)
  AND (T2.NFC3_CODE IS NULL OR T1.APP3_CODE = T2.NFC3_CODE)
  AND (T2.CORPORATION_CODE IS NULL OR T1.CORP_CODE = T2.CORPORATION_CODE)
  AND (T2.MANUFACTURER_CODE IS NULL OR T1.MANU_CODE = T2.MANUFACTURER_CODE)
  AND (T2.PRODUCT_CODE IS NULL OR T1.PROD_CODE = T2.PRODUCT_CODE)
  AND (T2.PACK_CODE IS NULL OR T1.PACK_CODE = T2.PACK_CODE)
  AND (T2.STRENGTH IS NULL OR T1.STGH_DESC = T2.STRENGTH)
  AND (T2.MOLECULE_CODE IS NULL OR T1.CMPS_CODE = T2.MOLECULE_CODE)
WHERE T2.MARKET IS NOT NULL;

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

INSERT OVERWRITE TABLE DM.DM_TD_EXT_RETAIL_PACK2MARKET
SELECT
  MARKET,
  PACK_CODE,
  PACK_DESC,
  STGH_DESC,
  PACK_LCH,
  PROD_CODE,
  CMPS_CODE,
  CMPS_DESC,
  ATC1_CODE,
  ATC2_CODE,
  ATC3_CODE,
  ATC4_CODE,
  APP1_CODE,
  APP2_CODE,
  APP3_CODE,
  BIO_DESC,
  GENE_ORIG,
  ETH_OTC_D,
  NRDL_DESC,
  NRDL_ENTR,
  EDL_DESC,
  TCM_DESC,
  PAED_DESC,
  GQCE_DESC,
  VBP_DESC,
  MANU_CODE,
  MANU_DESC,
  MNFL_CODE,
  MNFL_DESC,
  CORP_CODE,
  CORP_DESC,
  BRANDTYPE,
  BU,
  STARTTIME,
  ENDTIME,
  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 VIEW_EXT_RETAIL