-- Databricks notebook source
---------------------------------------------------------------------------
--修改时间：20241126
--修改人：Fanxujia
--修改内容：
--THC渠道分为14省的数据源以及全国的数据源两种，分在了2个事实表里，下面在维表在引用事实表时，需要将两个事实表union在一起
----------------------------------------------------------------------------
----------------------------------------------------------------------------
--修改时间：20241129
--修改人：Fanxujia
--修改内容：
--补充Retail直取中DTP的手工文件里的pack_cod
----------------------------------------------------------------------------
create or replace temporary view external_sales_union as
select distinct 
PACK_COD,
DATA_SOURCE
from dm.dm_tf_external_sales
union all
select distinct
PACK_COD,
DATA_SOURCE
from dm.dm_tf_external_sales_thc
union all
select distinct
pack_code,
'Retail(Quarterly)' as DATA_SOURCE
from dm.dm_zk_retail_sales_kpi
where NVL(pack_code,'Others') <> 'Others'
and pack_code <> ''

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

--------------------------------------------------------------------------------------------------------------------------------------------------
--修改时间：20240812
--修改人：FanXujia
--修改内容：
--在从DM_TD_EXTERNAL_KEYCOMPATITOR表里获取市场描述（MARKET_DESC字段）时，需要增加一个DATA_SOURCE的条件。
--修改原因：
--对于同一个市场，每个渠道的市场描述并不一样。
--------------------------------------------------------------------------------------------------------------------------------------------------

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

/*
修改日期：20250311
修改人：chenwu
修改内容：修改COUNTINGUNIT_MARKET_RATIO 取unit的值
*/
/*
修改日期：20250513
修改人：chenwu
修改内容：修改RE Market 为 RE Market (Global) 
*/
INSERT OVERWRITE dm.dm_td_external_market_pack_mapping_temp(
  PACK_COD,PACK_DES,PROD_DES,PROD_DES_C,MARKET,TA,KEY_COMPETITOR,AZ_RELATED,CLASS,
  VALUE_MARKET_RATIO,UNIT_MARKET_RATIO,COUNTINGUNIT_MARKET_RATIO,PDOT_MARKET_RATIO,MARKET_DESC,TA_RN,DATA_SOURCE,dept_name,key_dept_name
  ,new_code
  ,area
)
WITH MKT_PACK_RATIO AS (
  -------------------------------------根据沟通，市场+产品对应的ratio是唯一的------------------------------------------
SELECT A.MARKET,A.PACK_COD,MAX(A.Value_Market_Ratio) Value_Market_Ratio,MAX(A.Unit_Market_Ratio) Unit_Market_Ratio,MAX(A.PDOT_Market_Ratio) CountingUnit_Market_Ratio,MAX(A.CountingUnit_Market_Ratio) PDOT_Market_Ratio   --因为之前的报告在使用源表的数据，因此命名是反的。
FROM DWS.dws_ims_td_market_ratio A
WHERE A.YM = (SELECT MAX(YM) FROM DWS.dws_ims_td_market_ratio)
GROUP BY A.MARKET,A.PACK_COD
)
--------------------------------------------------------------------CHPA------------------------------------------------------------------
SELECT B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET,B.TA,B.KEY_COMPETITOR,
CASE
    WHEN B.MARKET = 'DM Market' THEN 'DM' 
    WHEN B.MARKET = 'CV Market' THEN 'CV' 
    WHEN B.MARKET = 'RD Market' THEN 'RD' 
    WHEN B.MARKET = 'RE Market' THEN 'RE' --WHEN B.MARKET = 'RE Market' THEN 'RE' 
    WHEN B.MARKET IN ('GI Market','Retail_GI Market') THEN 'GI' 
    WHEN B.MARKET = 'ONC Market' THEN 'ONCO' 
    WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD' 
    WHEN B.MARKET = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE 
    END AZ_RELATED
    ,B.CLASS,NVL(B.VALUE_MARKET_RATIO,1) VALUE_MARKET_RATIO,NVL(B.UNIT_MARKET_RATIO,1) UNIT_MARKET_RATIO,NVL(B.COUNTINGUNIT_MARKET_RATIO,1) COUNTINGUNIT_MARKET_RATIO,NVL(B.PDOT_MARKET_RATIO,1) PDOT_MARKET_RATIO,B.MARKET_DESC,
CASE 
    WHEN B.MARKET = 'DM Market' THEN 1 
    WHEN B.MARKET = 'CV Market' THEN 2 
    WHEN B.MARKET = 'RD Market' THEN 3 
    WHEN B.MARKET = 'RE Market' THEN 4 
    WHEN B.MARKET = 'GI Market' THEN 5 
    WHEN B.MARKET = 'ONC Market' THEN 6 
    WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7 
    WHEN B.MARKET = 'RARE Market' THEN 8 --20250701 chenwu 新增RARE 
    ELSE 99 END TA_RN
,'IQVIA-CHPA(Monthly)' DATA_SOURCE
,'' as dept_name
,'' as key_dept_name
,'' as new_code
,'' as area
FROM (
SELECT B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET,MAX(C.TA) TA,MAX(E.KEY_COMPETITOR) KEY_COMPETITOR,MAX(E.IS_AZ) IS_AZ,MAX(E.AZ_MAIN) AZ_MAIN,MAX(E.AZ_RELATED) AZ_RELATED,MAX(E.CLASS) CLASS,MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO,MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO,MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO,MAX(D.PDOT_MARKET_RATIO) PDOT_MARKET_RATIO,max(g.MARKET_DESC) MARKET_DESC
FROM (SELECT PACK_COD,COUNT(*) RUM FROM DM.DM_TF_EXTERNAL_SALES
-- WHERE YYYYMM >= (SELECT CAST((LEFT(MAX(YYYYMM),4)-3)*100+1 AS INT) FROM DM.DM_TF_EXTERNAL_SALES)
-- AND DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
where DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
GROUP BY PACK_COD) A
LEFT JOIN DM.DM_IMS_TD_PACK_PROPERTY B
ON B.PACK_COD = A.PACK_COD --AND B.MARKET <> 'IMS ALL Market'
LEFT JOIN DM.DM_IMS_TD_MARKET_PROPERTY C
ON B.MARKET = C.MARKET
LEFT JOIN DM.DM_IMS_TD_PACK_PROPERTY E ON B.MARKET_PACK_KEY = E.MARKET_PACK_KEY
LEFT JOIN MKT_PACK_RATIO D ON B.MARKET = D.MARKET AND B.PACK_COD = D.PACK_COD
LEFT JOIN (SELECT MARKET,DATA_SOURCE,MAX(MARKETDESC) MARKET_DESC FROM DM.DM_TD_EXTERNAL_KEYCOMPATITOR GROUP BY MARKET,DATA_SOURCE) G ON B.MARKET = G.MARKET AND UPPER(G.DATA_SOURCE) = 'IQVIA-CHPA(MONTHLY)'
GROUP BY B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET) B
WHERE B.PACK_COD IS NOT NULL AND B.MARKET IS NOT NULL
UNION ALL 
--------------------------------------------------------------------Retail------------------------------------------------------------------
SELECT
    B.PACK_COD,
    B.PACK_DES,
    B.PROD_DES,
    B.PROD_DES_C,
    CASE
        when B.MARKET = 'Non AZ Retail Related Market'
        and b.TA = 'CV' THEN 'CV Market'
        else b.MARKET
    end MARKET,
    CASE
        WHEN B.MARKET = 'RE Market' THEN 'RE' 
        when B.TA = 'RE' THEN NULL--只有 'RE Market (Global)' 的ta是 RE
        WHEN B.MARKET = 'Soliris Related Market' THEN 'RARE'--20250701 chenwu 新增RARE 
        ELSE b.TA 
    END TA,
    B.KEY_COMPETITOR,
    CASE
        WHEN B.MARKET = 'RE Market' THEN 'RE' 
        when B.TA = 'RE' THEN NULL--只有 'RE Market (Global)' 的ta是 RE
        WHEN B.MARKET = 'CVRM_OTHER Market' THEN 'CVRM'
        WHEN B.MARKET = 'Soliris Related Market' THEN 'RARE'--20250701 chenwu 新增RARE 
        ELSE b.TA
    END AZ_RELATED,
    B.CLASS,
    nvl(B.VALUE_MARKET_RATIO, 1),
    nvl(B.UNIT_MARKET_RATIO, 1),
    nvl(B.UNIT_MARKET_RATIO, 1) COUNTINGUNIT_MARKET_RATIO, --应该取unit的值作为count --,1 COUNTINGUNIT_MARKET_RATIO 
    nvl(B.PDOT_MARKET_RATIO, 1),
    B.MARKET_DESC,
    -- CASE WHEN b.TA='DM' THEN  1 WHEN b.TA ='CV' OR B.MARKET='CV Market' THEN 2 WHEN b.TA ='RD' THEN 3 WHEN b.MARKET ='RE Market' OR b.TA ='RE' or b.MARKET =  'Inhaled Extended Market by Brand' THEN 4 WHEN b.TA ='GI' THEN 5 WHEN b.TA ='ONCO' THEN 6 
    CASE
        WHEN b.TA = 'DM' THEN 1
        WHEN b.TA = 'CV' OR B.MARKET = 'CV Market' THEN 2
        WHEN b.TA = 'RD' THEN 3
        WHEN b.MARKET = 'RE Market' THEN 4
        WHEN b.TA = 'GI' THEN 5
        WHEN b.TA = 'ONCO' THEN 6
        WHEN B.MARKET = 'CVRM_OTHER Market' THEN 0
        WHEN B.MARKET = 'Soliris Related Market' THEN 8--20250701 chenwu 新增RARE 
        ELSE 99
    END TA_RN,
    'Retail(Quarterly)' DATA_SOURCE,
    '' as dept_name,
    '' as key_dept_name,
    '' as new_code,
    '' as area
