-- Databricks notebook source
-- DBTITLE 1,建表SQL
-- CREATE OR REPLACE TABLE dm.dm_ims_tf_sales (
--   YYYYMM STRING,
--   DAY_DATE STRING,
--   PACK_COD STRING,
--   AUDIT_COD STRING,
--   Related_flag STRING,
--   ORG_KEY STRING,
--   Market STRING,
--   MARKET_PACK_KEY STRING,
--   MARKET_AUDIT_KEY STRING,
--   SALES_VALUE DECIMAL(30,10),
--   SALES_VALUE_LY DECIMAL(30,10),
--   SALES_VALUE_L2Y DECIMAL(30,10),
--   SALES_UNIT DECIMAL(30,10),
--   SALES_UNIT_LY DECIMAL(30,10),
--   SALES_UNIT_L2Y DECIMAL(30,10),
--   CONUTING_UNIT DECIMAL(30,10),
--   CONUTING_UNIT_LY DECIMAL(30,10),
--   CONUTING_UNIT_L2Y DECIMAL(30,10),
--   PDOT DECIMAL(30,10),
--   PDOT_LY DECIMAL(30,10),
--   PDOT_L2Y DECIMAL(30,10),
--   SALES_VALUE_CAL DECIMAL(30,10),
--   SALES_VALUE_CAL_LY DECIMAL(30,10),
--   SALES_UNIT_CAL DECIMAL(30,10),
--   SALES_UNIT_CAL_LY DECIMAL(30,10),
--   ETL_INSERT_DT TIMESTAMP,
--   ETL_UPDATE_DT TIMESTAMP)
-- USING delta
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_ims_tf_sales';

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

-- DBTITLE 1,tmp_ims_tf_fact_sales_l2y
--提前算好前端需要两年前的数据
-- tmp.tmp_ims_tf_fact_sales 整张表对应的notebook path:/Workspace/Users/kthc245@azc.partner.onmschina.cn/20251125Rebuild MA/rebuildMA/DWS/dws_ims_tbl
CREATE
OR REPLACE TEMPORARY VIEW tmp_ims_tf_fact_sales_l2y AS
select
  YM,
  AUDIT_COD,
  PACK_COD,
  sum(MTH00LC) as MTH00LC,
  sum(MTH00LCLY) as MTH00LCLY,
  sum(MTH00LCL2Y) as MTH00LCL2Y,
  sum(MTH00CN) as MTH00CN,
  sum(MTH00CNLY) as MTH00CNLY,
  sum(MTH00CNL2Y) as MTH00CNL2Y,
  sum(MTH00UN) as MTH00UN,
  sum(MTH00UNLY) as MTH00UNLY,
  sum(MTH00UNL2Y) as MTH00UNL2Y
from
  (
    select
      YM,
      AUDIT_COD,
      PACK_COD,
      MTH00LC,
      MTH00LCLY,
      0 as MTH00LCL2Y,
      MTH00CN,
      MTH00CNLY,
      0 as MTH00CNL2Y,
      MTH00UN,
      MTH00UNLY,
      0 as MTH00UNL2Y
    from
      tmp.tmp_ims_tf_fact_sales
    union all
    select
      YM + 200 as YM,
      AUDIT_COD,
      PACK_COD,
      0 as MTH00LC,
      0 as MTH00LCLY,
      MTH00LC as MTH00LCL2Y,
      0 as MTH00CN,
      0 as MTH00CNLY,
      MTH00CN as MTH00CNL2Y,
      0 as MTH00UN,
      0 as MTH00UNLY,
      MTH00UN as MTH00UNL2Y
    from
      tmp.tmp_ims_tf_fact_sales
    where
      YM + 200 in (
        select distinct
          YM
        from
          tmp.tmp_ims_tf_fact_sales
      )
  )
group by
  YM,
  AUDIT_COD,
  PACK_COD

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

  -- DBTITLE 1,tmp_ims_tf_sales_total
  --提前算好需要IMS 折算的value和不折算的value
  CREATE
  OR REPLACE TEMPORARY VIEW tmp_ims_tf_sales_total AS
