-- Databricks notebook source
INSERT OVERWRITE dm.dm_td_external_market (
  MARKET, DATA_SOURCE, TA, AZ_RELATED, MARKET_DESC, TA_RN, PRD_FLAG
)
  SELECT
    MARKET,
    DATA_SOURCE,
    MAX(TA) TA,
    MAX(AZ_RELATED) AZ_RELATED,
    MAX(MARKET_DESC) MARKET_DESC,
    MAX(TA_RN) TA_RN,
    case
      when
        DATA_SOURCE = 'Retail(Quarterly)'
        and MARKET in (
          'Antacid anti GI swelling agent+PPI Oral',
          'HTN Market',
          'NIAD (Non-Insulin Anti-Diabetic) Market',
          'Respules Market',
          'Statin+XZK Market',
          'BUDESONIDE Market',
          'Rosuvastatin Market',
          'Atorvastatin Market',
          'Metoprolol Succinate Market',
          'PPI Oral ESOMEPRAZOLE Market',
          'PPI Oral OMEPRAZOLE Market',
          --'Inhaled Extended Market by Brand',
          'Inhaled Extended Market',
          'Non-Omeprazole',
          'Metoprolol Tartrate Market',
          'Pediatric Cough Market',
          'Anti-HER2 Market',
          'Anti-HER2 mBC Market',
          'Fasenra Market',
          'Severe Asthma'
        )
      then
        '1'
      when
        data_source = 'DTP(Quarterly)'
        and market in (
          'EGFR TKI Market',
          'Anti-HER2 Market',
          'Anti-HER2 mBC Market',
          'Fasenra/Tezspire Related Market',
          'Severe Asthma',
          'Tezspire CRSwNP Market',
          'CRSwNP Market',
          'PAM Market'
        )
      then
        '1'
      else '0'
    end as PRD_FLAG
  FROM
    DM.dm_td_external_market_pack_mapping_temp
  GROUP BY
    MARKET,
    DATA_SOURCE

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

refresh table dm.DM_TD_EXTERNAL_MARKET

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

insert overwrite dm.dm_td_external_market_ta(MARKET,DATA_SOURCE,TA,TA_RN,MARKET_DESC)
SELECT MARKET,DATA_SOURCE, AZ_RELATED TA, TA_RN,MARKET_DESC
FROM DM.DM_TD_EXTERNAL_MARKET
WHERE MARKET NOT LIKE '%ALL Market%'  and MARKET <>'Retail All Market'
UNION all
SELECT MARKET,DATA_SOURCE, 'CVRM' TA,0 TA_RN,MARKET_DESC
FROM DM.DM_TD_EXTERNAL_MARKET
WHERE MARKET = 'CVRM Market'

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

-----------------------------------------------------------------------------------
--修改时间：20240822
--修改人：FanXujia
--修改内容：
--Retail、EC渠道，新增NIAD这个TA。数据与DM相同
-----------------------------------------------------------------------------------
insert into dm.dm_td_external_market_ta
(
MARKET,
DATA_SOURCE,
TA,
TA_RN,
MARKET_DESC
)
select
market,
DATA_SOURCE,
'NIAD' as TA,
7 as TA_RN,
MARKET_DESC
from dm.dm_td_external_market_ta
where DATA_SOURCE = 'Retail(Quarterly)'
and ta = 'DM'
union all
select
market,
DATA_SOURCE,
'NIAD' as TA,
7 as TA_RN,
MARKET_DESC
from dm.dm_td_external_market_ta
where DATA_SOURCE = 'EC(Monthly)'
and ta = 'DM'

--20250701 chenwu 新增RARE = ONCO的Soliris Related Market
union all
select
market,
DATA_SOURCE,
'RARE' as TA,
8 as TA_RN,
MARKET_DESC
from dm.dm_td_external_market_ta
where DATA_SOURCE = 'Retail(Quarterly)'
and ta = 'ONCO'
and MARKET = 'Soliris Related Market'

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

-- log 20250226 auth:庄伟
-- 对market_ta表中市场进行补全，并依据手工文件维护的 market 与 ta 映射，匹配出完整市场对应的TA_NAME


insert overwrite table dm.dm_td_external_market_ta
(
MARKET,
DATA_SOURCE,
TA,
TA_MAP,
TA_RN,
MARKET_DESC
)

-----找出在market维度表中不存在的market列表
with temp_ma_ext_market
as 
(
select 
distinct 
MARKET
,DATA_SOURCE
from dm.dm_td_external_market
where MARKET NOT IN (
select distinct MARKET 
from dm.dm_td_external_market_ta
)
)


------补充完整的market列表，并通过market匹配手工文件维护的market对应的TA
select 
distinct 
dim_mkt_ta.MARKET,
DATA_SOURCE,
dim_mkt_ta.TA,
nvl(mkt_property.ta,mkt_property_key.TA) TA_MAP,
dim_mkt_ta.TA_RN,
dim_mkt_ta.MARKET_DESC
from
(
select MARKET,DATA_SOURCE,TA,TA_RN,MARKET_DESC
from dm.dm_td_external_market_ta
)dim_mkt_ta
left join dm.dm_ims_td_market_property mkt_property
on dim_mkt_ta.market = mkt_property.market
left join dm.dm_ims_td_market_property mkt_property_key
on dim_mkt_ta.MARKET = mkt_property_key.MARKET_KEY
union all
select 
        ext_market.MARKET,
        ext_market.DATA_SOURCE,
        null TA,
        mkt_property.TA TA_MAP,
        '99' as TA_RN,
        null MARKET_DESC
from temp_ma_ext_market ext_market 
left join dm.dm_ims_td_market_property mkt_property
on ext_market. market =mkt_property.MARKET
where nvl(mkt_property.TA,'')<>'';