FROM
    (
        SELECT
            a.MARKET_PACK_KEY,
            B.iqvia_pack_code PACK_COD,
            a.PACK_DES,
            a.PROD_DES,
            a.PROD_DES_C,
            a.MARKET,
            MAX(a.TA) TA,
            MAX(a.KEY_COMPETITOR) KEY_COMPETITOR,
            MAX(a.CLASS) CLASS,
            MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO,
            MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO,
            MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO,
            MAX(D.COUNTINGUNIT_MARKET_RATIO) PDOT_MARKET_RATIO,
            max(g.MARKET_DESC) MARKET_DESC
        FROM
            DM.dm_zk_retail_pack_property a
            LEFT JOIN DM.dm_zk_retail_market_property B ON A.iqvia_pack_code = B.iqvia_pack_code
            and a.market = b.market
            LEFT JOIN dm.dm_zk_retail_market_ratio D ON B.MARKET = D.MARKET
            AND B.iqvia_pack_code = D.pack_cod
            LEFT JOIN (
                SELECT
                    MARKET,
                    DATA_SOURCE,
                    MAX(MARKETDESC) MARKET_DESC
                FROM
                    DM.DM_TD_EXTERNAL_KEYCOMPATITOR
                GROUP BY
                    MARKET,
                    DATA_SOURCE
            ) G ON B.MARKET = G.MARKET
            AND UPPER(G.DATA_SOURCE) = 'RETAIL(QUARTERLY)'
        WHERE
            A.iqvia_pack_code in (
                select
                    distinct iqvia_pack_code
                from
                    dm.dm_zk_retail_sales
            )
        GROUP BY
            a.MARKET_PACK_KEY,
            B.iqvia_pack_code,
            a.PACK_DES,
            a.PROD_DES,
            a.PROD_DES_C,
            a.MARKET
    ) b
--------------------------------------------------------DTP--------------------------------------------------------
UNION ALL 
SELECT B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C
,MARKET
,CASE 
    WHEN B.TA IS NULL AND MARKET = 'ONC Market' 
    THEN 'ONCO'
ELSE B.TA END AS TA
,B.KEY_COMPETITOR
,CASE 
    WHEN B.TA IS NULL AND MARKET = 'ONC Market' 
    THEN 'ONCO'
    ELSE B.TA END AS AZ_RELATED
,B.CLASS
,nvl(B.VALUE_MARKET_RATIO,1)
,nvl(B.UNIT_MARKET_RATIO,1)
,nvl(B.UNIT_MARKET_RATIO,1) COUNTINGUNIT_MARKET_RATIO --应该取unit的值作为count--,1 COUNTINGUNIT_MARKET_RATIO 
,nvl(B.PDOT_MARKET_RATIO,1)
,B.MARKET_DESC,
CASE 
    WHEN B.TA IS NULL AND MARKET = 'ONC Market' THEN 6
    WHEN B.TA IS NULL THEN 99
    WHEN B.TA ='CVRM' THEN 0
    WHEN B.TA ='DM' THEN 1
    WHEN B.TA ='CV' THEN 2
    WHEN B.TA ='RD' THEN 3
    WHEN B.TA ='RE' THEN 4
    WHEN B.TA ='GI' THEN 5
    WHEN B.TA ='ONCO' THEN 6
    WHEN B.TA ='NIAD' THEN 7
    WHEN B.TA ='RARE' THEN 8
    WHEN B.TA ='NP' THEN 9   
    ELSE 99 END TA_RN
,'DTP(Quarterly)' DATA_SOURCE
,'' as dept_name
,'' as key_dept_name
,'' as new_code
,'' as area
FROM (
SELECT a.MARKET_PACK_KEY,a.iqvia_pack_code PACK_COD,a.PACK_DES,a.PROD_DES,a.PROD_DES_C
,a.MARKET,max(t3.TA) AS TA,max(t3.TA_ORDER) AS TA_ORDER
,MAX(a.KEY_COMPETITOR) KEY_COMPETITOR,MAX(a.CLASS) CLASS,MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO,MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO,MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO,MAX(D.COUNTINGUNIT_MARKET_RATIO) PDOT_MARKET_RATIO,max(g.MARKET_DESC) MARKET_DESC
FROM DM.dm_zk_retail_dtp_pack_property a
LEFT JOIN dm.dm_zk_retail_market_ratio D ON a.MARKET = D.MARKET AND a.iqvia_pack_code = D.pack_cod
LEFT JOIN (SELECT MARKET,DATA_SOURCE,MAX(MARKETDESC) MARKET_DESC FROM DM.DM_TD_EXTERNAL_KEYCOMPATITOR GROUP BY MARKET,DATA_SOURCE) G ON a.MARKET = G.MARKET AND UPPER(G.DATA_SOURCE) = 'RETAIL(QUARTERLY)'
LEFT JOIN (
    select 
        MARKET,IF(TA = 'ONC' ,'ONCO',TA) AS TA,TA_ORDER
    from dwd.dwd_td_ims_market_ta_order
    group by 1,2,3) t3 ON a.MARKET = t3.market
WHERE A.iqvia_pack_code in (select distinct iqvia_pack_code from dm.dm_zk_retail_dtp_sales)
GROUP BY a.MARKET_PACK_KEY,A.iqvia_pack_code,a.PACK_DES,a.PROD_DES,a.PROD_DES_C,a.MARKET
) b

--------------------------------------------------------RETAIL all market--------------------------------------------------------
union all 
SELECT B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C
,'Retail All Market' MARKET ,TA,b.KEY_COMPETITOR,null  AZ_RELATED,null CLASS,
1 VALUE_MARKET_RATIO,1 UNIT_MARKET_RATIO,1 COUNTINGUNIT_MARKET_RATIO,1 PDOT_MARKET_RATIO,null MARKET_DESC,null TA_RN
,'Retail(Quarterly)' DATA_SOURCE
,'' as dept_name
,'' as key_dept_name
,'' as new_code
,'' as area
FROM (
SELECT a.iqvia_pack_code PACK_COD,a.PACK_DES,a.PROD_DES,a.PROD_DES_C,null TA,MAX(a.KEY_COMPETITOR) KEY_COMPETITOR
FROM DM.dm_zk_retail_pack_property a
WHERE A.iqvia_pack_code in (select distinct iqvia_pack_code from dm.dm_zk_retail_sales)
GROUP BY A.iqvia_pack_code,a.PACK_DES,a.PROD_DES,a.PROD_DES_C
union 
select a.pack_code,nvl(b.pack_des,c.pack_des),nvl(b.PROD_DES,c.PROD_DES),nvl(b.PROD_DES_C,c.PROD_DES_C),null ta,MAX(b.KEY_COMPETITOR) KEY_COMPETITOR
from dm.dm_zk_retail_sales_kpi a 
left join dm.dm_zk_retail_pack_property b  on a.pack_code =b.iqvia_pack_code
left join (select distinct iqvia_pack_code,pack_des,PROD_DES,PROD_DES_C from dm.dm_zk_retail_pack_property ) c on a.pack_code =c.iqvia_pack_code
where a.pack_code <>''
--排除Others以及null
and NVL(a.pack_code,'Others') <> 'Others'
GROUP BY  a.pack_code,nvl(b.pack_des,c.pack_des),nvl(b.PROD_DES,c.PROD_DES),nvl(b.PROD_DES_C,c.PROD_DES_C)
) b