-- select distinct
--   t1.YM as YYYYMM,
--   t1.PACK_COD,
--   t1.AUDIT_COD,
--   'ALL' AS RELATE_FLAG,
--   t1.AUDIT_COD as ORG_KEY,
--   case
--     when t2.market is null then 'Non Market'
--     else t2.market
--   end Market,
--   case
--     when t2.market is null then concat ('Non Market_', t1.pack_cod)
--     else concat (t2.market, '_', t2.pack_cod)
--   end as MARKET_PACK_KEY,
--   case
--     when t2.market is null then concat ('Non Market_', t1.audit_cod)
--     else concat (t2.market, '_', t1.audit_cod)
--   end as MARKET_AUDIT_KEY,
--   t1.MTH00LC * (
--     case
--       when t2.Value_Market_Ratio is null then 1
--       else t2.Value_Market_Ratio
--     end
--   ) as SALES_VALUE,
--   t1.MTH00LCLY * (
--     case
--       when t2.Value_Market_Ratio is null then 1
--       else t2.Value_Market_Ratio
--     end
--   ) as SALES_VALUE_LY,
--   t1.MTH00LCL2Y * (
--     case
--       when t2.Value_Market_Ratio is null then 1
--       else t2.Value_Market_Ratio
--     end
--   ) as SALES_VALUE_L2Y,
--   t1.MTH00UN * (
--     case
--       when t2.Unit_Market_Ratio is null then 1
--       else t2.Unit_Market_Ratio
--     end
--   ) as SALES_UNIT,
--   t1.MTH00UNLY * (
--     case
--       when t2.Unit_Market_Ratio is null then 1
--       else t2.Unit_Market_Ratio
--     end
--   ) as SALES_UNIT_LY,
--   t1.MTH00UNL2Y * (
--     case
--       when t2.Unit_Market_Ratio is null then 1
--       else t2.Unit_Market_Ratio
--     end
--   ) as SALES_UNIT_L2Y,
--   t1.MTH00CN as CONUTING_UNIT,
--   t1.MTH00CNLY as CONUTING_UNIT_LY,
--   t1.MTH00CNL2Y as CONUTING_UNIT_L2Y,
--   t1.MTH00CN * (
--     case
--       when t2.CountingUnit_Market_Ratio is null then 1
--       else t2.CountingUnit_Market_Ratio
--     end
--   ) as PDOT,
--   t1.MTH00CNLY * (
--     case
--       when t2.CountingUnit_Market_Ratio is null then 1
--       else t2.CountingUnit_Market_Ratio
--     end
--   ) as PDOT_LY,
--   t1.MTH00CNL2Y * (
--     case
--       when t2.CountingUnit_Market_Ratio is null then 1
--       else t2.CountingUnit_Market_Ratio
--     end
--   ) as PDOT_L2Y,
--   t1.MTH00LC as SALES_VALUE_CAL,
--   t1.MTH00LCLY as SALES_VALUE_CAL_LY,
--   t1.MTH00LCL2Y as SALES_VALUE_CAL_L2Y,
--   t1.MTH00UN as SALES_UNIT_CAL,
--   t1.MTH00UNLY as SALES_UNIT_CAL_LY,
--   t1.MTH00UNL2Y as SALES_UNIT_CAL_L2Y,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') as ETL_INSERT_DT,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') as ETL_UPDATE_DT
-- from
--   tmp_ims_tf_fact_sales_l2y t1
--   left join (
--     select distinct
--       YM,
--       market,
--       pack_cod,
--       Value_Market_Ratio,
--       Unit_Market_Ratio,
--       CountingUnit_Market_Ratio
--     from
--       dws.dws_ims_td_market_ratio
--   ) t2 on t1.pack_cod = t2.pack_cod
--   and t1.YM = t2.YM
-- where
--   -- t1.YM in (select YYYYMM from dm.dm_ims_td_calendar)and 
--   t2.market is not null
-- union all
-- -- IMS ALL Market 为所有的IMS市场 不折算的数据的汇总，即不折算的所有市场的city粒度数据
select distinct
  t1.YM as YYYYMM,
  t1.PACK_COD,
  t1.AUDIT_COD,
  -- 'ALL' AS RELATE_FLAG,
  t1.AUDIT_COD as ORG_KEY,
  -- 'IMS ALL Market' Market,
  -- concat ('IMS ALL Market_', t1.pack_cod) as MARKET_PACK_KEY,
  -- concat ('IMS ALL Market_', t1.audit_cod) as MARKET_AUDIT_KEY,
  t1.MTH00LC SALES_VALUE,
  t1.MTH00LCLY SALES_VALUE_LY,
  t1.MTH00LCL2Y SALES_VALUE_L2Y,
  t1.MTH00UN SALES_UNIT,
  t1.MTH00UNLY SALES_UNIT_LY,
  t1.MTH00UNL2Y SALES_UNIT_L2Y,
  t1.MTH00CN CONUTING_UNIT,
  t1.MTH00CNLY CONUTING_UNIT_LY,
  t1.MTH00CNL2Y CONUTING_UNIT_L2Y,
  t1.MTH00CN PDOT,
  t1.MTH00CNLY PDOT_LY,
  t1.MTH00CNL2Y PDOT_L2Y,
  t1.MTH00LC SALES_VALUE_CAL,
  t1.MTH00LCLY SALES_VALUE_CAL_LY,
  t1.MTH00LCL2Y SALES_VALUE_CAL_L2Y,
  t1.MTH00UN SALES_UNIT_CAL,
  t1.MTH00UNLY SALES_UNIT_CAL_LY,
  t1.MTH00UNL2Y SALES_UNIT_CAL_L2Y,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
