-- Databricks notebook source
-- MAGIC %md
-- MAGIC ## 清空表

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

TRUNCATE TABLE dm.dm_tf_external_sales

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

-- MAGIC %md
-- MAGIC ## 各渠道逻辑

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

-- DBTITLE 1,CHPA
-- DELETE FROM
--   dm.dm_tf_external_sales
-- WHERE
--   DATA_SOURCE = 'IQVIA-CHPA(Monthly)';

INSERT INTO dm.dm_tf_external_sales (
    YYYYMM,
    PACK_COD,
    AUDIT_COD,
    DATA_SOURCE,
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    CONUTING_UNIT,
    CONUTING_UNIT_LY,
    prescription,
    prescription_ly,
    PACK_FLAG,
    PROD_FLAG,
    DTP_FLAG,
    cmps_flag,
    PLATFORM_TYPE,
    STORE_NAME,
    STORE_TYPE,
    REGION_TYPE,
    inst_code,
    dept_name,
    new_code,
    area,
    h_level,
    reimburse,
    reimburse_type,
    prescription_source,
    CORP_COD
  )
  SELECT
    A.YYYYMM,
    A.PACK_COD,
    A.AUDIT_COD,
    'IQVIA-CHPA(Monthly)' DATA_SOURCE,
    -------------------------------------
    SUM(SALES_UNIT_CAL) SALES_UNIT_CAL,
    SUM(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY,
    SUM(SALES_VALUE_CAL) SALES_VALUE_CAL,
    SUM(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
    SUM(CONUTING_UNIT) CONUTING_UNIT,
    SUM(CONUTING_UNIT_LY) as CONUTING_UNIT_LY,
    null as prescription,
    null as prescription_ly,
    -------------------------------------
    --Retail藏数逻辑标签------------------
    1 PACK_FLAG,
    1 PROD_FLAG,
    0 DTP_FLAG,
    null as cmps_flag,
    -------------------------------------
    --EC数据标签--------------------------
    '' PLATFORM_TYPE,
    '' STORE_NAME,
    '' STORE_TYPE,
    -------------------------------------
    --COUNTY数据标签----------------------
    '' REGION_TYPE,
    -------------------------------------
    --AIA数据标签-------------------------
    null inst_code, --可以移除，报告没有使用
    -------------------------------------
    --XIE HE 数据标签---------------------
    '' as dept_name,
    '' as new_code,
    '' as area,
    '' as h_level,
    '' as reimburse,
    '' as reimburse_type,
    '' as prescription_source,
    -------------------------------------
    '' as CORP_COD --可以移除，报告没有使用
  -------------------------------------
  FROM
    dm.dm_ims_tf_sales A
  where
    A.YYYYMM >= '202001'
    AND A.MARKET = 'IMS ALL Market'
  GROUP BY
    A.YYYYMM,
    A.PACK_COD,
    A.AUDIT_COD

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

-- DBTITLE 1,COUNTY
-- DELETE FROM
--   dm.dm_tf_external_sales
-- WHERE
--   DATA_SOURCE = 'IQVIA-COUNTY(Quarterly)';

INSERT INTO dm.dm_tf_external_sales (
    YYYYMM,
    PACK_COD,
    AUDIT_COD,
    DATA_SOURCE,
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    CONUTING_UNIT,
    CONUTING_UNIT_LY,
    prescription,
    prescription_ly,
    PACK_FLAG,
    PROD_FLAG,
    DTP_FLAG,
    cmps_flag,
    PLATFORM_TYPE,
    STORE_NAME,
    STORE_TYPE,
    REGION_TYPE,
    inst_code,
    dept_name,
    new_code,
    area,
    h_level,
    reimburse,
    reimburse_type,
    prescription_source,
    CORP_COD
  )
  SELECT
    A.YYYYMM,
    A.PACK_COD,
    A.AUDIT_COD,
    'IQVIA-COUNTY(Quarterly)' DATA_SOURCE,
    -------------------------------------
    SUM(SALES_UNIT_CAL) SALES_UNIT_CAL,
    SUM(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY,
    SUM(SALES_VALUE_CAL) SALES_VALUE_CAL,
    SUM(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
    SUM(CONUTING_UNIT) CONUTING_UNIT,
    SUM(CONUTING_UNIT_LY) as CONUTING_UNIT_LY,
    null as prescription,
    null as prescription_ly,
    -------------------------------------
    --Retail藏数逻辑标签------------------
    1 PACK_FLAG,
    1 PROD_FLAG,
    0 DTP_FLAG,
    null as cmps_flag,
    -------------------------------------
    --EC数据标签--------------------------
    '' PLATFORM_TYPE,
    '' STORE_NAME,
    '' STORE_TYPE,
    -------------------------------------
    --COUNTY数据标签----------------------
    NVL(A.REGION_TYPE, '') REGION_TYPE,
    -------------------------------------
    --AIA数据标签-------------------------
    null inst_code, --可以移除，报告没有使用
    -------------------------------------
    --XIE HE 数据标签---------------------
    '' as dept_name,
    '' as new_code,
    '' as area,
    '' as h_level,
    '' as reimburse,
    '' as reimburse_type,
    '' as prescription_source,
    -------------------------------------
    '' as CORP_COD --可以移除，报告没有使用
  -------------------------------------
  FROM
    DM.dm_ext_county_tf_sales_region A
  where
    A.YYYYMM >= '202001'
    AND A.MARKET = 'IMS ALL Market'
  GROUP BY
    A.YYYYMM,
    A.PACK_COD,
    A.AUDIT_COD,
    A.REGION_TYPE

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

-- DBTITLE 1,THC
-- DELETE FROM
--   dm.dm_tf_external_sales
-- WHERE
--   DATA_SOURCE = 'THC(Quarterly)';

INSERT INTO dm.dm_tf_external_sales (
    YYYYMM,
    PACK_COD,
    AUDIT_COD,
    DATA_SOURCE,
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    CONUTING_UNIT,
    CONUTING_UNIT_LY,
    prescription,
    prescription_ly,
    PACK_FLAG,
    PROD_FLAG,
    DTP_FLAG,
    cmps_flag,
    PLATFORM_TYPE,
    STORE_NAME,
    STORE_TYPE,
    REGION_TYPE,
    inst_code,
    dept_name,
    new_code,
    area,
    h_level,
    reimburse,
    reimburse_type,
    prescription_source,
    CORP_COD
  )
  SELECT
    A.YYYYMM,
    A.PACK_COD,
    A.AUDIT_COD,
    'THC(Quarterly)' DATA_SOURCE,
    -------------------------------------
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    COUNTING_UNIT,
    COUNTING_UNIT_LY ,
    null as prescription,
    null as prescription_ly,
    -------------------------------------
    --Retail藏数逻辑标签------------------
    1 PACK_FLAG,
    1 PROD_FLAG,
    0 DTP_FLAG,
    null as cmps_flag,
    -------------------------------------
    --EC数据标签--------------------------
    '' PLATFORM_TYPE,
    '' STORE_NAME,
    '' STORE_TYPE,
    -------------------------------------
    --COUNTY数据标签----------------------
    '' REGION_TYPE,
    -------------------------------------
    --AIA数据标签-------------------------
    null inst_code, --可以移除，报告没有使用
    -------------------------------------
    --XIE HE 数据标签---------------------
    '' as dept_name,
    '' as new_code,
    '' as area,
    '' as h_level,
    '' as reimburse,
    '' as reimburse_type,
    '' as prescription_source,
    -------------------------------------
    '' as CORP_COD --可以移除，报告没有使用
  -------------------------------------
  FROM
    dm.dm_ext_thc_sales A
  where
    AUDIT_COD = 'CHT'

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

-- DBTITLE 1,CHC_RAW_DATA
------------------------------------------------------------------------------CHC-------------------------------------------------------------------
CREATE OR REPLACE TEMPORARY VIEW V_CHC_RAW_DATA AS
with exchc_rawdata_temp as (        --主表数据 
      select      
                CASE
                        WHEN SUBSTRING(yq, 5,2) = 'Q1' THEN CONCAT(SUBSTRING(yq, 1, 4), '03')
                        WHEN SUBSTRING(yq, 5, 2) = 'Q2' THEN CONCAT(SUBSTRING(yq, 1, 4), '06')
                        WHEN SUBSTRING(yq, 5, 2) = 'Q3' THEN CONCAT(SUBSTRING(yq, 1, 4), '09')
                        WHEN SUBSTRING(yq, 5, 2) = 'Q4' THEN CONCAT(SUBSTRING(yq, 1, 4), '12')
                        ELSE yq 
                END AS yq,
                city_c, 
                pack_cod, 
                prod_cod, 
                value, 
                totalunit, 
                countingunit
      from dwd.dwd_gnd_chc_rawdata  
      where year >= '2021'
),
temp_corp as (
    select 
      PACK_COD,
      CORP_COD,
      CORP_DES 
    from dm.dm_ims_td_pack_property  
    group by PACK_COD,CORP_COD,CORP_DES 
),
chc_pack_property_clean as (
select distinct 
	iqvia_pack_code    ,
	iqvia_prod_code    ,
	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           ,
	builtinarchivedate ,
	source_file_path   ,
	source_file_name   ,
	etl_insert_dt      ,
	countingunit		
from  dwd.dwd_gnd_ext_chc_pack_property
),
temp_corp_temp as (   --  chc本身唯独表 存在corp_cod以及 CORP_DES 与手工数据不一致的问题 
  select 
    t1.pack_cod,
    t1.iqvia_pack_code,
    t1.countingunit,
    coalesce(t2.CORP_COD, t1.CORP_COD) as CORP_COD ,
    coalesce(t2.CORP_DES,t1.CORP_DES ) as CORP_DES
  from chc_pack_property_clean t1
  left join temp_corp t2
    on t1.iqvia_pack_code = t2.PACK_COD
),
dim_exchc_geo as (
  -- geo相关信息   在CHPA中没有 苏州、无锡、福州等数据,根据业务反馈，按照 city_c当作AUDIT_COD
  select
    t1.city_e,
    concat( t1.city_c , '市') as city_c,
    upper( nvl(t2.AUDIT_COD ,t1.city_e)  ) as AUDIT_COD
  from  dwd.dwd_gnd_ext_chc_geo t1
  left join dm.dm_ims_td_geo  t2
    on t1.city_e= t2.city
-- --20260122 按照新的业务逻辑，直接用城市名称匹配获得省份code
-- SELECT DISTINCT city_name,province_code,province_name FROM DM.dm_td_geography
-- WHERE nvl(city_code,'')!='' and nvl(province_code,'')!=''  and nvl(province_name,'')!=''
),
exchc_rawdata_iqvia as ( 
  select
    t1.yq ,
    t1.city_c, 
    -- t1.pack_cod pack_cod_old, 
    t3.iqvia_pack_code as pack_cod,
    t3.CORP_COD as CORP_COD,
    C.AUDIT_COD as AUDIT_COD,
    t1.prod_cod, 
    t1.value, 
    t1.totalunit, 
    -- t1.countingunit,
    t1.totalunit * nvl(t3.countingunit,1 ) as countingunit
    -- t2.countingunit as  tt
  from exchc_rawdata_temp t1
  left join temp_corp_temp t3 
    on t1.pack_cod = t3.pack_cod
  LEFT JOIN dim_exchc_geo C
    ON t1.CITY_C = C.city_c
),
value_temp as(
  select 
      a.yq,
		  a.AUDIT_COD, 
		  a.pack_cod, 
		  --t1.prod_cod,
      a.CORP_COD,
		  sum(a.value) value,
		  sum(a.totalunit) totalunit,
		  sum(a.countingunit) countingunit,
		  sum(value_ly) value_ly,
      sum(totalunit_ly) totalunit_ly,
      sum(countingunit_ly) countingunit_ly
      from (
	  select      
		  t1.yq,
		  t1.AUDIT_COD, 
		  t1.pack_cod, 
		  --t1.prod_cod,
      t1.CORP_COD,
		  t1.value,
		  t1.totalunit,
		  t1.countingunit,
		  0 value_ly,
      0 totalunit_ly,
      0 countingunit_ly
	  from exchc_rawdata_iqvia t1
    union all 
    select      
		  cast(t1.yq+100 as int),
		  t1.AUDIT_COD, 
		  t1.pack_cod, 
		  --t1.prod_cod,
      t1.CORP_COD,
		  0 value,
		  0 totalunit,
		  0 countingunit,
		  t1.value value_ly,
      t1.totalunit totalunit_ly,
      t1.countingunit countingunit_ly
	  from exchc_rawdata_iqvia t1
   WHERE T1.YQ+100 <= (SELECT MAX(YQ) FROM exchc_rawdata_iqvia)
  ) a
  group by a.yq,
		  a.AUDIT_COD, 
		  a.pack_cod, 
		  --t1.prod_cod,
      a.CORP_COD
)
 
select 
		t1.yq as YYYYMM ,
		t1.AUDIT_COD, 
		t1.pack_cod, 
    t1.CORP_COD
		--t1.prod_cod  
		,sum(totalunit)	  AS SALES_UNIT_CAL			     -- totalunit
		,sum(totalunit_ly)     AS SALES_UNIT_CAL_LY  --totalunit_ly
		,sum(value)    AS SALES_VALUE_CAL   --value
		,sum(value_ly   )     AS SALES_VALUE_CAL_LY  
		,sum(countingunit)    AS CONUTING_UNIT   --countingunit
		,sum(countingunit_ly) AS CONUTING_UNIT_LY 
    ,'CHC(Quarterly)' as DATA_SOURCE
	from value_temp t1 
	group by  1,2,3,4

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

-- DBTITLE 1,CHC
-- DELETE FROM
--   dm.dm_tf_external_sales
-- WHERE
--   DATA_SOURCE = 'CHC(Quarterly)';

INSERT INTO dm.dm_tf_external_sales (
    YYYYMM,
    PACK_COD,
    AUDIT_COD,
    DATA_SOURCE,
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    CONUTING_UNIT,
    CONUTING_UNIT_LY,
    prescription,
    prescription_ly,
    PACK_FLAG,
    PROD_FLAG,
    DTP_FLAG,
    cmps_flag,
    PLATFORM_TYPE,
    STORE_NAME,
    STORE_TYPE,
    REGION_TYPE,
    inst_code,
    dept_name,
    new_code,
    area,
    h_level,
    reimburse,
    reimburse_type,
    prescription_source,
    CORP_COD
  )
  SELECT
    A.YYYYMM,
    A.PACK_COD,
    A.AUDIT_COD,
    'CHC(Quarterly)' DATA_SOURCE,
    -------------------------------------
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    CONUTING_UNIT,
    CONUTING_UNIT_LY,
    null as prescription,
    null as prescription_ly,
    -------------------------------------
    --Retail藏数逻辑标签------------------
    1 PACK_FLAG,
    1 PROD_FLAG,
    0 DTP_FLAG,
    null as cmps_flag,
    -------------------------------------
    --EC数据标签--------------------------
    '' PLATFORM_TYPE,
    '' STORE_NAME,
    '' STORE_TYPE,
    -------------------------------------
    --COUNTY数据标签----------------------
    '' REGION_TYPE,
    -------------------------------------
    --AIA数据标签-------------------------
    null inst_code, --可以移除，报告没有使用
    -------------------------------------
    --XIE HE 数据标签---------------------
    '' as dept_name,
    '' as new_code,
    '' as area,
    '' as h_level,
    '' as reimburse,
    '' as reimburse_type,
    '' as prescription_source,
    -------------------------------------
    '' as CORP_COD --可以移除，报告没有使用
  -------------------------------------
  FROM
    V_CHC_RAW_DATA A
  WHERE
    YYYYMM >= (
      SELECT
        CAST((LEFT(MAX(YYYYMM), 4) - 3) * 100 + 1 AS INT)
      FROM
        V_CHC_RAW_DATA
    )

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

-- DBTITLE 1,Retail
-- DELETE FROM
--   dm.dm_tf_external_sales
-- WHERE
--   DATA_SOURCE = 'Retail(Quarterly)';

INSERT INTO dm.dm_tf_external_sales (
    YYYYMM,
    PACK_COD,
    AUDIT_COD,
    DATA_SOURCE,
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    CONUTING_UNIT,
    CONUTING_UNIT_LY,
    prescription,
    prescription_ly,
    PACK_FLAG,
    PROD_FLAG,
    DTP_FLAG,
    cmps_flag,
    PLATFORM_TYPE,
    STORE_NAME,
    STORE_TYPE,
    REGION_TYPE,
    inst_code,
    dept_name,
    new_code,
    area,
    h_level,
    reimburse,
    reimburse_type,
    prescription_source,
    CORP_COD
  )
SELECT
  A.YYYYMM,
  A.iqvia_pack_code,
  trim(A.AUDIT_COD) AS AUDIT_COD,
  'Retail(Quarterly)' DATA_SOURCE,
  -------------------------------------
  SUM(sales_unit) SALES_UNIT_CAL,
  SUM(sales_unit_ly) as SALES_UNIT_CAL_LY,
  SUM(sales_value) SALES_VALUE_CAL,
  SUM(sales_value_LY) as SALES_VALUE_CAL_LY,
  SUM(counting_unit) CONUTING_UNIT,
  SUM(counting_unit_LY) as CONUTING_UNIT_LY,
  null as prescription,
  null as prescription_ly,
  -------------------------------------
  --Retail藏数逻辑标签------------------
  A.PACK_FLAG,
  A.brand_flag PROD_FLAG,
  0 DTP_FLAG,
  case
    when
      max(lower(C.CMPS_DES)) in ('atorvastatin', 'rosuvastatin', 'esomeprazole', 'omeprazole')
    then
      1
    else 0
  end as cmps_flag,
  -------------------------------------
  --EC数据标签--------------------------
  '' PLATFORM_TYPE,
  '' STORE_NAME,
  '' STORE_TYPE,
  -------------------------------------
  --COUNTY数据标签----------------------
  '' REGION_TYPE,
  -------------------------------------
  --AIA数据标签-------------------------
  null inst_code, --可以移除，报告没有使用
  -------------------------------------
  --XIE HE 数据标签---------------------
  '' as dept_name,
  '' as new_code,
  '' as area,
  '' as h_level,
  '' as reimburse,
  '' as reimburse_type,
  '' as prescription_source,
  -------------------------------------
  '' as CORP_COD --可以移除，报告没有使用
-------------------------------------
FROM
  dm.dm_zk_retail_sales A
    LEFT JOIN (
      select DISTINCT
        iqvia_pack_code,
        CMPS_DES
      from
        dm.dm_zk_retail_pack_property
    ) C
      ON A.iqvia_pack_code = C.iqvia_pack_code
-------------------------------------
LEFT JOIN (SELECT OTHERS_MONTH FROM tmp.tmp_dm_td_ext_retail_niad_month) D
-------------------------------------
where
  A.YYYYMM >= '202001' AND A.YYYYMM <= D.OTHERS_MONTH
GROUP BY
  A.YYYYMM,
  A.iqvia_pack_code,
  A.AUDIT_COD,
  A.PACK_FLAG,
  A.brand_flag

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

-- DBTITLE 1,DTP
-- DELETE FROM
--   dm.dm_tf_external_sales
-- WHERE
--   DATA_SOURCE = 'DTP(Quarterly)';

INSERT INTO dm.dm_tf_external_sales (
    YYYYMM,
    PACK_COD,
    AUDIT_COD,
    DATA_SOURCE,
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    CONUTING_UNIT,
    CONUTING_UNIT_LY,
    prescription,
    prescription_ly,
    PACK_FLAG,
    PROD_FLAG,
    DTP_FLAG,
    cmps_flag,
    PLATFORM_TYPE,
    STORE_NAME,
    STORE_TYPE,
    REGION_TYPE,
    inst_code,
    dept_name,
    new_code,
    area,
    h_level,
    reimburse,
    reimburse_type,
    prescription_source,
    CORP_COD
  )
  SELECT
    A.YYYYMM,
    A.iqvia_pack_code,
    trim(A.AUDIT_COD) AS AUDIT_COD,
    'DTP(Quarterly)' DATA_SOURCE,
    -------------------------------------
    SUM(sales_unit) SALES_UNIT_CAL,
    SUM(sales_unit_ly) as SALES_UNIT_CAL_LY,
    SUM(sales_value) SALES_VALUE_CAL,
    SUM(sales_value_LY) as SALES_VALUE_CAL_LY,
    SUM(counting_unit) CONUTING_UNIT,
    SUM(counting_unit_LY) as CONUTING_UNIT_LY,
    null as prescription,
    null as prescription_ly,
    -------------------------------------
    --Retail藏数逻辑标签------------------
    1 PACK_FLAG,
    brand_flag AS PROD_FLAG,
    1 DTP_FLAG,
    null as cmps_flag,
    -------------------------------------
    --EC数据标签--------------------------
    '' PLATFORM_TYPE,
    '' STORE_NAME,
    '' STORE_TYPE,
    -------------------------------------
    --COUNTY数据标签----------------------
    '' REGION_TYPE,
    -------------------------------------
    --AIA数据标签-------------------------
    null inst_code, --可以移除，报告没有使用
    -------------------------------------
    --XIE HE 数据标签---------------------
    '' as dept_name,
    '' as new_code,
    '' as area,
    '' as h_level,
    '' as reimburse,
    '' as reimburse_type,
    '' as prescription_source,
    -------------------------------------
    '' as CORP_COD --可以移除，报告没有使用
  -------------------------------------
  FROM
    dm.dm_zk_retail_dtp_sales A
  where
    A.YYYYMM >= '202001'
  GROUP BY
    A.YYYYMM,
    A.iqvia_pack_code,
    A.AUDIT_COD,
    A.brand_flag,
    A.pack_flag

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

-- DBTITLE 1,EC
-- DELETE FROM
--   dm.dm_tf_external_sales
-- WHERE
--   DATA_SOURCE = 'EC(Monthly)';

INSERT INTO dm.dm_tf_external_sales (
    YYYYMM,
    PACK_COD,
    AUDIT_COD,
    DATA_SOURCE,
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    CONUTING_UNIT,
    CONUTING_UNIT_LY,
    prescription,
    prescription_ly,
    PACK_FLAG,
    PROD_FLAG,
    DTP_FLAG,
    cmps_flag,
    PLATFORM_TYPE,
    STORE_NAME,
    STORE_TYPE,
    REGION_TYPE,
    inst_code,
    dept_name,
    new_code,
    area,
    h_level,
    reimburse,
    reimburse_type,
    prescription_source,
    CORP_COD
  )
  SELECT
    A.YYYYMM,
    A.iqvia_pack_code,
    A.AUDIT_COD,
    'EC(Monthly)' DATA_SOURCE,
    -------------------------------------
    SUM(sales_unit) SALES_UNIT_CAL,
    SUM(sales_unit_ly) SALES_UNIT_CAL_LY,
    SUM(sales_value) SALES_VALUE_CAL,
    SUM(sales_value_LY) SALES_VALUE_CAL_LY,
    SUM(counting_unit) CONUTING_UNIT,
    SUM(counting_unit_LY) CONUTING_UNIT_LY,
    null as prescription,
    null as prescription_ly,
    -------------------------------------
    --Retail藏数逻辑标签------------------
    1 PACK_FLAG,
    1 PROD_FLAG,
    0 DTP_FLAG,
    null as cmps_flag,
    -------------------------------------
    --EC数据标签--------------------------
    A.PLATFORM,
    A.STORE_NAME,
    A.STORE_TYPE,
    -------------------------------------
    --COUNTY数据标签----------------------
    '' REGION_TYPE,
    -------------------------------------
    --AIA数据标签-------------------------
    null inst_code, --可以移除，报告没有使用
    -------------------------------------
    --XIE HE 数据标签---------------------
    '' as dept_name,
    '' as new_code,
    '' as area,
    '' as h_level,
    '' as reimburse,
    '' as reimburse_type,
    '' as prescription_source,
    -------------------------------------
    '' as CORP_COD --可以移除，报告没有使用
  -------------------------------------
  FROM
    dm.dm_zk_ec_sales A
  LEFT JOIN (SELECT OTHERS_MONTH FROM tmp.tmp_dm_td_ext_ec_niad_month) D
  where
    A.YYYYMM >= '202001' and A.YYYYMM<=D.others_month
  GROUP BY
    A.YYYYMM,
    A.iqvia_pack_code,
    A.AUDIT_COD,
    A.PLATFORM,
    A.STORE_NAME,
    A.STORE_TYPE,
    A.PACK_FLAG,
    A.PROD_FLAG

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

-- DBTITLE 1,AIA
-- DELETE FROM
--   dm.dm_tf_external_sales
-- WHERE
--   DATA_SOURCE = 'AIA(Monthly)';

INSERT INTO dm.dm_tf_external_sales (
    YYYYMM,
    PACK_COD,
    AUDIT_COD,
    DATA_SOURCE,
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    CONUTING_UNIT,
    CONUTING_UNIT_LY,
    prescription,
    prescription_ly,
    PACK_FLAG,
    PROD_FLAG,
    DTP_FLAG,
    cmps_flag,
    PLATFORM_TYPE,
    STORE_NAME,
    STORE_TYPE,
    REGION_TYPE,
    inst_code,
    dept_name,
    new_code,
    area,
    h_level,
    reimburse,
    reimburse_type,
    prescription_source,
    CORP_COD
  )
  SELECT
    A.ym,
    A.PACK_COD,
    A.AUDIT_COD,
    'AIA(Monthly)' DATA_SOURCE,
    -------------------------------------
    sum(volume) as SALES_UNIT_CAL,
    sum(volume_ly) as SALES_UNIT_CAL_LY,
    sum(value) as SALES_VALUE_CAL,
    sum(value_ly) as SALES_VALUE_CAL_LY,
    sum(counting_unit) as CONUTING_UNIT,
    sum(counting_unit_ly) as CONUTING_UNIT_LY,
    null as prescription,
    null as prescription_ly,
    -------------------------------------
    --Retail藏数逻辑标签------------------
    1 PACK_FLAG,
    1 PROD_FLAG,
    0 DTP_FLAG,
    null as cmps_flag,
    -------------------------------------
    --EC数据标签--------------------------
    '' PLATFORM_TYPE,
    '' STORE_NAME,
    '' STORE_TYPE,
    -------------------------------------
    --COUNTY数据标签----------------------
    '' REGION_TYPE,
    -------------------------------------
    --AIA数据标签-------------------------
    inst_code AS inst_code, --可以移除，报告没有使用
    -------------------------------------
    --XIE HE 数据标签---------------------
    '' as dept_name,
    '' as new_code,
    '' as area,
    '' as h_level,
    '' as reimburse,
    '' as reimburse_type,
    '' as prescription_source,
    -------------------------------------
    '' as CORP_COD --可以移除，报告没有使用
  -------------------------------------
  from
    dm.dm_ext_aia_sales A
  --取202401年之后的数
  where
    ym >= '202401'
  group by
    ym,
    pack_cod,
    corp_cod,
    AUDIT_COD,
    inst_code

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

-- DBTITLE 1,XIEHE
-- DELETE FROM
--   dm.dm_tf_external_sales
-- WHERE
--   DATA_SOURCE = 'XH Data(Quarterly)';

INSERT INTO dm.dm_tf_external_sales (
    YYYYMM,
    PACK_COD,
    AUDIT_COD,
    DATA_SOURCE,
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    CONUTING_UNIT,
    CONUTING_UNIT_LY,
    prescription,
    prescription_ly,
    PACK_FLAG,
    PROD_FLAG,
    DTP_FLAG,
    cmps_flag,
    PLATFORM_TYPE,
    STORE_NAME,
    STORE_TYPE,
    REGION_TYPE,
    inst_code,
    dept_name,
    new_code,
    area,
    h_level,
    reimburse,
    reimburse_type,
    prescription_source,
    CORP_COD
  )
  SELECT
    A.YYYYMM,
    A.iqvia_pack_code,
    A.AUDIT_COD,
    'XH Data(Quarterly)' DATA_SOURCE,
    -------------------------------------
    0 SALES_UNIT_CAL,
    0 SALES_UNIT_CAL_LY,
    SUM(sales_value) SALES_VALUE_CAL,
    SUM(sales_value_LY) SALES_VALUE_CAL_LY,
    SUM(A.counting_unit) CONUTING_UNIT,
    SUM(A.counting_unit_ly) CONUTING_UNIT_LY,
    null as prescription,
    null as prescription_ly,
    -------------------------------------
    --Retail藏数逻辑标签------------------
    1 PACK_FLAG,
    1 PROD_FLAG,
    0 DTP_FLAG,
    null as cmps_flag,
    -------------------------------------
    --EC数据标签--------------------------
    '' PLATFORM_TYPE,
    '' STORE_NAME,
    '' STORE_TYPE,
    -------------------------------------
    --COUNTY数据标签----------------------
    '' REGION_TYPE,
    -------------------------------------
    --AIA数据标签-------------------------
    null inst_code, --可以移除，报告没有使用
    -------------------------------------
    --XIE HE 数据标签---------------------
    NVL(A.dept_name, '') dept_name,
    NVL(A.new_code, '') new_code,
    NVL(A.province, '') area,
    NVL(A.h_level, '') h_level,
    NVL(A.reimburse, '') reimburse,
    NVL(A.reimburse_type, '') reimburse_type,
    NVL(A.prescription_source, '') prescription_source,
    -------------------------------------
    '' as CORP_COD --可以移除，报告没有使用
  -------------------------------------
  FROM
    dm.dm_ext_xiehe_sales A
  WHERE
    A.YYYYMM >= '202001'
  GROUP BY
    A.YYYYMM,
    A.iqvia_pack_code,
    A.AUDIT_COD,
    A.dept_name,
    A.new_code,
    A.province,
    A.h_level,
    A.reimburse,
    A.reimburse_type,
    A.prescription_source

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

-- MAGIC %md
-- MAGIC ## 特殊逻辑

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

-- DBTITLE 1,更新pack code
update dm.dm_tf_external_sales set PACK_COD = concat('PACK_COD_',DATA_SOURCE)
where PACK_COD is null or PACK_COD = '';
update dm.dm_tf_external_sales set AUDIT_COD = 'ROC'
where AUDIT_COD is null or AUDIT_COD = '';


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

-- MAGIC %md
-- MAGIC ## NIAD单独页面数据

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

update dm.dm_zk_retail_sales set DATA_SOURCE = 'Retail(Quarterly)'

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

-- DBTITLE 1,NIAD
-- =============================================================================
-- 脚本名称: dm.dm_tf_external_sales_niad 数据装载脚本
-- 功能描述: 将零售和电商的销售数据（仅限NIAD市场）插入目标表
-- 数据来源: dm.dm_zk_retail_sales, dm.dm_zk_ec_sales, dm.dm_td_external_market_pack_mapping_temp
-- 目标表:  dm.dm_tf_external_sales_niad
-- 更新方式: INSERT OVERWRITE（全量覆盖）
-- 创建时间: 2026-03-09
-- =============================================================================
 
INSERT OVERWRITE TABLE dm.dm_tf_external_sales_niad
(
    YYYYMM,                -- 年月分区键
    PACK_COD,               -- 药品包装代码
    AUDIT_COD,              -- 审计代码
    SALES_UNIT_CAL,         -- 本期销量（单位）
    SALES_UNIT_CAL_LY,      -- 去年同期销量（单位）
    SALES_VALUE_CAL,        -- 本期销售额
    SALES_VALUE_CAL_LY,     -- 去年同期销售额
    CONUTING_UNIT,          -- 计数单位（本期）
    CONUTING_UNIT_LY,       -- 计数单位（去年同期）
    DATA_SOURCE,            -- 数据来源（零售/电商）
    DATE_KEY,               -- 日期键（转换为日期类型）
    PACK_SOURCE,            -- 包装来源唯一标识
    AUDIT_SOURCE            -- 审计来源唯一标识
)
 
WITH mkt_pack AS (
    SELECT DISTINCT
           PACK_COD,
           DATA_SOURCE
      FROM dm.dm_td_external_market_pack_mapping_temp
     WHERE DATA_SOURCE IN ('Retail(Quarterly)', 'EC(Monthly)')  -- 仅限零售和电商
       AND MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market'   -- NIAD市场过滤
),
 
sales AS (
    -- 零售数据（季度）
    SELECT
           YYYYMM,
           iqvia_pack_code AS PACK_COD,
           AUDIT_COD,
           sales_unit       AS SALES_UNIT_CAL,
           sales_unit_ly    AS SALES_UNIT_CAL_LY,
           sales_value      AS SALES_VALUE_CAL,
           sales_value_ly   AS SALES_VALUE_CAL_LY,
           counting_unit    AS CONUTING_UNIT,
           counting_unit_ly AS CONUTING_UNIT_LY,
           DATA_SOURCE,
           -- 将日期转换放在CTE中统一处理，避免重复解析
           TO_DATE(YYYYMM, 'yyyyMM') AS DATE_KEY,
           CONCAT(iqvia_pack_code, DATA_SOURCE) AS PACK_SOURCE,
           CONCAT(AUDIT_COD, DATA_SOURCE)       AS AUDIT_SOURCE
      FROM dm.dm_zk_retail_sales
     WHERE YYYYMM >= '202001'                     -- 仅保留2020年及以后数据
 
    UNION ALL
 
    -- 电商数据（月度）
    SELECT
           YYYYMM,
           iqvia_pack_code AS PACK_COD,
           AUDIT_COD,
           sales_unit       AS SALES_UNIT_CAL,
           sales_unit_ly    AS SALES_UNIT_CAL_LY,
           sales_value      AS SALES_VALUE_CAL,
           sales_value_ly   AS SALES_VALUE_CAL_LY,
           counting_unit    AS CONUTING_UNIT,
           counting_unit_ly AS CONUTING_UNIT_LY,
           DATA_SOURCE,
           TO_DATE(YYYYMM, 'yyyyMM') AS DATE_KEY,
           CONCAT(iqvia_pack_code, DATA_SOURCE) AS PACK_SOURCE,
           CONCAT(AUDIT_COD, DATA_SOURCE)       AS AUDIT_SOURCE
      FROM dm.dm_zk_ec_sales
     WHERE YYYYMM >= '202001'                     -- 仅保留2020年及以后数据
)
SELECT
       a.YYYYMM,
       a.PACK_COD,
       a.AUDIT_COD,
       a.SALES_UNIT_CAL,
       a.SALES_UNIT_CAL_LY,
       a.SALES_VALUE_CAL,
       a.SALES_VALUE_CAL_LY,
       a.CONUTING_UNIT,
       a.CONUTING_UNIT_LY,
       a.DATA_SOURCE,
       a.DATE_KEY,
       a.PACK_SOURCE,
       a.AUDIT_SOURCE
  FROM sales a
 INNER JOIN mkt_pack b
    ON a.PACK_COD = b.PACK_COD          
   AND a.DATA_SOURCE = b.DATA_SOURCE;    