--------------------------------------------------------DTP all market--------------------------------------------------------
union all 
SELECT B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C
,'DTP ALL Market' MARKET ,TA,b.KEY_COMPETITOR,null  AZ_RELATED,null CLASS,
1 VALUE_MARKET_RATIO,1 UNIT_MARKET_RATIO,1 COUNTINGUNIT_MARKET_RATIO,1 PDOT_MARKET_RATIO,null MARKET_DESC,null TA_RN
,'DTP(Quarterly)' DATA_SOURCE
,'' as dept_name
,'' as key_dept_name
,'' as new_code
,'' as area
FROM (
  SELECT a.iqvia_pack_code PACK_COD,a.PACK_DES,a.PROD_DES,a.PROD_DES_C,null TA,MAX(a.KEY_COMPETITOR) KEY_COMPETITOR
FROM DM.dm_zk_retail_dtp_pack_property a
WHERE A.iqvia_pack_code in (select distinct iqvia_pack_code from dm.dm_zk_retail_dtp_sales)
GROUP BY A.iqvia_pack_code,a.PACK_DES,a.PROD_DES,a.PROD_DES_C
) B

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

---------------------------------------------------------------------------------
--修改时间：20240918
--修改人：FanXuJia
--修改内容：
--计算market_ratio\pdot_ratio时，之前使用的是现成的dws表，但是这个dws表不是外部数据报告的，更新频率也不是每天更新。会存在一定的更新时间差。
--因此改成使用dwd层表
-----------------------------------------------------------------------------------
INSERT into dm.dm_td_external_market_pack_mapping_temp(
  PACK_COD,PACK_DES,PROD_DES,PROD_DES_C,MARKET,TA,KEY_COMPETITOR,AZ_RELATED,CLASS,
  VALUE_MARKET_RATIO,UNIT_MARKET_RATIO,COUNTINGUNIT_MARKET_RATIO,PDOT_MARKET_RATIO,MARKET_DESC,TA_RN,DATA_SOURCE,dept_name,key_dept_name
  ,new_code
  ,area
)
WITH county_brand_ratio as (
  SELECT distinct market,
         CASE WHEN length(cmps_cod) < 6 THEN RIGHT(CONCAT('000000',cmps_cod),6) ELSE cmps_cod END AS cmps_cod,
         cmps_des,
         CASE WHEN length(pack_cod) < 12 and pack_cod REGEXP '^[0-9]' THEN RIGHT(CONCAT('000000000000',pack_cod),12) ELSE pack_cod END AS pack_cod,
         pack_des,
         level,
         type,
         ratio
  from dwd.dwd_gnd_ims_tblBrandRatio
)
,MKT_PACK_RATIO_tmp as (
select t1.market,
t1.PACK_COD,
--优先取pack粒度的ratio
case when t2.ratio is not null then t1.Market_Ratio * t2.ratio
     else t1.Market_Ratio * NVL(t3.ratio,1)
end as Value_Market_Ratio,
case when t4.ratio is not null then t1.Market_Ratio * t4.ratio
     else t1.Market_Ratio * NVL(t5.ratio,1)
end as Unit_Market_Ratio,
t1.Market_Ratio as CountingUnit_Market_Ratio,
case when t6.ratio is not null then t1.Market_Ratio * t6.ratio
     else t1.Market_Ratio * NVL(t7.ratio,1)
end as PDOT_Market_Ratio
from DWS.dws_ims_td_market t1
--value by Pack
left join (select * from county_brand_ratio where UPPER(TYPE) = 'VALUE' and UPPER(Level) = 'PACK') t2
on t1.MARKET = t2.market
and t1.PACK_COD = t2.pack_cod
--value by MOLECULE
LEFT JOIN (select * from county_brand_ratio where UPPER(TYPE) = 'VALUE' and UPPER(Level) = 'MOLECULE') t3
on t1.MARKET = t3.market
and t1.CMPS_COD = t3.cmps_cod
--unit by Pack
left join (select * from county_brand_ratio where UPPER(TYPE) = 'UNIT' and UPPER(Level) = 'PACK') t4
on t1.MARKET = t4.market
and t1.PACK_COD = t4.pack_cod
--unit by MOLECULE
LEFT JOIN (select * from county_brand_ratio where UPPER(TYPE) = 'UNIT' and UPPER(Level) = 'MOLECULE') t5
on t1.MARKET = t5.market
and t1.CMPS_COD = t5.cmps_cod
--PDOT by Pack
left join (select * from county_brand_ratio where UPPER(TYPE) = 'COUNTINGUNIT' and UPPER(Level) = 'PACK') t6
on t1.MARKET = t6.market
and t1.PACK_COD = t6.pack_cod
--PDOT by MOLECULE
LEFT JOIN (select * from county_brand_ratio where UPPER(TYPE) = 'COUNTINGUNIT' and UPPER(Level) = 'MOLECULE') t7
on t1.MARKET = t7.market
and t1.CMPS_COD = t7.cmps_cod
)
,MKT_PACK_RATIO as (
select market,
PACK_COD,
max(Value_Market_Ratio) as Value_Market_Ratio,
max(Unit_Market_Ratio) as Unit_Market_Ratio,
max(CountingUnit_Market_Ratio) as CountingUnit_Market_Ratio,
max(PDOT_Market_Ratio) as PDOT_Market_Ratio
FROM MKT_PACK_RATIO_tmp
GROUP BY market,PACK_COD
)


SELECT B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET,B.TA,B.KEY_COMPETITOR,
CASE 
WHEN B.MARKET = 'DM Market' THEN 'DM' 
WHEN B.MARKET = 'CV Market' THEN 'CV' 
WHEN B.MARKET = 'RD Market' THEN 'RD' 
WHEN B.MARKET = 'RE Market' THEN 'RE' 
WHEN B.MARKET = 'GI Market' THEN 'GI' 
WHEN B.MARKET = 'ONC Market' THEN 'ONCO' 
WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD' 
WHEN B.MARKET = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE
END AZ_RELATED
,B.CLASS,nvl(CAST(B.VALUE_MARKET_RATIO AS decimal(20,16)),1),nvl(CAST(B.UNIT_MARKET_RATIO AS decimal(20,16)),1),nvl(CAST(B.COUNTINGUNIT_MARKET_RATIO AS decimal(20,16)),1),nvl(CAST(B.PDOT_MARKET_RATIO AS decimal(20,16)),1),B.MARKET_DESC,
CASE 
WHEN B.MARKET = 'DM Market' THEN 1 
WHEN B.MARKET = 'CV Market' THEN 2 
WHEN B.MARKET = 'RD Market' THEN 3 
WHEN B.MARKET = 'RE Market' THEN 4 
WHEN B.MARKET = 'GI Market' THEN 5 
WHEN B.MARKET = 'ONC Market' THEN 6 
WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7 
WHEN B.MARKET = 'RARE Market' THEN 8--20250701 chenwu 新增RARE
ELSE 99 END TA_RN,'IQVIA-COUNTY(Quarterly)' DATA_SOURCE,'' as dept_name,'' as key_dept_name
,'' as new_code
,'' as area
FROM (
SELECT B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET,MAX(C.TA) TA,MAX(E.KEY_COMPETITOR) KEY_COMPETITOR,MAX(E.IS_AZ) IS_AZ,MAX(E.AZ_MAIN) AZ_MAIN,MAX(E.AZ_RELATED) AZ_RELATED,MAX(E.CLASS) CLASS,MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO,MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO,MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO,MAX(D.PDOT_MARKET_RATIO) PDOT_MARKET_RATIO,MAX(G.MARKET_DESC) MARKET_DESC
FROM (SELECT PACK_COD,COUNT(*) RUM FROM DM.DM_TF_EXTERNAL_SALES
-- WHERE YYYYMM >= (SELECT CAST((LEFT(MAX(YYYYMM),4)-3)*100+1 AS INT) FROM DM.DM_TF_EXTERNAL_SALES)
-- AND DATA_SOURCE = 'IQVIA-COUNTY(Quarterly)'
where DATA_SOURCE = 'IQVIA-COUNTY(Quarterly)'
GROUP BY PACK_COD) A
LEFT JOIN dm.dm_ext_county_td_pack_property  B
ON B.PACK_COD = A.PACK_COD --AND B.MARKET <> 'IMS ALL Market'
LEFT JOIN DM.DM_IMS_TD_MARKET_PROPERTY C
ON B.MARKET = C.MARKET
LEFT JOIN DM.dm_ext_county_td_pack_property E ON B.MARKET_PACK_KEY = E.MARKET_PACK_KEY
LEFT JOIN MKT_PACK_RATIO D ON B.MARKET = D.MARKET AND B.PACK_COD = D.PACK_COD
LEFT JOIN (SELECT MARKET,DATA_SOURCE,MAX(MARKETDESC) MARKET_DESC FROM DM.DM_TD_EXTERNAL_KEYCOMPATITOR GROUP BY MARKET,DATA_SOURCE) G ON B.MARKET = G.MARKET AND UPPER(G.DATA_SOURCE) = 'IQVIA-COUNTY(QUARTERLY)'
WHERE A.PACK_COD IS NOT NULL
GROUP BY B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET) B
WHERE B.PACK_COD IS NOT NULL AND B.MARKET IS NOT NULL


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