from
  tmp_ims_tf_fact_sales_l2y t1
  -- where t1.YM in (select YYYYMM from dm.dm_ims_td_calendar) 
;

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

-- DBTITLE 1,tmp_ims_tf_sales_tamarket
-- --在原本的数据上加上AZ Related Market，此为AZ 相关的六大市场数据汇总 
-- --以及年月日的日期格式标签
-- CREATE
-- OR REPLACE TEMPORARY VIEW tmp_ims_tf_sales_tamarket AS
-- select distinct
--   YYYYMM,
--   concat (left (YYYYMM, 4), '-', right (YYYYMM, 2), '-01') as DAY_DATE,
--   PACK_COD,
--   AUDIT_COD,
--   RELATE_FLAG,
--   ORG_KEY,
--   Market,
--   MARKET_PACK_KEY,
--   MARKET_AUDIT_KEY,
--   SALES_VALUE,
--   SALES_VALUE_LY,
--   SALES_VALUE_L2Y,
--   SALES_UNIT,
--   SALES_UNIT_LY,
--   SALES_UNIT_L2Y,
--   CONUTING_UNIT,
--   CONUTING_UNIT_LY,
--   CONUTING_UNIT_L2Y,
--   PDOT,
--   PDOT_LY,
--   PDOT_L2Y,
--   SALES_VALUE_CAL,
--   SALES_VALUE_CAL_LY,
--   SALES_VALUE_CAL_L2Y,
--   SALES_UNIT_CAL,
--   SALES_UNIT_CAL_LY,
--   SALES_UNIT_CAL_L2Y,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
-- from
--   tmp_ims_tf_sales_total

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

  -- DBTITLE 1,插入逻辑1-需保留
  -- dm.dm_ims_tf_sales_china 和 dm.dm_ims_tf_sales 区别就在于  audit_cod = 'CHT' 或者 t1.audit_cod <> 'CHT'
  -- insert overwrite table  dm.dm_ims_tf_sales_china
  -- from tmp_ims_tf_sales_tamarket t1
  -- where t1.audit_cod = 'CHT' and (sales_value<>0 or sales_value_ly<>0) and t1.market not in ('Primary Prevention Market-XZK','Iressa 1L TKI Market'  )
  -- union all
  -- from tmp_ims_tf_sales_tamarket t1
  -- where t1.audit_cod = 'CHT' and (sales_value<>0 or sales_value_ly<>0) and t1.market in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')
  -- insert overwrite table  dm.dm_ims_tf_sales
  -- from tmp_ims_tf_sales_tamarket t1
  -- where t1.audit_cod <> 'CHT' and t1.market not in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')and (sales_value<>0 or sales_value_ly<>0)
  -- union all
  -- from tmp_ims_tf_sales_tamarket t1
  -- where t1.audit_cod <> 'CHT' and t1.market in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')and (sales_value<>0 or sales_value_ly<>0)

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