delete from dm.dm_td_external_market_pack_mapping_temp where DATA_SOURCE = 'THC(Quarterly)'

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

-----------------------------------------------THC---------------------------------------------------------------------------------------------------------
with exchc_rawdata_temp as (        --主表数据 
	select
					temp.yq,
					pack_property.iqvia_pack_code as pack_cod
	from (
			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,
				pack_cod
			from dwd.dwd_gnd_ext_thc_rawdata
	) temp
	LEFT JOIN dwd.dwd_gnd_ext_thc_pack_property pack_property
		ON temp.pack_cod = pack_property.pack_cod      
),
A as(
		SELECT 
			PACK_COD,
			COUNT(*) RUM 
		FROM exchc_rawdata_temp
		-- WHERE yq >= (SELECT CAST((LEFT(MAX(yq),4)-3)*100+1 AS INT) FROM exchc_rawdata_temp)
		GROUP BY PACK_COD
)
insert into table dm.dm_td_external_market_pack_mapping_temp
(
  PACK_COD,PACK_DES,PROD_DES,PROD_DES_C,MARKET,TA,KEY_COMPETITOR,AZ_RELATED,CLASS,
  VALUE_MARKET_RATIO,UNIT_MARKET_RATIO,COUNTINGUNIT_MARKET_RATIO,PDOT_MARKET_RATIO,MARKET_DESC,TA_RN,DATA_SOURCE,dept_name,key_dept_name
	,new_code
	,area
)
SELECT 
	B.PACK_COD,
	B.PACK_DES,
	B.PROD_DES,
	B.PROD_DES_C,
	B.MARKET,
	B.TA,
	B.KEY_COMPETITOR,
	CASE 
			WHEN B.MARKET = 'DM Market' THEN 'DM' 
			WHEN B.MARKET = 'CV Market' THEN 'CV' 
			WHEN B.MARKET = 'RD Market' THEN 'RD' 
			WHEN B.MARKET = 'RE Market' THEN 'RE'
			WHEN B.MARKET = 'GI Market' THEN 'GI' 
			WHEN B.MARKET = 'ONC Market' THEN 'ONCO' 
			WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD'
			WHEN B.MARKET = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE
			END AZ_RELATED,
	B.Class,
	cast (B.VALUE_MARKET_RATIO as double),
	cast (B.UNIT_MARKET_RATIO as double),
	cast (B.COUNTINGUNIT_MARKET_RATIO as double),
	cast (B.PDOT_MARKET_RATIO as double),
	B.MARKET_DESC,
	CASE 
			WHEN B.MARKET = 'DM Market' THEN 1
			WHEN B.MARKET = 'CV Market' THEN 2
			WHEN B.MARKET = 'RD Market' THEN 3
			WHEN B.MARKET = 'RE Market' THEN 4
			WHEN B.MARKET = 'GI Market' THEN 5 
			WHEN B.MARKET = 'ONC Market' THEN 6
			WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7 
			WHEN B.MARKET = 'RARE Market' THEN 8--20250701 chenwu 新增RARE
			ELSE 99 
	END TA_RN,
	'THC(Quarterly)' DATA_SOURCE
	,'' as dept_name
	,'' as key_dept_name
	,'' as new_code
	,'' as area
FROM (
	SELECT
		B.MARKET_PACK_KEY,
		B.PACK_COD,
		B.PACK_DES,
		B.PROD_DES,
		B.PROD_DES_C,
		B.MARKET,
		nvl(MAX(C.TA),'') as TA,
		MAX(E.KEY_COMPETITOR) as KEY_COMPETITOR,
		NULL  IS_AZ,                                        
		NULL  AZ_MAIN,                                        
		NULL  AZ_Related,
		nvl(MAX(E.CLASS),'') CLASS, 
		MAX(nvl(B.Market_Ratio,1)) as VALUE_MARKET_RATIO,   --  sales *  VALUE_MARKET_RATIO = 折算好的sales
		MAX(nvl(B.Market_Ratio,1) ) as UNIT_MARKET_RATIO,				--  unint *  UNIT_MARKET_RATIO  = 折算好的 UNIT
		MAX(nvl(B.Market_Ratio,1)) as COUNTINGUNIT_MARKET_RATIO,		--    counting_unit*COUNTINGUNIT_MARKET_RATIO = 折算好的 CU
    MAX(nvl(B.Market_Ratio,1)  * nvl(D.Ratio,1)) as  PDOT_MARKET_RATIO,	  --  counting_unit*Market_Ratio*PDOT_MARKET_RATIO = PDOT	
		max(g.MARKET_DESC) MARKET_DESC
	FROM  A
	LEFT JOIN dm.dm_thc_pack_property B  -- 以A表 pack_code 为准  获取需要的维度信息
		ON B.PACK_COD = A.PACK_COD
	LEFT JOIN DM.DM_IMS_TD_MARKET_PROPERTY C						--  THC没有TA字段，关联CHC的相关表，获取TA
		ON B.MARKET = C.MARKET
	LEFT JOIN dm.dm_thc_pack_property E 		-- MARKET_PACK_KEY 为唯一主键，获取KEY_COMPETITOR，class等信息
		ON B.MARKET_PACK_KEY = E.MARKET_PACK_KEY
	LEFT JOIN(										--转码  补0
		select  
				MARKET,
				b.iqvia_pack_code as pack_cod,
				ratio
		from dwd.dwd_gnd_ext_thc_brandratio a 
		inner join dwd.dwd_gnd_ext_thc_pack_property b 
		on (case when length(a.pack_cod) < 12 and a.pack_cod REGEXP '^[0-9]' then right(concat('000000000000',a.pack_cod),12) else a.pack_cod end )
			=(case when length(b.pack_cod) < 12 and b.pack_cod REGEXP '^[0-9]' then right(concat('000000000000',b.pack_cod),12) else b.pack_cod end )
	) D 
		ON B.MARKET = D.MARKET
		AND B.PACK_COD = D.PACK_COD
	LEFT JOIN (			--获取 MARKET_DESC  字段 
		SELECT 
			MARKET,DATA_SOURCE,
			MAX(MARKETDESC) MARKET_DESC 
		FROM DM.DM_TD_EXTERNAL_KEYCOMPATITOR 
		GROUP BY MARKET,DATA_SOURCE
	) G 
		ON B.MARKET = G.MARKET AND UPPER(G.DATA_SOURCE) = 'THC(QUARTERLY)'
	GROUP BY B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET
) B
WHERE B.PACK_COD IS NOT NULL AND B.MARKET IS NOT NULL

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

-----------------------------------------------CHC----------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------

with exchc_rawdata_temp as (        --主表数据 
        select
                temp.yq,
                pack_property.iqvia_pack_code as pack_cod
        from (
                  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,
                pack_cod
        from dwd.dwd_gnd_chc_rawdata  
        ) temp
        LEFT JOIN dwd.dwd_gnd_ext_chc_pack_property pack_property
                ON temp.pack_cod = pack_property.pack_cod      
),
A as(
        SELECT 
                PACK_COD,
                COUNT(*) RUM 
        FROM exchc_rawdata_temp
        -- WHERE yq >= (SELECT CAST((LEFT(MAX(yq),4)-3)*100+1 AS INT) FROM exchc_rawdata_temp)
        GROUP BY PACK_COD
)
insert into table dm.dm_td_external_market_pack_mapping_temp(
  PACK_COD,PACK_DES,PROD_DES,PROD_DES_C,MARKET,TA,KEY_COMPETITOR,AZ_RELATED,CLASS,
  VALUE_MARKET_RATIO,UNIT_MARKET_RATIO,COUNTINGUNIT_MARKET_RATIO,PDOT_MARKET_RATIO,MARKET_DESC,TA_RN,DATA_SOURCE,dept_name,key_dept_name
	,new_code
	,area
)
SELECT 
	B.PACK_COD,
	B.PACK_DES,
	B.PROD_DES,
	B.PROD_DES_C,
	B.MARKET,
	B.TA,
	B.KEY_COMPETITOR,
	CASE 
			WHEN B.MARKET = 'DM Market' THEN 'DM' 
			WHEN B.MARKET = 'CV Market' THEN 'CV' 
			-- WHEN B.MARKET = 'RD Market' THEN 'RD'		--RD隐藏
			when B.MARKET = 'RE Market' THEN 'RE'	--RE改为Respules Market
			WHEN B.MARKET = 'GI Market' THEN 'GI' 
			WHEN B.MARKET = 'ONC Market' THEN 'ONCO' 
			WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD'
			WHEN B.MARKET = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE
			END AZ_RELATED,
	B.Class,
	cast (B.VALUE_MARKET_RATIO as double),
	cast (B.UNIT_MARKET_RATIO as double),
	cast (B.COUNTINGUNIT_MARKET_RATIO as double),
	cast (B.PDOT_MARKET_RATIO as double),
	B.MARKET_DESC,
	CASE 
			WHEN B.MARKET = 'DM Market' THEN 1
			WHEN B.MARKET = 'CV Market' THEN 2
			-- WHEN B.MARKET = 'RD Market' THEN 3
			WHEN B.MARKET = 'RE Market' THEN 4
			WHEN B.MARKET = 'GI Market' THEN 5 
			WHEN B.MARKET = 'ONC Market' THEN 6
			WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7 
			WHEN B.MARKET = 'RARE Market' THEN 8 --20250701 chenwu 新增RARE
			ELSE 99 
	END TA_RN,
	'CHC(Quarterly)' DATA_SOURCE
	,'' as dept_name
	,'' as key_dept_name
	,'' as new_code
	,'' as area