-- CREATE TABLE catalog_e2_ngbi_qas.dm.dm_ims_tf_sales_china (
--   YYYYMM INT,
--   PACK_COD STRING,
--   AUDIT_COD STRING,
--   SALES_VALUE DECIMAL(38,10),
--   SALES_VALUE_LY DECIMAL(38,10),
--   SALES_VALUE_L2Y DECIMAL(38,10),
--   SALES_UNIT DECIMAL(38,10),
--   SALES_UNIT_LY DECIMAL(38,10),
--   SALES_UNIT_L2Y DECIMAL(38,10),
--   CONUTING_UNIT DECIMAL(38,10),
--   CONUTING_UNIT_LY DECIMAL(38,10),
--   CONUTING_UNIT_L2Y DECIMAL(38,10),
--   PDOT DECIMAL(38,10),
--   PDOT_LY DECIMAL(38,10),
--   PDOT_L2Y DECIMAL(38,10),
--   SALES_VALUE_CAL DECIMAL(38,10),
--   SALES_VALUE_CAL_LY DECIMAL(38,10),
--   SALES_UNIT_CAL DECIMAL(38,10),
--   SALES_UNIT_CAL_LY DECIMAL(38,10),
--   ETL_INSERT_DT TIMESTAMP,
--   ETL_UPDATE_DT TIMESTAMP)
-- USING delta
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_ims_tf_sales_china'

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

  -- DBTITLE 1,overwrite dm.dm_ims_tf_sales_china
  --SALES
  --CHINA CHPA数据
  -- t1.market not in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')
  -- ,CONUTING_UNIT
  -- ,CONUTING_UNIT_LY
  -- ,CONUTING_UNIT_L2Y
  -- ,SALES_VALUE_CAL
  -- ,SALES_VALUE_CAL_LY
  -- ,SALES_UNIT_CAL
  -- ,SALES_UNIT_CAL_LY
  -- from tmp_ims_tf_sales_tamarket t1
  -- where t1.audit_cod = 'CHT' and (sales_value<>0 or sales_value_ly<>0) and t1.market not in ('Primary Prevention Market-XZK','Iressa 1L TKI Market'  )
  -- t1.market in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')
  -- PDOT as CONUTING_UNIT
  -- ,PDOT_LY as CONUTING_UNIT_LY
  -- ,PDOT_L2Y as CONUTING_UNIT_L2Y
  -- ,SALES_VALUE as SALES_VALUE_CAL
  -- ,SALES_VALUE_LY as SALES_VALUE_CAL_LY
  -- ,SALES_UNIT as SALES_UNIT_CAL
  -- ,SALES_UNIT_LY as SALES_UNIT_CAL_LY
  -- from tmp_ims_tf_sales_tamarket t1
  -- where t1.audit_cod = 'CHT' and (sales_value<>0 or sales_value_ly<>0) and t1.market in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')
  -- create or replace table dm.dm_ims_tf_sales_china
  insert overwrite table dm.dm_ims_tf_sales_china
select distinct
  YYYYMM,
  -- DAY_DATE,
  PACK_COD,
  AUDIT_COD,
  -- RELATE_FLAG,
  -- ORG_KEY,
  -- Market,
  -- MARKET_PACK_KEY,
  -- MARKET_AUDIT_KEY,
  SALES_VALUE,
  SALES_VALUE_LY,
  SALES_VALUE_L2Y,
  SALES_UNIT,
  SALES_UNIT_LY,
  SALES_UNIT_L2Y,
  CONUTING_UNIT,
  CONUTING_UNIT_LY,
  CONUTING_UNIT_L2Y,
  PDOT,
  PDOT_LY,
  PDOT_L2Y,
  SALES_VALUE_CAL,
  SALES_VALUE_CAL_LY,
  SALES_UNIT_CAL,
  SALES_UNIT_CAL_LY,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
from
  tmp_ims_tf_sales_total t1
where
  t1.audit_cod = 'CHT'
  and (
    sales_value <> 0
    or sales_value_ly <> 0
  )
  -- and t1.market not in (
  --   'Primary Prevention Market-XZK',
  --   'Iressa 1L TKI Market'
  -- )
-- union all
-- select distinct
--   YYYYMM,
--   DAY_DATE,
--   PACK_COD,
--   AUDIT_COD,
--   RELATE_FLAG,
--   ORG_KEY,
--   Market,
--   MARKET_PACK_KEY,
--   MARKET_AUDIT_KEY,
--   SALES_VALUE,
--   SALES_VALUE_LY,
--   SALES_VALUE_L2Y,
--   SALES_UNIT,
--   SALES_UNIT_LY,
--   SALES_UNIT_L2Y,
--   PDOT as CONUTING_UNIT,
--   PDOT_LY as CONUTING_UNIT_LY,
--   PDOT_L2Y as CONUTING_UNIT_L2Y,
--   PDOT,
--   PDOT_LY,
--   PDOT_L2Y,
--   SALES_VALUE as SALES_VALUE_CAL,
--   SALES_VALUE_LY as SALES_VALUE_CAL_LY,
--   SALES_UNIT as SALES_UNIT_CAL,
--   SALES_UNIT_LY as SALES_UNIT_CAL_LY,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
-- from
--   tmp_ims_tf_sales_tamarket t1
-- where
--   t1.audit_cod = 'CHT'
--   and (
--     sales_value <> 0
--     or sales_value_ly <> 0
--   )
--   and t1.market in (
--     'Primary Prevention Market-XZK',
--     'Iressa 1L TKI Market'
--   )

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

-- CREATE or REPLACE TABLE dm.dm_ims_tf_sales (
--   YYYYMM INT,
--   PACK_COD STRING,
--   AUDIT_COD STRING,
--   SALES_VALUE DECIMAL(38,10),
--   SALES_VALUE_LY DECIMAL(38,10),
--   SALES_VALUE_L2Y DECIMAL(38,10),
--   SALES_UNIT DECIMAL(38,10),
--   SALES_UNIT_LY DECIMAL(38,10),
--   SALES_UNIT_L2Y DECIMAL(38,10),
--   CONUTING_UNIT DECIMAL(38,10),
--   CONUTING_UNIT_LY DECIMAL(38,10),
--   CONUTING_UNIT_L2Y DECIMAL(38,10),
--   PDOT DECIMAL(38,10),
--   PDOT_LY DECIMAL(38,10),
--   PDOT_L2Y DECIMAL(38,10),
--   SALES_VALUE_CAL DECIMAL(38,10),
--   SALES_VALUE_CAL_LY DECIMAL(38,10),
--   SALES_UNIT_CAL DECIMAL(38,10),
--   SALES_UNIT_CAL_LY DECIMAL(38,10),
--   ETL_INSERT_DT TIMESTAMP,
--   ETL_UPDATE_DT TIMESTAMP)
-- USING delta
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_ims_tf_sales'

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

  -- DBTITLE 1,overwrite dm.dm_ims_tf_sales
  --SALES
  --CITY 
  insert overwrite table dm.dm_ims_tf_sales
select distinct
  YYYYMM,
  -- DAY_DATE,
  PACK_COD,
  AUDIT_COD,
  -- RELATE_FLAG,
  -- ORG_KEY,
  -- Market,
  -- MARKET_PACK_KEY,
  -- MARKET_AUDIT_KEY,
  SALES_VALUE,
  SALES_VALUE_LY,
  SALES_VALUE_L2Y,
  SALES_UNIT,
  SALES_UNIT_LY,
  SALES_UNIT_L2Y,
  CONUTING_UNIT,
  CONUTING_UNIT_LY,
  CONUTING_UNIT_L2Y,
  PDOT,
  PDOT_LY,
  PDOT_L2Y,
  SALES_VALUE_CAL,
  SALES_VALUE_CAL_LY,
  SALES_UNIT_CAL,
  SALES_UNIT_CAL_LY,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
from
  tmp_ims_tf_sales_total t1