FROM (
	SELECT
		B.MARKET_PACK_KEY,
		B.PACK_COD,
		B.PACK_DES,
		B.PROD_DES,
		B.PROD_DES_C,
		B.MARKET,
		nvl(MAX(C.TA) ,'') TA,
		-- null as TA,
		MAX(E.KEY_COMPETITOR) KEY_COMPETITOR,
		--MAX(B.KEY_COMPETITOR) KEY_COMPETITOR,
		--MAX(E.IS_AZ) IS_AZ,
		--MAX(E.AZ_MAIN) AZ_MAIN,
		--MAX(E.AZ_RELATED) AZ_RELATED,
		NULL as IS_AZ,                                        
		NULL as AZ_MAIN,                                        
		NULL as AZ_Related,
		nvl (MAX(E.CLASS) ,'' ) CLASS,
		MAX(nvl(B.Market_Ratio,1)) as VALUE_MARKET_RATIO,   --  sales *  VALUE_MARKET_RATIO = 折算好的sales
		MAX(nvl(B.Market_Ratio,1)) as UNIT_MARKET_RATIO,				--  unint *  UNIT_MARKET_RATIO  = 折算好的 UNIT
		MAX(nvl(B.Market_Ratio,1)) as COUNTINGUNIT_MARKET_RATIO,		--    counting_unit*COUNTINGUNIT_MARKET_RATIO = 折算好的 CU
    MAX(nvl(B.Market_Ratio,1)  * nvl(D.Ratio,1)) PDOT_MARKET_RATIO,	  --  counting_unit*Market_Ratio*PDOT_MARKET_RATIO = PDOT	
		max(G.MARKET_DESC)  as MARKET_DESC
	FROM  A
	LEFT JOIN dm.dm_chc_pack_property B		-- 以A表 pack_code 为准  获取需要的维度信息
		ON B.PACK_COD = A.PACK_COD
	LEFT JOIN DM.DM_IMS_TD_MARKET_PROPERTY C							--  THC没有TA字段，关联CHPA的相关表，获取TA
		ON B.MARKET = C.MARKET
	LEFT JOIN dm.dm_chc_pack_property E 		--MARKET_PACK_KEY 为唯一主键，获取KEY_COMPETITOR，class等信息
		ON B.MARKET_PACK_KEY = E.MARKET_PACK_KEY
	LEFT JOIN(																-- 获取RATIO  ，其中chc_brandratio没有补0和转码的操作，这里处理 
		select  
				MARKET,
				b.iqvia_pack_code as pack_cod,
				ratio
		from dwd.dwd_gnd_ext_chc_brandratio a   
		inner join dwd.dwd_gnd_ext_chc_pack_property b 
		on (case when length(a.pack_cod) < 12 and a.pack_cod REGEXP '^[0-9]' then right(concat('000000000000',a.pack_cod),12) else a.pack_cod end )
			=(case when length(b.pack_cod) < 12 and b.pack_cod REGEXP '^[0-9]' then right(concat('000000000000',b.pack_cod),12) else b.pack_cod end )
	) D 
		ON B.MARKET = D.MARKET
		AND B.PACK_COD = D.PACK_COD
	LEFT JOIN (																			--获取 MARKET_DESC  字段 
		SELECT 
			MARKET,DATA_SOURCE,
			MAX(MARKETDESC) MARKET_DESC 
		FROM DM.DM_TD_EXTERNAL_KEYCOMPATITOR 
		GROUP BY MARKET,DATA_SOURCE
	) G 
		ON B.MARKET = G.MARKET AND UPPER(G.DATA_SOURCE) = 'CHC(QUARTERLY)'
	GROUP BY B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET

) B
WHERE B.PACK_COD IS NOT NULL AND B.MARKET IS NOT NULL 
and market!='RD Market'

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

/*
修改人：chenwu
修改时间：20250319
修改内容：  EC的数据源cu恒等于1
*/
-----------------------------------------------------------ec-----------------------------------------------------------
INSERT into dm.dm_td_external_market_pack_mapping_temp(
  PACK_COD,PACK_DES,PROD_DES,PROD_DES_C,MARKET,TA,KEY_COMPETITOR,AZ_RELATED,CLASS,
  VALUE_MARKET_RATIO,UNIT_MARKET_RATIO,COUNTINGUNIT_MARKET_RATIO,PDOT_MARKET_RATIO,MARKET_DESC,TA_RN,DATA_SOURCE,dept_name,key_dept_name
  ,new_code
  ,area
)
SELECT
    B.PACK_COD,
    B.PACK_DES,
    B.PROD_DES,
    B.PROD_DES_C,
    B.MARKET,
    CASE 
        WHEN B.MARKET = 'RE Market' THEN 'RE' 
        when B.TA = 'RE' THEN NULL--只有 'RE Market (Global)' 的ta是 RE
        ELSE B.TA
    END TA,
    B.KEY_COMPETITOR,
    CASE
        WHEN B.MARKET = 'RE Market' THEN 'RE'
        WHEN B.TA = 'RE' THEN NULL
        ELSE B.TA
    END AZ_RELATED,
    B.CLASS,
    nvl(B.VALUE_MARKET_RATIO, 1),
    nvl(B.UNIT_MARKET_RATIO, 1),
    1,
    --nvl(B.COUNTINGUNIT_MARKET_RATIO,1), --ec的数据源cu恒等于1
    nvl(B.PDOT_MARKET_RATIO, 1),
    B.MARKET_DESC,
    CASE
        WHEN b.TA = 'DM' THEN 1
        WHEN b.TA = 'CV' THEN 2
        WHEN b.TA = 'RE' THEN 4
        WHEN b.TA = 'GI' THEN 5
        WHEN B.TA = 'CVRM' THEN 0
        ELSE 99
    END TA_RN,
    'EC(Monthly)' DATA_SOURCE,
    '' as dept_name,
    '' as key_dept_name,
    '' as new_code,
    '' as area
FROM
    (
        SELECT
            a.MARKET_PACK_KEY,
            B.iqvia_pack_code PACK_COD,
            a.PACK_DES,
            a.PROD_DES,
            a.PROD_DES_C,
            a.MARKET,
            MAX(a.TA) TA,
            MAX(a.KEY_COMPETITOR) KEY_COMPETITOR,
            MAX(a.CLASS) CLASS,
            MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO,
            MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO,
            MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO,
            MAX(D.COUNTINGUNIT_MARKET_RATIO) PDOT_MARKET_RATIO,
            max(g.MARKET_DESC) MARKET_DESC
        FROM
            DM.dm_zk_ec_pack_property a
            LEFT JOIN DM.dm_zk_ec_market_property B ON A.iqvia_pack_code = B.iqvia_pack_code
            and a.market = b.market
            LEFT JOIN dm.dm_zk_ec_market_ratio D ON B.MARKET = D.MARKET
            AND B.iqvia_pack_code = D.PACK_COD
            LEFT JOIN (
                SELECT
                    MARKET,
                    DATA_SOURCE,
                    MAX(MARKETDESC) MARKET_DESC
                FROM
                    DM.DM_TD_EXTERNAL_KEYCOMPATITOR
                GROUP BY
                    MARKET,
                    DATA_SOURCE
            ) G ON B.MARKET = G.MARKET
            AND UPPER(G.DATA_SOURCE) = 'EC(MONTHLY)'
        WHERE
            A.iqvia_pack_code in (
                select
                    distinct iqvia_pack_code
                from
                    dm.dm_zk_ec_sales
            )
        GROUP BY
            a.MARKET_PACK_KEY,
            B.iqvia_pack_code,
            a.PACK_DES,
            a.PROD_DES,
            a.PROD_DES_C,
            a.MARKET
    ) b


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


-----------------------------------------------AIA----------------------------------------------------------------------------------
WITH AIA_brand_ratio as (
  SELECT distinct market,
         CASE WHEN length(cmps_cod) < 6 THEN RIGHT(CONCAT('000000',cmps_cod),6) ELSE cmps_cod END AS cmps_cod,
         cmps_des,
         CASE WHEN length(pack_cod) < 12 and pack_cod REGEXP '^[0-9]' THEN RIGHT(CONCAT('000000000000',pack_cod),12) ELSE pack_cod END AS pack_cod,
         pack_des,
         level,
         type,
         ratio
  from dwd.dwd_gnd_ims_tblBrandRatio
)
,AIA_RATIO as (
select t1.PACK_COD,
t1.MARKET,
case when t6.ratio is not null then t1.market_ratio * t6.ratio
     else t1.market_ratio * NVL(t7.ratio,1)
end as VALUE_MARKET_RATIO,
case when t2.ratio is not null then t1.market_ratio * t2.ratio
     else t1.market_ratio * NVL(t3.ratio,1)
end as UNIT_MARKET_RATIO,
t1.market_ratio as COUNTINGUNIT_MARKET_RATIO,
case when t4.ratio is not null then t1.market_ratio * t4.ratio
     else t1.market_ratio * NVL(t5.ratio,1)
end as PDOT_MARKET_RATIO
from dm.dm_aia_pack_property t1
--unit by Pack
left join (select * from AIA_brand_ratio where upper(type) = 'UNIT' and upper(Level) = 'PACK') t2
on t1.PACK_COD = t2.pack_cod
and t1.MARKET = t2.market
--unit by MOLECULE
left join (select * from AIA_brand_ratio where upper(type) = 'UNIT' and upper(Level) = 'MOLECULE') t3
on t1.CMPS_COD = t3.cmps_cod
and t1.MARKET = t3.market
--PDOT by Pack
left join (select * from AIA_brand_ratio where upper(type) = 'COUNTINGUNIT' and upper(Level) = 'PACK') t4
on t1.PACK_COD = t4.pack_cod
and t1.MARKET = t4.market
--PDOT by MOLECULE
left join (select * from AIA_brand_ratio where upper(type) = 'COUNTINGUNIT' and upper(Level) = 'MOLECULE') t5
on t1.CMPS_COD = t5.cmps_cod
and t1.MARKET = t5.market
--VALUE by Pack
left join (select * from AIA_brand_ratio where upper(type) = 'VALUE' and upper(Level) = 'PACK') t6
on t1.PACK_COD = t6.pack_cod
and t1.MARKET = t6.market
--VALUE by MOLECULE
left join (select * from AIA_brand_ratio where upper(type) = 'VALUE' and upper(Level) = 'MOLECULE') t7
on t1.CMPS_COD = t7.cmps_cod
and t1.MARKET = t7.market

)
,MARKET_PACK_MAPPING_1 as (
  select
      B.MARKET_PACK_KEY,
		  B.PACK_COD,
		  B.PACK_DES,
	  	B.PROD_DES,
	  	B.PROD_DES_C,
		  B.MARKET as MARKET_AIA,
    --AIA有一部分market有通过手工表AIA_IMS_Market_Mapping更新过名称
    --使用旧的从CHPA里来的名称关联
      B.MARKET_CHPA,
		  nvl(MAX(C.TA),'') as TA,
      MAX(E.KEY_COMPETITOR) KEY_COMPETITOR,
      NULL as IS_AZ,
      NULL as AZ_MAIN,
      NULL as AZ_Related,
      nvl(MAX(E.CLASS),'') as CLASS,
      MAX(t1.VALUE_MARKET_RATIO) as VALUE_MARKET_RATIO,   --  sales *  VALUE_MARKET_RATIO = 折算好的sales
      MAX(t1.UNIT_MARKET_RATIO) as UNIT_MARKET_RATIO,				--  unint *  UNIT_MARKET_RATIO  = 折算好的 UNIT
      MAX(t1.COUNTINGUNIT_MARKET_RATIO) as COUNTINGUNIT_MARKET_RATIO,		--    counting_unit*COUNTINGUNIT_MARKET_RATIO = 折算好的 CU
      MAX(t1.PDOT_MARKET_RATIO) as PDOT_MARKET_RATIO,                   --  counting_unit*Market_Ratio*PDOT_MARKET_RATIO = PDOT
      max(G.MARKET_DESC)  as MARKET_DESC
  from (
    SELECT 
        PACK_COD,
        COUNT(*) RUM 
    FROM DM.DM_TF_EXTERNAL_SALES
    WHERE DATA_SOURCE = 'AIA(Monthly)'
    GROUP BY PACK_COD
      ) A
    left join dm.dm_aia_pack_property B
    on A.PACK_COD = B.PACK_COD
    left join DM.DM_IMS_TD_MARKET_PROPERTY C
    --AIA有一部分market有通过手工表AIA_IMS_Market_Mapping更新过名称
    --使用旧的从CHPA里来的名称关联
    on B.MARKET_CHPA = C.MARKET
    left join dm.dm_aia_pack_property E
    on B.MARKET_PACK_KEY = E.MARKET_PACK_KEY
    left join AIA_RATIO t1
    on B.PACK_COD = t1.PACK_COD
    and B.MARKET = t1.MARKET
    left join (
      select MARKET,DATA_SOURCE,
          MAX(MARKETDESC) MARKET_DESC
      from DM.DM_TD_EXTERNAL_KEYCOMPATITOR
      group by market,DATA_SOURCE
      ) G
      --竞品配置信息表里使用的AIA的市场名称，因此使用AIA市场名关联
    on B.MARKET = G.MARKET and upper(G.DATA_SOURCE) = 'AIA(Monthly)'
    group by B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET,B.MARKET_CHPA
)


insert into table dm.dm_td_external_market_pack_mapping_temp (
PACK_COD,
PACK_DES,
PROD_DES,
PROD_DES_C,
MARKET,
TA,
KEY_COMPETITOR,
AZ_RELATED,
CLASS,
VALUE_MARKET_RATIO,
UNIT_MARKET_RATIO,
COUNTINGUNIT_MARKET_RATIO,
PDOT_MARKET_RATIO,
MARKET_DESC,
TA_RN,
DATA_SOURCE,
dept_name,
key_dept_name,
new_code,
area
)
  select 
      PACK_COD,
      PACK_DES,
      PROD_DES,
      PROD_DES_C,
      MARKET_AIA as MARKET,
      TA,
      KEY_COMPETITOR,
      case when MARKET_CHPA = 'DM Market' THEN 'DM'
           when MARKET_CHPA = 'CV Market' THEN 'CV'
           when MARKET_CHPA = 'RD Market' THEN 'RD'
           when MARKET_CHPA = 'RE Market' THEN 'RE'
           when MARKET_CHPA = 'GI Market' THEN 'GI'
           when MARKET_CHPA = 'ONC Market' THEN 'ONCO'
           when MARKET_CHPA = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD'
           WHEN MARKET_CHPA = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE
      end as AZ_RELATED,
      Class,
      cast(VALUE_MARKET_RATIO as double) as VALUE_MARKET_RATIO,
      cast(UNIT_MARKET_RATIO as double) as UNIT_MARKET_RATIO,
      cast(COUNTINGUNIT_MARKET_RATIO as double) as COUNTINGUNIT_MARKET_RATIO,
      cast(PDOT_MARKET_RATIO as double) as PDOT_MARKET_RATIO,
      MARKET_DESC,
      case when MARKET_CHPA = 'DM Market' THEN 1
           when MARKET_CHPA = 'CV Market' THEN 2
           when MARKET_CHPA = 'RD Market' THEN 3
           when MARKET_CHPA = 'RE Market' THEN 4
           when MARKET_CHPA = 'GI Market' THEN 5
           when MARKET_CHPA = 'ONC Market' THEN 6
           when MARKET_CHPA = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7
           WHEN MARKET_CHPA = 'RARE Market' THEN 8--20250701 chenwu 新增RARE
           else 99
      end as TA_RN,
      'AIA(Monthly)' as DATA_SOURCE
      ,'' as dept_name
      ,'' as key_dept_name
      ,'' as new_code
      ,'' as area
  from MARKET_PACK_MAPPING_1
  where PACK_COD IS NOT NULL
  and MARKET_AIA IS NOT NULL


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