where
  t1.audit_cod <> 'CHT'
  -- and t1.market not in (
  --   'Primary Prevention Market-XZK',
  --   'Iressa 1L TKI Market'
  -- )
  and (
    sales_value <> 0
    or sales_value_ly <> 0
  )
-- union all
-- --'Primary Prevention Market-XZK','Iressa 1L TKI Market' 两个市场特殊处理，所有的value都是折算过的
-- select distinct
--   YYYYMM,
--   DAY_DATE,
--   PACK_COD,
--   AUDIT_COD,
--   RELATE_FLAG,
--   ORG_KEY,
--   Market,
--   MARKET_PACK_KEY,
--   MARKET_AUDIT_KEY,
--   SALES_VALUE,
--   SALES_VALUE_LY,
--   SALES_VALUE_L2Y,
--   SALES_UNIT,
--   SALES_UNIT_LY,
--   SALES_UNIT_L2Y,
--   PDOT as CONUTING_UNIT,
--   PDOT_LY as CONUTING_UNIT_LY,
--   PDOT_L2Y as CONUTING_UNIT_L2Y,
--   PDOT,
--   PDOT_LY,
--   PDOT_L2Y,
--   SALES_VALUE as SALES_VALUE_CAL,
--   SALES_VALUE_LY as SALES_VALUE_CAL_LY,
--   SALES_UNIT as SALES_UNIT_CAL,
--   SALES_UNIT_LY as SALES_UNIT_CAL_LY,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
--   from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
-- from
--   tmp_ims_tf_sales_tamarket t1
-- where
--   t1.audit_cod <> 'CHT'
--   -- and t1.market in (
--   --   'Primary Prevention Market-XZK',
--   --   'Iressa 1L TKI Market'
--   -- )
--   -- and (
--   --   sales_value <> 0
--   --   or sales_value_ly <> 0
--   -- )

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

  -- DBTITLE 1,插入逻辑2-需保留
  -- -- 关键词
  -- audit_cod <> 'CHT' -》 dm.dm_ims_tf_sales
  -- audit_cod = 'CHT' -》dm.dm_ims_tf_sales_china
  -- KCT 所有城市的IMS数据 基于 dm.dm_ims_tf_sales
  -- ROC 其他城市的IMS数据计算 ROC = CHT-KCT

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

  -- DBTITLE 1,overwrite tmp.tmp_ims_tf_sales_kct
  -- 所有城市汇总的IMS数据计算 KCT=sum(all city)
  -- create or replace table tmp.tmp_ims_tf_sales_kct
  insert overwrite table tmp.tmp_ims_tf_sales_kct