INSERT into table dm.dm_td_external_market_pack_mapping_temp(
    PACK_COD,
    PACK_DES,
    PROD_DES,
    PROD_DES_C,
    MARKET,
    TA,
    KEY_COMPETITOR,
    AZ_RELATED,
    CLASS,
    VALUE_MARKET_RATIO,
    UNIT_MARKET_RATIO,
    COUNTINGUNIT_MARKET_RATIO,
    PDOT_MARKET_RATIO,
    MARKET_DESC,
    TA_RN,
    DATA_SOURCE,
    dept_name,
    key_dept_name,
    new_code,
    area
  )
  
  WITH MKT_PACK_RATIO AS (
    -------------------------------------根据沟通，市场+产品对应的ratio是唯一的------------------------------------------
    SELECT
      A.MARKET,
      A.PACK_COD,
      MAX(A.Value_Market_Ratio) Value_Market_Ratio,
      MAX(A.Unit_Market_Ratio) Unit_Market_Ratio,
      MAX(A.PDOT_Market_Ratio) CountingUnit_Market_Ratio,
      MAX(A.CountingUnit_Market_Ratio) PDOT_Market_Ratio --因为之前的报告在使用源表的数据，因此命名是反的。
    FROM
      DWS.dws_ims_td_market_ratio A
    WHERE
      A.YM = (
        SELECT
          MAX(YM)
        FROM
          DWS.dws_ims_td_market_ratio
      )
    GROUP BY
      A.MARKET,
      A.PACK_COD
  ) 
------------------------------------------------------------------xiehe------------------------------------------------------------------
SELECT
  B.PACK_COD,
  B.PACK_DES,
  B.PROD_DES,
  B.PROD_DES_C,
  B.MARKET,
  B.TA,
  B.KEY_COMPETITOR,
  CASE
    WHEN B.MARKET = 'DM Market' THEN 'DM'
    WHEN B.MARKET = 'CV Market' THEN 'CV'
    WHEN B.MARKET = 'RD Market' THEN 'RD'
    WHEN B.MARKET = 'RE Market' THEN 'RE'
    WHEN B.MARKET IN ('GI Market', 'Retail_GI Market') THEN 'GI'
    WHEN B.MARKET = 'ONC Market' THEN 'ONCO'
    WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD'
    WHEN B.MARKET = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE
  END AZ_RELATED,
  B.CLASS,
  NVL(B.VALUE_MARKET_RATIO, 1) VALUE_MARKET_RATIO,
  NVL(B.UNIT_MARKET_RATIO, 1) UNIT_MARKET_RATIO,
  NVL(B.COUNTINGUNIT_MARKET_RATIO, 1) COUNTINGUNIT_MARKET_RATIO,
  NVL(B.PDOT_MARKET_RATIO, 1) PDOT_MARKET_RATIO,
  B.MARKET_DESC,
  CASE
    WHEN B.MARKET = 'DM Market' THEN 1
    WHEN B.MARKET = 'CV Market' THEN 2
    WHEN B.MARKET = 'RD Market' THEN 3
    WHEN B.MARKET = 'RE Market' THEN 4
    WHEN B.MARKET = 'GI Market' THEN 5
    WHEN B.MARKET = 'ONC Market' THEN 6
    WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7
    WHEN B.MARKET = 'RARE Market' THEN 8 --20250701 chenwu 新增RARE
    ELSE 99
  END TA_RN,
  'XH Data(Quarterly)' DATA_SOURCE,
  B.dept_name,
  B.key_dept_name,
  B.new_code,
  B.area
FROM
  (
    SELECT
      B.MARKET_PACK_KEY,
      B.PACK_COD,
      B.PACK_DES,
      B.PROD_DES,
      B.PROD_DES_C,
      B.MARKET,
      NVL(H.dept_name,'') dept_name,
      NVL(t1.CORE_DEPT,'Others') as key_dept_name,
      NVL(H.new_code,'') new_code,
      NVL(H.area,'') area,
      MAX(C.TA) TA,
      MAX(E.KEY_COMPETITOR) KEY_COMPETITOR,
      MAX(E.IS_AZ) IS_AZ,
      MAX(E.AZ_MAIN) AZ_MAIN,
      MAX(E.AZ_RELATED) AZ_RELATED,
      MAX(E.CLASS) CLASS,
      MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO,
      MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO,
      MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO,
      MAX(D.PDOT_MARKET_RATIO) PDOT_MARKET_RATIO,
      max(g.MARKET_DESC) MARKET_DESC
    FROM
      (
        SELECT
          PACK_COD,
          COUNT(*) RUM
        FROM
          DM.DM_TF_EXTERNAL_SALES
        WHERE DATA_SOURCE = 'XH Data(Quarterly)'
        GROUP BY
          PACK_COD
      ) A
      LEFT JOIN dm.dm_xiehe_pack_property B ON B.PACK_COD = A.PACK_COD --AND B.MARKET <> 'IMS ALL Market'
      LEFT JOIN DM.DM_IMS_TD_MARKET_PROPERTY C ON B.MARKET = C.MARKET
      LEFT JOIN dm.dm_xiehe_pack_property E ON B.MARKET_PACK_KEY = E.MARKET_PACK_KEY
      LEFT JOIN MKT_PACK_RATIO D ON B.MARKET = D.MARKET
      AND B.PACK_COD = D.PACK_COD
      LEFT JOIN (
        SELECT
          MARKET,DATA_SOURCE,
          MAX(MARKETDESC) MARKET_DESC
        FROM
          DM.DM_TD_EXTERNAL_KEYCOMPATITOR
        GROUP BY
          MARKET,DATA_SOURCE
      ) G ON B.MARKET = G.MARKET and G.DATA_SOURCE = 'XH Data(Quarterly)'
      left join (
        SELECT distinct PACK_COD,dept_name,new_code,area
        from dm.dm_td_external_packinfo_temp2
        where DATA_SOURCE = 'XH Data(Quarterly)'
      ) H
      on A.PACK_COD = H.PACK_COD
      left join dm.dm_td_xiehe_core_dept t1
      on B.MARKET = t1.MARKET
      AND H.dept_name = t1.Dept_NAME
    GROUP BY
      B.MARKET_PACK_KEY,
      B.PACK_COD,
      B.PACK_DES,
      B.PROD_DES,
      B.PROD_DES_C,
      B.MARKET,
      H.dept_name,
      H.new_code,
      H.area,
      t1.CORE_DEPT
  ) B
WHERE
  B.PACK_COD IS NOT NULL
  AND B.MARKET IS NOT NULL

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

--修改时间：20240808
--修改人：FanXuJia
--修改背景：
--事实表与维度表来自于不同手工表，目前存在一部分pack_cod在事实表有销量，但是在维度表没有这个pack_cod，导致销量缺失。
--因此需要从事实表补全这部分pack_cod
with tmp_market_pack as (
select distinct 
  t1.PACK_COD,
  CASE WHEN t1.DATA_SOURCE = 'CHC(Quarterly)' then 'CHC ALL Market'
       WHEN t1.DATA_SOURCE = 'IQVIA-CHPA(Monthly)' then 'IMS ALL Market'
       WHEN t1.DATA_SOURCE = 'IQVIA-COUNTY(Quarterly)' then 'IMS ALL Market'
       WHEN t1.DATA_SOURCE = 'Retail(Quarterly)' then 'Retail All Market'
       WHEN t1.DATA_SOURCE = 'THC(Quarterly)' then 'THC ALL Market'
       WHEN t1.DATA_SOURCE = 'EC(Monthly)' then 'EC ALL Market'
       WHEN t1.DATA_SOURCE = 'AIA(Monthly)' then 'NON Market'
       WHEN t1.DATA_SOURCE = 'XH Data(Quarterly)' then 'XIEHE ALL Market'
       WHEN t1.DATA_SOURCE = 'DTP(Quarterly)' then 'DTP ALL Market'
  end as market,
  t1.DATA_SOURCE
from external_sales_union t1
left join dm.dm_td_external_market_pack_mapping_temp t2
on t1.PACK_COD = t2.PACK_COD
and t1.DATA_SOURCE = t2.DATA_SOURCE
where t2.PACK_COD is null
)
,tmp_packinfo as (
select
PACK_COD,
new_code,
area,
dept_name,
DATA_SOURCE,
max(PACK_DES) as PACK_DES,
max(PROD_DES) as PROD_DES,
max(PROD_DES_C) as PROD_DES_C
from dm.dm_td_external_packinfo_temp2
group by PACK_COD,DATA_SOURCE,dept_name,new_code,area
)