select
  YYYYMM,
  PACK_COD,
  'KCT' as AUDIT_COD,
  -- Related_flag,
  -- 'KCT' as ORG_KEY,
  -- Market,
  -- MARKET_PACK_KEY,
  -- concat (Market, '_KCT') as MARKET_AUDIT_KEY,
  sum(SALES_VALUE) as SALES_VALUE,
  sum(SALES_VALUE_LY) as SALES_VALUE_LY,
  sum(SALES_VALUE_L2Y) as SALES_VALUE_L2Y,
  sum(SALES_UNIT) as SALES_UNIT,
  sum(SALES_UNIT_LY) as SALES_UNIT_LY,
  sum(SALES_UNIT_L2Y) as SALES_UNIT_L2Y,
  sum(CONUTING_UNIT) as CONUTING_UNIT,
  sum(CONUTING_UNIT_LY) as CONUTING_UNIT_LY,
  sum(CONUTING_UNIT_L2Y) as CONUTING_UNIT_L2Y,
  sum(PDOT) as PDOT,
  sum(PDOT_LY) as PDOT_LY,
  sum(PDOT_LY) as PDOT_L2Y,
  sum(SALES_VALUE_CAL) as SALES_VALUE_CAL,
  sum(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
  sum(SALES_UNIT_CAL) as SALES_UNIT_CAL,
  sum(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY
from
  dm.dm_ims_tf_sales
group by
  YYYYMM,
  PACK_COD
  -- Related_flag,
  -- Market,
  -- MARKET_PACK_KEY;

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

-- DBTITLE 1,overwrite tmp.tmp_ims_tf_sales_roc
-- 其他城市的IMS数据计算 ROC=CHT-KCT
insert overwrite table tmp.tmp_ims_tf_sales_roc
WITH
  DIM_YM_PACK_MARKT (
    SELECT
      YYYYMM as CHINA_YYYYMM,
      PACK_COD as CHINA_PACK_COD
      -- ,CHINA_Market
    from
      dm.dm_ims_tf_sales_china
    group by
      YYYYMM,
      PACK_COD
      -- ,CHINA_Market
    UNION
    SELECT
      YYYYMM,
      PACK_COD
      -- ,Market
    from
      tmp.tmp_ims_tf_sales_kct
    group by
      YYYYMM,
      PACK_COD
      -- ,Market
  ),
  CHPA_SALES (
    select
      YYYYMM,
      PACK_COD,
      -- CHINA_Market,
      sum(SALES_VALUE) as CHINA_SALES_VALUE,
      sum(SALES_VALUE_LY) as CHINA_SALES_VALUE_LY,
      sum(SALES_VALUE_L2Y) as CHINA_SALES_VALUE_L2Y,
      sum(SALES_UNIT) as CHINA_SALES_UNIT,
      sum(SALES_UNIT_LY) as CHINA_SALES_UNIT_LY,
      sum(SALES_UNIT_L2Y) as CHINA_SALES_UNIT_L2Y,
      sum(CONUTING_UNIT) as CHINA_CONUTING_UNIT,
      sum(CONUTING_UNIT_LY) as CHINA_CONUTING_UNIT_LY,
      sum(CONUTING_UNIT_L2Y) as CHINA_CONUTING_UNIT_L2Y,
      sum(PDOT) as CHINA_PDOT,
      sum(PDOT_LY) as CHINA_PDOT_LY,
      sum(PDOT_L2Y) as CHINA_PDOT_L2Y,
      sum(SALES_VALUE_CAL) as SALES_VALUE_CAL,
      sum(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
      sum(SALES_UNIT_CAL) as SALES_UNIT_CAL,
      sum(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY
    from
      dm.dm_ims_tf_sales_china
    group by
      YYYYMM,
      PACK_COD
      -- ,CHINA_Market
  ),
  FABO_SALES (
    select
      YYYYMM,
      PACK_COD,
      -- Market,
      sum(SALES_VALUE) as SALES_VALUE,
      sum(SALES_VALUE_LY) as SALES_VALUE_LY,
      sum(SALES_VALUE_L2Y) as SALES_VALUE_L2Y,
      sum(SALES_UNIT) as SALES_UNIT,
      sum(SALES_UNIT_LY) as SALES_UNIT_LY,
      sum(SALES_UNIT_L2Y) as SALES_UNIT_L2Y,
      sum(CONUTING_UNIT) as CONUTING_UNIT,
      sum(CONUTING_UNIT_LY) as CONUTING_UNIT_LY,
      sum(CONUTING_UNIT_L2Y) as CONUTING_UNIT_L2Y,
      sum(PDOT) as PDOT,
      sum(PDOT_LY) as PDOT_LY,
      sum(PDOT_L2Y) as PDOT_L2Y,
      sum(SALES_VALUE_CAL) as SALES_VALUE_CAL,
      sum(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
      sum(SALES_UNIT_CAL) as SALES_UNIT_CAL,
      sum(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY
    from
      tmp.tmp_ims_tf_sales_kct
    group by
      YYYYMM,
      PACK_COD
      -- ,Market
  )
SELECT
  t1.CHINA_YYYYMM as YYYYMM,
  t1.CHINA_PACK_COD as PACK_COD,
  'ROC' as AUDIT_COD,
  -- null as Related_flag,
  -- 'ROC' as ORG_KEY,
  -- t1.CHINA_Market as Market,
  -- concat (t1.CHINA_Market, '_', t1.china_pack_cod) as MARKET_PACK_KEY,
  -- concat (t1.CHINA_Market, '_ROC') as MARKET_AUDIT_KEY,
  nvl (t2.CHINA_SALES_VALUE, 0) - nvl (t3.SALES_VALUE, 0) as SALES_VALUE,
  nvl (t2.CHINA_SALES_VALUE_LY, 0) - nvl (t3.SALES_VALUE_LY, 0) as SALES_VALUE_LY,
  nvl (t2.CHINA_SALES_VALUE_L2Y, 0) - nvl (t3.SALES_VALUE_L2Y, 0) as SALES_VALUE_L2Y,
  nvl (t2.CHINA_SALES_UNIT, 0) - nvl (t3.SALES_UNIT, 0) as SALES_UNIT,
  nvl (t2.CHINA_SALES_UNIT_L2Y, 0) - nvl (t3.SALES_UNIT_L2Y, 0) as SALES_UNIT_LY,
  nvl (t2.CHINA_SALES_UNIT_LY, 0) - nvl (t3.SALES_UNIT_LY, 0) as SALES_UNIT_L2Y,
  nvl (t2.CHINA_CONUTING_UNIT, 0) - nvl (t3.CONUTING_UNIT, 0) as CONUTING_UNIT,
  nvl (t2.CHINA_CONUTING_UNIT_LY, 0) - nvl (t3.CONUTING_UNIT_LY, 0) as CONUTING_UNIT_LY,
  nvl (t2.CHINA_CONUTING_UNIT_L2Y, 0) - nvl (t3.CONUTING_UNIT_L2Y, 0) as CONUTING_UNIT_L2Y,
  nvl (t2.CHINA_PDOT, 0) - nvl (t3.PDOT, 0) as PDOT,
  nvl (t2.CHINA_PDOT_LY, 0) - nvl (t3.PDOT_LY, 0) as PDOT_LY,
  nvl (t2.CHINA_PDOT_L2Y, 0) - nvl (t3.PDOT_L2Y, 0) as PDOT_L2Y,
  nvl (t2.SALES_VALUE_CAL, 0) - nvl (t3.SALES_VALUE_CAL, 0) as SALES_VALUE_CAL,
  nvl (t2.SALES_VALUE_CAL_LY, 0) - nvl (t3.SALES_VALUE_CAL_LY, 0) as SALES_VALUE_CAL_LY,
  nvl (t2.SALES_UNIT_CAL, 0) - nvl (t3.SALES_UNIT_CAL, 0) as SALES_UNIT_CAL,
  nvl (t2.SALES_UNIT_CAL_LY, 0) - nvl (t3.SALES_UNIT_CAL_LY, 0) as SALES_UNIT_CAL_LY
FROM
  dim_ym_pack_markt t1
  LEFT JOIN chpa_sales t2 on 
  -- t1.china_market = t2.china_market   and 
  t1.china_yyyymm = t2.yyyymm
  and t1.china_pack_cod = t2.pack_cod
  LEFT JOIN fabo_sales t3 on 
  -- t1.china_market = t3.market  and 
  t1.china_yyyymm = t3.yyyymm
  and t1.china_pack_cod = t3.pack_cod

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

  -- DBTITLE 1,insert into dm.dm_ims_tf_sales
  --将ROC的数据写入dm.dm_ims_tf_sales
insert into
  dm.dm_ims_tf_sales
select distinct
  YYYYMM,
  -- concat (left (YYYYMM, 4), '-', right (YYYYMM, 2), '-01') as DAY_DATE,
  PACK_COD,
  AUDIT_COD,
  -- RELATED_FLAG,
  -- ORG_KEY,
  -- Market,
  -- MARKET_PACK_KEY,
  -- MARKET_AUDIT_KEY,
  SALES_VALUE,
  SALES_VALUE_LY,
  SALES_VALUE_L2Y,
  SALES_UNIT,
  SALES_UNIT_LY,
  SALES_UNIT_L2Y,
  CONUTING_UNIT,
  CONUTING_UNIT_LY,
  CONUTING_UNIT_L2Y,
  PDOT,
  PDOT_LY,
  PDOT_L2Y,
  SALES_VALUE_CAL,
  SALES_VALUE_CAL_LY,
  SALES_UNIT_CAL,
  SALES_UNIT_CAL_LY,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
from
  tmp.tmp_ims_tf_sales_roc
where
  sales_value <> 0
  or sales_value_ly <> 0;