INSERT into table dm.dm_td_external_market_pack_mapping_temp(
    PACK_COD,
    PACK_DES,
    PROD_DES,
    PROD_DES_C,
    MARKET,
    TA,
    KEY_COMPETITOR,
    AZ_RELATED,
    CLASS,
    VALUE_MARKET_RATIO,
    UNIT_MARKET_RATIO,
    COUNTINGUNIT_MARKET_RATIO,
    PDOT_MARKET_RATIO,
    MARKET_DESC,
    TA_RN,
    DATA_SOURCE,
    dept_name,
    key_dept_name,
    new_code,
    area
  )
  select 
    t1.PACK_COD,
    t2.PACK_DES,
    t2.PROD_DES,
    t2.PROD_DES_C,
    t1.MARKET,
    null as TA,
    null as KEY_COMPETITOR,
    null as AZ_RELATED,
    null as CLASS,
    1 as VALUE_MARKET_RATIO,
    1 as UNIT_MARKET_RATIO,
    1 as COUNTINGUNIT_MARKET_RATIO,
    1 as PDOT_MARKET_RATIO,
    null as MARKET_DESC,
    null as TA_RN,
    t1.DATA_SOURCE,
    t2.dept_name,
    NVL(t3.CORE_DEPT,'Others') as key_dept_name,
    t2.new_code,
    t2.area
    from tmp_market_pack t1
    left join tmp_packinfo t2
    on t1.PACK_COD = t2.PACK_COD
    and t1.DATA_SOURCE = t2.DATA_SOURCE
    left join dm.dm_td_xiehe_core_dept t3
    on t1.MARKET = t3.MARKET
    AND t2.dept_name = t3.Dept_NAME


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

-----------------------------------------------------------
--修改时间：20241118
--修改人：Fanxujia
--修改内容：
--使用手工文件排除掉每个渠道的不需要显示的市场，不再Hardcoding
-----------------------------------------------------------
-- ----------------------------------------------------------------------------
-- --修改时间：20240813
-- --修改人：FanXujia
-- --修改内容：CHC_AZ related TA ALL这个市场只在CHC中显示，其他渠道排除这个市场（Hard Coding）
-- ----------------------------------------------------------------------------
-- delete from dm.dm_td_external_market_pack_mapping_temp
-- where upper(MARKET) = 'CHC_AZ RELATED TA ALL'
-- and data_source <> 'CHC(Quarterly)'

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

-----------------------------------------------------------
--修改时间：20241118
--修改人：Fanxujia
--修改内容：
--使用手工文件排除掉每个渠道的不需要显示的市场
-----------------------------------------------------------
with hidden_market as (
select distinct 
case when upper(data_source) = 'CHPA'    then 'IQVIA-CHPA(Monthly)'
     when upper(data_source) = '科室市场' then 'XH Data(Quarterly)'
     when upper(data_source) = 'EC'      then 'EC(Monthly)'
     when upper(data_source) = 'RETAIL'  then 'Retail(Quarterly)'
     when upper(data_source) = 'CHC'     then 'CHC(Quarterly)'
     when upper(data_source) = 'AIA'     then 'AIA(Monthly)'
     when upper(data_source) = 'THC'     then 'THC(Quarterly)'
     when upper(data_source) = 'COUNTY'  then 'IQVIA-COUNTY(Quarterly)'
     else data_source
end as data_source,
upper(hidden_market) as hidden_market
from dwd.dwd_gnd_ims_hidden_market
union all 
select
'AIA(Monthly)' as data_source,
'AIA ALL Market'
)


MERGE into dm.dm_td_external_market_pack_mapping_temp as t1
USING hidden_market t2
on t1.data_source = t2.data_source
and upper(t1.MARKET) = upper(t2.hidden_market)
WHEN MATCHED THEN
DELETE

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

--------------------------------------------------------------------------------
--修改时间：20241025
--修改人：Fanxujia
--修改内容：
--外部数据报告优化，将PBI的逻辑搬到DBR
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--修改时间：20241209
--修改人：Fanxujia
--修改内容：
--新增Key Competitor CN，从packinfo表里关联出中文名，关联不到的保留英文名
--------------------------------------------------------------------------------
with mapping as (
select distinct PROD_DES,PROD_DES_C
from dm.dm_td_external_packinfo
)
,market_pack_mapping_1 as (
SELECT
  PACK_COD,
  max(PROD_DES) PROD_DES,
  max(PROD_DES_C) PROD_DES_C,
  MARKET,
  ----------------------------------------------------------------------------------
  --修改时间：20241217
  --修改人：Fanxujia
  --修改内容：
  --取不到Class时，不再显示成中划线-，改成Others
  ----------------------------------------------------------------------------------
  CASE
    -- WHEN NVL(max(CLASS), '') = '' OR max(CLASS) = 'Others' THEN '-'
    WHEN NVL(max(CLASS), '') = '' THEN 'Others'
    ELSE UPPER(max(CLASS))
  END CLASS,
  CAST(max(VALUE_MARKET_RATIO) AS DECIMAL(38, 20)) VALUE_MARKET_RATIO,
  CAST(max(UNIT_MARKET_RATIO) AS DECIMAL(38, 20)) UNIT_MARKET_RATIO,
  CAST(max(COUNTINGUNIT_MARKET_RATIO) AS DECIMAL(38, 20)) COUNTINGUNIT_MARKET_RATIO,
  CAST(max(PDOT_MARKET_RATIO) AS DECIMAL(38, 20)) PDOT_MARKET_RATIO,
  DATA_SOURCE,
  case when max(KEY_COMPETITOR) is null or max(KEY_COMPETITOR) = '' then 'Others'
       else max(KEY_COMPETITOR)
  end as KEY_COMPETITOR,
  NEW_CODE
FROM DM.dm_td_external_market_pack_mapping_temp
group by PACK_COD,NEW_CODE,DATA_SOURCE,MARKET
)

, mapping_match_key_competitor_with_mutil as (
select 
t3.PACK_COD,
t3.PROD_DES,
t3.PROD_DES_C,
t3.MARKET,
t3.CLASS,
t3.VALUE_MARKET_RATIO,
t3.UNIT_MARKET_RATIO,
t3.COUNTINGUNIT_MARKET_RATIO,
t3.PDOT_MARKET_RATIO,
t3.DATA_SOURCE,
case WHEN  t3.KEY_COMPETITOR ='Others' and (UPPER(t2.keycompetitor) is not null or UPPER(t2.keycompetitor) <>'')
THEN upper(t2.keycompetitor)  ELSE t3.KEY_COMPETITOR
end as KEY_COMPETITOR,
t3.NEW_CODE
,t2.no1
 from market_pack_mapping_1 t3
left join dm.dm_td_external_packinfo t1
on t1.PACK_COD = t3.PACK_COD and t1.DATA_SOURCE = t3.DATA_SOURCE
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(t3.market  ,'') = case when t2.market is null 			  then COALESCE(t3.market,'') else t2.market end
) 

,mapping_match_key_competitor as (
  select * from (select *, row_number() over (partition by PACK_COD,MARKET,DATA_SOURCE,NEW_CODE,CLASS order by no1 desc ) as new_order FROM mapping_match_key_competitor_with_mutil ) as new
  WHERE  new.new_order =1
  )
  
insert overwrite table DM.dm_td_external_market_pack_mapping
(
PACK_COD,
PROD_DES,
PROD_DES_C,
MARKET,
CLASS,
VALUE_MARKET_RATIO,
UNIT_MARKET_RATIO,
COUNTINGUNIT_MARKET_RATIO,
PDOT_MARKET_RATIO,
DATA_SOURCE,
KEY_COMPETITOR,
KEY_COMPETITOR_CN,
NEW_CODE
)

SELECT
  t1.PACK_COD,
  t1.PROD_DES,
  t1.PROD_DES_C,
  t1.MARKET,
  t1.CLASS,
  t1.VALUE_MARKET_RATIO,
  t1.UNIT_MARKET_RATIO,
  t1.COUNTINGUNIT_MARKET_RATIO,
  t1.PDOT_MARKET_RATIO,
  t1.DATA_SOURCE,
  t1.KEY_COMPETITOR,
  COALESCE(t2.PROD_DES_C,t1.KEY_COMPETITOR) as KEY_COMPETITOR_CN,
  t1.NEW_CODE
FROM mapping_match_key_competitor t1
left join mapping t2
on upper(t1.KEY_COMPETITOR) = upper(t2.PROD_DES)

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

update dm.dm_td_external_market_pack_mapping
  set KEY_COMPETITOR = 'OTHERS',KEY_COMPETITOR_CN ='OTHERS'
where upper(KEY_COMPETITOR) = 'OTHERS' or upper(KEY_COMPETITOR_CN) = 'OTHERS'

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

create or replace temporary view dim_KEY_COMPETITOR
as
select upper(KEY_COMPETITOR) as KEY_COMPETITOR,max(KEY_COMPETITOR_CN) as KEY_COMPETITOR_CN 
from dm.dm_td_external_market_pack_mapping
group by KEY_COMPETITOR

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

MERGE INTO dm.dm_td_external_market_pack_mapping AS t1
USING dim_KEY_COMPETITOR AS t2
   ON t1.KEY_COMPETITOR = t2.KEY_COMPETITOR
WHEN MATCHED THEN
  UPDATE SET t1.KEY_COMPETITOR_CN = t2.KEY_COMPETITOR_CN;
