-- Databricks notebook source
-- MAGIC %md
-- MAGIC ### 维度信息计算

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

-------------------------------------------------------------------------------------
--修改时间：20240819
--修改人：FanXujia
--修改内容：
--模板整合，将原本的十几个手工表改成3个表。
--其中，
--新文件《全国-集团排名》筛选：TA=Retail Total，Rx/OTC= Rx，Top/Top Incre. = Top Sales，对应了旧文件：《全国-Rx-TOP集团》
--新文件《全国-集团排名》筛选：TA <> Retail Total，Top/Top Incre. = Top Sales，对应了旧文件：《全国-分TA-TOP集团》
--《全国-集团排名》：dwd.dwd_gnd_retail_national_corp_rank
--《全国-产品排名》：dwd.dwd_gnd_retail_national_prd_rank
--《大区-集团排名》：dwd.dwd_gnd_retail_region_corp_rank
-------------------------------------------------------------------------------------

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

--直接写入旧的文件对应的表，这样后面的代码都不用改动。
--新文件《全国-集团排名》筛选：TA=Retail Total，Rx/OTC= Rx，Top/Top Incre. = Top Sales，对应了旧文件：《全国-Rx-TOP集团》
insert overwrite table dwd.dwd_gnd_ext_retail_nataional_top_corp
(
rank1,
corp_desc,
corporation,
corp_type,
sales_quarter,
sales_amount,
ytd_gr,
ytd_ms,
ytd_delta_ms,
builtinarchivedate,
source_file_path,
source_file_name,
etl_insert_dt
)
select 
trim(rank1) as rank1,
corp_desc,
corporation,
`type` as corp_type,
sales_quarter,
sales_amount,
null as ytd_gr,
null as ytd_ms,
null as ytd_delta_ms,
builtinarchivedate,
source_file_path,
source_file_name,
etl_insert_dt
from dwd.dwd_gnd_retail_national_corp_rank
where upper(ta) = 'RETAIL TOTAL'
and upper(rx_otc) = 'RX'
and upper(top_top_incre) = 'TOP SALES'

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

--直接写入旧的文件对应的表，这样后面的代码都不用改动。
--新文件《全国-集团排名》筛选：TA <> Retail Total，Top/Top Incre. = Top Sales，对应了旧文件：《全国-分TA-TOP集团》
insert overwrite table dwd.dwd_gnd_ext_retail_nataional_ta_top_corp
(
rank1,
corp_desc,
corp_name,
corp_type,
source_name,
sales_quarter,
sales_amount,
ytd_gr,
ytd_ms,
ytd_delta_ms,
builtinarchivedate,
source_file_path,
source_file_name,
etl_insert_dt
)
select 
trim(rank1) as rank1,
corp_desc,
corporation as corp_name,
`type` as corp_type,
case when ta = 'GI' then concat(ta,'-',rx_otc)
     when ta = 'NIAD-excl. GLP1' then 'DM'
     else ta end as source_name,
sales_quarter,
sales_amount,
null as ytd_gr,
null as ytd_ms,
null as ytd_delta_ms,
builtinarchivedate,
source_file_path,
source_file_name,
etl_insert_dt
from dwd.dwd_gnd_retail_national_corp_rank
where upper(ta) <> 'RETAIL TOTAL'
and upper(top_top_incre) = 'TOP SALES'

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

-------------------从底表获取market 与ta 对应关系------------------------------
insert overwrite table dws.dws_ext_retail_td_ta
select
	market,
    ta,
    from_utc_timestamp(current_timestamp(),'UTC+8')	etl_insert_dt,
    from_utc_timestamp(current_timestamp(),'UTC+8')	etl_update_dt
from  dwd.dwd_gnd_ext_retail_dim_ta;   

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

--增加retail的省份和城市 geo表
 with t1 as (
	----获取底表的 province_city
select province_city
from (select zk_region province_city from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all 
union all 
select province_city  from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all)
 group by province_city
---增加dtp的部分
UNION 
select distinct province_city from dwd.dwd_gnd_ext_dtp_zk_brand
)
, tmp as (
----------------按照retail 逻辑划分福厦泉、全国，并从chpa的geo中提取其他维度信息---------------------
select distinct 
 case when t1.province_city = '全国' then 'CHT'
 	when t1.province_city = '福厦泉' then 'FXQ'
 else 
 	nvl(t2.geo_key, t3.geo_key) 
 end geo_key,
 t4.AZ_City_tier
 ,t4.city_tier,
 province_city,
 t3.city_name,
 t3.city_code, 
 t3.city_name_en,
 case when t1.province_city = '福厦泉' then '福建'
 when t1.province_city = '全国' then '全国'
 else 
 	nvl(t3.province_name,t2.province_name) 
 end province_name,
 case when t1.province_city = '福厦泉' then 'FJ'
 when t1.province_city = '全国' then 'National'
 else 
 	nvl(t3.province_code,t2.province_code)
 end province_code, 
 case when t1.province_city = '福厦泉' then 'Fujian'
  when t1.province_city = '全国' then 'National'
 else 
 	nvl(t3.province_name_en,t2.province_name_en) 
 end province_name_en,  
 case when t1.province_city = '福厦泉' then 'SOUTH'
  when t1.province_city = '全国' then 'National'
 else 
 	nvl(t3.area_code,t2.area_code) 
 end area_code,
 case when t1.province_city = '福厦泉' then '南部地区'
  when t1.province_city = '全国' then 'National'
 else 
 	nvl(t3.area_name,t2.area_name) 
 end area_name, 
 case when t1.province_city = '福厦泉' then 'South China'
  when t1.province_city = '全国' then 'National'
 else 
 	nvl(t3.area_name_en,t2.area_name_en) 
 end area_name_en,

 case when t1.province_city = '福厦泉' then 'SOUTH'
  when t1.province_city = '全国' then 'National'
 else 
 	nvl(t3.rc_code,t2.rc_code) 
 end rc_code, 
 case when t1.province_city = '福厦泉' then '南部地区'
  when t1.province_city = '全国' then 'National'
 else 
 	nvl(t3.rc_name,t2.rc_name) 
 end rc_name, 
 case when t1.province_city = '福厦泉' then 'South China'
  when t1.province_city = '全国' then 'National'
 else 
 	nvl(t3.rc_name_en,t2.rc_name_en) 
 end rc_name_en 
from  t1 left 
join dm.dm_td_geography t2
on t1.province_city = t2.province_name and t2.geo_key = t2.province_code
left join dm.dm_td_geography t3 
on t1.province_city = replace(t3.city_name,'市','') and t3.geo_key = t3.city_code
left join dws.dws_ims_td_geo t4 
on  t1.province_city = replace(t4.AUDIT_DES_C,'市','')    
)
-------------------省份、自治区等处理-----------------------
insert overwrite table dm.dm_zk_retail_geo
SELECT 
    geo_key
	 ,city_name_en
	 ,replace(city_name,'市')  																			  city_c
	 ,case when city_code is not null then 'City' else 'Province' END city_type
	 ,city_tier
   ,AZ_City_tier
	 ,province_name_en
	 ,province_name
	 ,province_code
	 ,city_name     																			  city_map
	 ,case when  province_code  in ('TJ','CQ','BJ','SH')  then concat(province_name,'市') 
	 			 when  province_code in  ('SC','YN','LN','SA','GZ','HN','HL','SX','ZJ','JS','SD','JL','FJ','GD','HU','AH','GS','HE','JX','HB') then  concat(province_name,'省') 
	 			 when  province_code  ='GX' THEN concat(province_name,'壮族自治区')
				 when  province_code  ='IM' THEN concat(province_name,'自治区') 
		ELSE province_name
	 end 																						  province_map
	 ,city_code
	 ,province_city
   ,area_code
   ,area_name
   ,area_name_en
   ,rc_code
   ,rc_name
   ,rc_name_en
	,from_utc_timestamp(current_timestamp(),'UTC+8')	etl_insert_dt
	,from_utc_timestamp(current_timestamp(),'UTC+8')	etl_update_dt
 from tmp 

UNION ALL 

SELECT 
'ROC'
,'Other Low Tiers'
,'Other Low Tiers'
,'City'
,	1
,	1	
,'Other Low Tiers'
,'Other Low Tiers'
,	null	
,'Other Low Tiers'
,'Other Low Tiers'
,'Other Low Tiers'
,'Other Low Tiers'
,'Other Low Tiers'
,'Other Low Tiers'
,'Other Low Tiers'
,'Other Low Tiers'
,'Other Low Tiers'
,'Other Low Tiers'
	,from_utc_timestamp(current_timestamp(),'UTC+8')	etl_insert_dt
	,from_utc_timestamp(current_timestamp(),'UTC+8')	etl_update_dt

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

/**
 B2C标签表整合关联tblmarket 表，获取market 信息

*/
insert overwrite table tmp.tmp_zk_retail_pack_property 
with  tmp1 as (
	
	select distinct
			prod.iqvia_lineno 
			,if( prod.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',prod.iqvia_pack_code),12),prod.iqvia_pack_code) as iqvia_pack_code --right(concat('0000000',prod.iqvia_pack_code),7) 	  
			, right(concat('000000000',prod.iqvia_prod_code),9) 	as PROD_COD
			,prod.iqvia_notes 
			,prod.new_pack_flag 
			,prod.is_exists_chpa 
			,prod.product_id 
			,prod.prescription_nature 
			,prod.medicine_type 
			,prod.zk_medicine_tier1 
				,prod.zk_medicine_tier2 
			,prod.zk_medicine_tier3 
			,prod.zk_medicine_tier4 
			,prod.common_name 
			,prod.dosage_form 
			,prod.user_type 
			,prod.category_name 
			,prod.product_name 
			,prod.brand_name 
			,prod.zk_manu_des 
			,prod.zk_corp_des 
			,prod.zk_pack_des 
			,prod.counting_unit_a 
			,prod.dosage_unit_a 
			,prod.unit_a 
			,prod.app1_cod 
			,prod.app1_des 
			,prod.app1_des_c 
			,prod.app2_cod 
			,prod.app2_des 
			,prod.app2_des_c 
			,prod.app3_cod 
			,prod.app3_des 
			,prod.app3_des_c 
			,prod.atc1_cod 
			,prod.atc1_des 
			,prod.atc1_des_c 
			,prod.atc2_cod 
			,prod.atc2_des 
			,prod.atc2_des_c 
			,prod.atc3_cod 
			,prod.atc3_des 
			,prod.atc3_des_c 
			,prod.atc4_cod 
			,prod.atc4_des 
			,prod.atc4_des_c 
			,prod.bio_desc 
			,right(concat('000000',nvl(prod.cmps_cod,0 )),6)   cmps_cod
			,prod.cmps_des 
			,prod.cmps_des_c 
			,prod.corp_cod 
			,prod.corp_des 
			,prod.corp_des_c 
			,prod.edl_desc 
			,prod.eth_otc_desc 
			,prod.gene_orig_desc 
			,prod.gqce_desc 
			,prod.manu_cod 
			,prod.manu_des 
			,prod.manu_des_c 
			,prod.mnfl_cod 
			,prod.mnfl_des 
			,prod.nrdl_desc 
			,prod.pack_des 
			,prod.stgh_des 
			,prod.pack_lch 
			,prod.paed_desc 
			,prod.prod_des 
			,prod.prod_des_c 
			,prod.tcm_desc 
			,prod.vbp_desc 
			,prod.unit 
			,prod.counting_unit 
			,prod.dosage_unit  
			,nvl(MOLE.NRDL_Entry_Date,'') 											  AS NRDL_ENTRY_DATE
			,tblmkt.market 
			,tblmkt.bu
			,nvl(tblmkt.extend_market_ratio,1)										as extend_market_ratio
from dwd.dwd_gnd_ext_retail_pack_property  prod 
LEFT JOIN dwd.dwd_ims_td_pack_additional_attribute MOLE ON prod.iqvia_pack_code = MOLE.Pack_Code 
left join (
				select market
							,bu
							,atc1_code
							,atc2_code
							,atc3_code
							,atc4_code
							,nfc1_code
							,nfc2_code
							,nfc3_code
							,pack_code
							,Molecule_Code
							,zk_common_name
							,extend_market_ratio 
							,zk_product_id
							,zk_dosage_form
							,Product_Code
							,not_in_flag
							,extend_market
				from dwd.dwd_gnd_ext_retail_tblmarket
				-- where (market not in( 'ZOK Default Market', 'Inhaled Extended Market') or (case when market in( 'Inhaled Extended Market') then  zk_product_id is not null end))
				where 1=1
					and  Extend_Market IS NULL 
					AND ( NOT_IN_FLAG IS NULL or  NOT_IN_FLAG = '1' )
)tblmkt
on  nvl(prod.ATC1_COD,'')	 = case when tblmkt.ATC1_Code    is null then nvl(prod.ATC1_COD,'')  else tblmkt.ATC1_Code end
and nvl(prod.ATC2_COD,'')	 = case when tblmkt.ATC2_Code    is null then nvl(prod.ATC2_COD,'')  else tblmkt.ATC2_Code end
and nvl(prod.ATC3_COD,'')	 = case when tblmkt.ATC3_Code    is null then nvl(prod.ATC3_COD,'')  else tblmkt.ATC3_Code end
and nvl(prod.ATC4_COD,'')	 = case when tblmkt.ATC4_Code    is null then nvl(prod.ATC4_COD,'')  else tblmkt.ATC4_Code end
and nvl(prod.APP1_COD,'')	 = case when tblmkt.NFC1_Code    is null then nvl(prod.APP1_COD,'')  else tblmkt.NFC1_Code end
and nvl(prod.APP2_COD,'')	 = case when tblmkt.NFC2_Code    is null then nvl(prod.APP2_COD,'')  else tblmkt.NFC2_Code end
and nvl(prod.APP3_COD,'')	 = case when tblmkt.NFC3_Code    is null then nvl(prod.APP3_COD,'')  else tblmkt.NFC3_Code end
and  right(concat('000000000',nvl(prod.iqvia_prod_code,0 )),9)    = case when tblmkt.Product_Code is null then  right(concat('000000000',nvl(prod.iqvia_prod_code,0 )),9)  else   right(concat('000000000',nvl(tblmkt.Product_Code,0 )),9)  end
and if(nvl(prod.iqvia_pack_code,0 ) REGEXP '^[0-9]' ,right(concat('000000000000',nvl(prod.iqvia_pack_code,0 )),12),nvl(prod.iqvia_pack_code,0 ))
	= case when tblmkt.Pack_Code is null then if(nvl(prod.iqvia_pack_code,0 ) REGEXP '^[0-9]' ,right(concat('000000000000',nvl(prod.iqvia_pack_code,0 )),12),nvl(prod.iqvia_pack_code,0 ))
	else if(nvl(tblmkt.Pack_Code,0 ) REGEXP '^[0-9]' ,right(concat('000000000000',nvl(tblmkt.Pack_Code,0 )),12),nvl(tblmkt.Pack_Code,0 ))  end
-- and  right(concat('00000',nvl(prod.iqvia_prod_code,0 )),5)    = case when tblmkt.Product_Code is null then  right(concat('00000',nvl(prod.iqvia_prod_code,0 )),5)  else   right(concat('00000',nvl(tblmkt.Product_Code,0 )),5)  end
-- and right(concat('0000000',nvl(prod.iqvia_pack_code,0 )),7)  = case when tblmkt.Pack_Code    is null then right(concat('0000000',nvl(prod.iqvia_pack_code,0 )),7)   else right(concat('0000000',tblmkt.Pack_Code ),7 )  end
and right(concat('000000',nvl(prod.cmps_cod,0 )),6) 	= case when tblmkt.Molecule_Code 	is null then right(concat('000000',nvl(prod.cmps_cod,0 )),6)	else right(concat('000000',nvl(tblmkt.Molecule_Code,0 )),6)  end 
where tblmkt.market is not null

)
, tmp2 as (
	
	select distinct
			prod.iqvia_lineno 
			,if( prod.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',prod.iqvia_pack_code),12),prod.iqvia_pack_code) as iqvia_pack_code --right(concat('0000000',prod.iqvia_pack_code),7) 	  
			, right(concat('000000000',prod.iqvia_prod_code),9) 	as PROD_COD
			,prod.iqvia_notes 
			,prod.new_pack_flag 
			,prod.is_exists_chpa 
			,prod.product_id 
			,prod.prescription_nature 
			,prod.medicine_type 
			,prod.zk_medicine_tier1 
			,prod.zk_medicine_tier2 
			,prod.zk_medicine_tier3 
			,prod.zk_medicine_tier4 
			,prod.common_name 
			,prod.dosage_form 
			,prod.user_type 
			,prod.category_name 
			,prod.product_name 
			,prod.brand_name 
			,prod.zk_manu_des 
			,prod.zk_corp_des 
			,prod.zk_pack_des 
			,prod.counting_unit_a 
			,prod.dosage_unit_a 
			,prod.unit_a 
			,prod.app1_cod 
			,prod.app1_des 
			,prod.app1_des_c 
			,prod.app2_cod 
			,prod.app2_des 
			,prod.app2_des_c 
			,prod.app3_cod 
			,prod.app3_des 
			,prod.app3_des_c 
			,prod.atc1_cod 
			,prod.atc1_des 
			,prod.atc1_des_c 
			,prod.atc2_cod 
			,prod.atc2_des 
			,prod.atc2_des_c 
			,prod.atc3_cod 
			,prod.atc3_des 
			,prod.atc3_des_c 
			,prod.atc4_cod 
			,prod.atc4_des 
			,prod.atc4_des_c 
			,prod.bio_desc 
			,right(concat('000000',nvl(prod.cmps_cod,0 )),6)  cmps_cod
			,prod.cmps_des 
			,prod.cmps_des_c 
			,prod.corp_cod 
			,prod.corp_des 
			,prod.corp_des_c 
			,prod.edl_desc 
			,prod.eth_otc_desc 
			,prod.gene_orig_desc 
			,prod.gqce_desc 
			,prod.manu_cod 
			,prod.manu_des 
			,prod.manu_des_c 
			,prod.mnfl_cod 
			,prod.mnfl_des 
			,prod.nrdl_desc 
			,prod.pack_des 
			,prod.stgh_des 
			,prod.pack_lch 
			,prod.paed_desc 
			,prod.prod_des 
			,prod.prod_des_c 
			,prod.tcm_desc 
			,prod.vbp_desc 
			,prod.unit 
			,prod.counting_unit 
			,prod.dosage_unit  
			,nvl(MOLE.NRDL_Entry_Date,'') 											  AS NRDL_ENTRY_DATE
			,tblmkt.market 
			,tblmkt.bu
			,nvl(tblmkt.extend_market_ratio,1)										as extend_market_ratio
			,1  																	as f_flag
from dwd.dwd_gnd_ext_retail_pack_property  prod 
LEFT JOIN dwd.dwd_ims_td_pack_additional_attribute MOLE ON prod.iqvia_pack_code = MOLE.Pack_Code 
left join (
				select market
							,bu
							,atc1_code
							,atc2_code
							,atc3_code
							,atc4_code
							,nfc1_code
							,nfc2_code
							,nfc3_code
							,pack_code
							,Molecule_Code
							,zk_common_name
							,extend_market_ratio 
							,zk_product_id
							,zk_dosage_form
							,Product_Code
							,not_in_flag
							,extend_market
				from dwd.dwd_gnd_ext_retail_tblmarket
				-- where (market not in( 'ZOK Default Market', 'Inhaled Extended Market') or (case when market in( 'Inhaled Extended Market') then  zk_product_id is not null end))
				where 1=1
							and Extend_Market IS NULL  and NOT_IN_FLAG ='0' 
 
)tblmkt
on  nvl(prod.ATC1_COD,'')	 = case when tblmkt.ATC1_Code    is null then nvl(prod.ATC1_COD,'')  else tblmkt.ATC1_Code end
and nvl(prod.ATC2_COD,'')	 = case when tblmkt.ATC2_Code    is null then nvl(prod.ATC2_COD,'')  else tblmkt.ATC2_Code end 
and nvl(prod.ATC3_COD,'')	 = case when tblmkt.ATC3_Code    is null then nvl(prod.ATC3_COD,'')  else tblmkt.ATC3_Code end
and nvl(prod.ATC4_COD,'')	 = case when tblmkt.ATC4_Code    is null then nvl(prod.ATC4_COD,'')  else tblmkt.ATC4_Code end
and nvl(prod.APP1_COD,'')	 = case when tblmkt.NFC1_Code    is null then nvl(prod.APP1_COD,'')  else tblmkt.NFC1_Code end
and nvl(prod.APP2_COD,'')	 = case when tblmkt.NFC2_Code    is null then nvl(prod.APP2_COD,'')  else tblmkt.NFC2_Code end
and nvl(prod.APP3_COD,'')	 = case when tblmkt.NFC3_Code    is null then nvl(prod.APP3_COD,'')  else tblmkt.NFC3_Code end
and  right(concat('000000000',nvl(prod.iqvia_prod_code,0 )),9)    = case when tblmkt.Product_Code is null then  right(concat('000000000',nvl(prod.iqvia_prod_code,0 )),9)  else   right(concat('000000000',nvl(tblmkt.Product_Code,0 )),9)  end
and if(nvl(prod.iqvia_pack_code,0 ) REGEXP '^[0-9]' ,right(concat('000000000000',nvl(prod.iqvia_pack_code,0 )),12),nvl(prod.iqvia_pack_code,0 ))
	= case when tblmkt.Pack_Code is null then if(nvl(prod.iqvia_pack_code,0 ) REGEXP '^[0-9]' ,right(concat('000000000000',nvl(prod.iqvia_pack_code,0 )),12),nvl(prod.iqvia_pack_code,0 ))
	else if(nvl(tblmkt.Pack_Code,0 ) REGEXP '^[0-9]' ,right(concat('000000000000',nvl(tblmkt.Pack_Code,0 )),12),nvl(tblmkt.Pack_Code,0 ))  end
and right(concat('000000',nvl(prod.cmps_cod,0 )),6) 	= case when tblmkt.Molecule_Code 	is null then right(concat('000000',nvl(prod.cmps_cod,0 )),6)	else right(concat('000000',nvl(tblmkt.Molecule_Code,0 )),6)  end 
where tblmkt.market is not null


)
, tmp3 as (
	select distinct
			 tmp1.iqvia_lineno 
			,tmp1.iqvia_pack_code 
			,tmp1.PROD_COD
			,tmp1.iqvia_notes 
			,tmp1.new_pack_flag 
			,tmp1.is_exists_chpa 
			,tmp1.product_id 
			,tmp1.prescription_nature 
			,tmp1.medicine_type 
			,tmp1.zk_medicine_tier1 
			,tmp1.zk_medicine_tier2 
			,tmp1.zk_medicine_tier3 
			,tmp1.zk_medicine_tier4 
			,tmp1.common_name 
			,tmp1.dosage_form 
			,tmp1.user_type 
			,tmp1.category_name 
			,tmp1.product_name 
			,tmp1.brand_name 
			,tmp1.zk_manu_des 
			,tmp1.zk_corp_des 
			,tmp1.zk_pack_des 
			,tmp1.counting_unit_a 
			,tmp1.dosage_unit_a 
			,tmp1.unit_a 
			,tmp1.app1_cod 
			,tmp1.app1_des 
			,tmp1.app1_des_c 
			,tmp1.app2_cod 
			,tmp1.app2_des 
			,tmp1.app2_des_c 
			,tmp1.app3_cod 
			,tmp1.app3_des 
			,tmp1.app3_des_c 
			,tmp1.atc1_cod 
			,tmp1.atc1_des 
			,tmp1.atc1_des_c 
			,tmp1.atc2_cod 
			,tmp1.atc2_des 
			,tmp1.atc2_des_c 
			,tmp1.atc3_cod 
			,tmp1.atc3_des 
			,tmp1.atc3_des_c 
			,tmp1.atc4_cod 
			,tmp1.atc4_des 
			,tmp1.atc4_des_c 
			,tmp1.bio_desc 
			,tmp1.cmps_cod 
			,tmp1.cmps_des   
			,tmp1.cmps_des_c 
			,tmp1.corp_cod 
			,tmp1.corp_des 
			,tmp1.corp_des_c 
			,tmp1.edl_desc 
			,tmp1.eth_otc_desc 
			,tmp1.gene_orig_desc 
			,tmp1.gqce_desc 
			,tmp1.manu_cod 
			,tmp1.manu_des 
			,tmp1.manu_des_c 
			,tmp1.mnfl_cod 
			,tmp1.mnfl_des 
			,tmp1.nrdl_desc 
			,tmp1.pack_des 
			,tmp1.stgh_des 
			,tmp1.pack_lch 
			,tmp1.paed_desc 
			,tmp1.prod_des 
			,tmp1.prod_des_c 
			,tmp1.tcm_desc 
			,tmp1.vbp_desc 
			,tmp1.unit 
			,tmp1.counting_unit 
			,tmp1.dosage_unit  
			,tmp1.NRDL_ENTRY_DATE
			,tmp1.market 
			,nvl(t2.bu,tmp1.bu)									as bu
			,tmp1.extend_market_ratio	
			,IF(tmp1.corp_des_c in('阿斯利康制药集团') ,'Y','N')      is_az
from tmp1 left join tmp2
	 on  nvl(tmp1.ATC1_COD,'')			= nvl(tmp2.ATC1_COD,'')			
     and nvl(tmp1.ATC2_COD,'')			= nvl(tmp2.ATC2_COD,'')			
     and nvl(tmp1.ATC3_COD,'')			= nvl(tmp2.ATC3_COD,'')			
     and nvl(tmp1.ATC4_COD,'')			= nvl(tmp2.ATC4_COD,'')			
     and nvl(tmp1.APP1_COD,'')			= nvl(tmp2.APP1_COD,'')			
     and nvl(tmp1.APP2_COD,'')			= nvl(tmp2.APP2_COD,'')			
     and nvl(tmp1.APP3_COD,'')			= nvl(tmp2.APP3_COD,'')			
     and nvl(tmp1.PROD_COD,'')			= nvl(tmp2.PROD_COD,'')			
     and nvl(tmp1.iqvia_pack_code,'')	= nvl(tmp2.iqvia_pack_code,'')	
     and nvl(tmp1.common_name ,'')		= nvl(tmp2.common_name ,'')		
     and nvl(tmp1.product_id,'')  		= nvl(tmp2.product_id,'')  		
     and nvl(tmp1.dosage_form ,'')		= nvl(tmp2.dosage_form ,'')		
     and nvl(tmp1.CMPS_COD,'')  		= nvl(tmp2.CMPS_COD,'')  		
     and nvl(tmp1.market ,'')			= nvl(tmp2.market ,'')		
left join  (select Market, BU,Extend_Market,Extend_Market_Ratio from dwd.dwd_gnd_ext_retail_tblmarket
					--  where (market not in( 'ZOK Default Market', 'Inhaled Extended Market') and  Extend_Market IS NOT NULL
					--  )or (case when market in( 'Inhaled Extended Market') then  zk_product_id is not null end)
					where Extend_Market IS NOT NULL
					 ) t2 on tmp1.Market=t2.Extend_Market
where  NVL(tmp2.f_flag,0)  !=1   

)


-- 合并所有市场数据  
select 
	* 
from tmp3
--没有匹配上的给默认市场：Non AZ Retail Related Market
union all
select 
			prod.iqvia_lineno 
			,if( prod.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',prod.iqvia_pack_code),12),prod.iqvia_pack_code) as iqvia_pack_code
			,right(concat('000000000',prod.iqvia_prod_code),9) as iqvia_prod_code
			-- ,right(concat('0000000', prod.iqvia_pack_code ),7 ) iqvia_pack_code
			-- ,right(concat('00000', prod.iqvia_prod_code ),5) iqvia_prod_code
			,prod.iqvia_notes 
			,prod.new_pack_flag 
			,prod.is_exists_chpa 
			,prod.product_id 
			,prod.prescription_nature 
			,prod.medicine_type 
			,prod.zk_medicine_tier1 
			,prod.zk_medicine_tier2 
			,prod.zk_medicine_tier3 
			,prod.zk_medicine_tier4 
			,prod.common_name 
			,prod.dosage_form 
			,prod.user_type 
			,prod.category_name 
			,prod.product_name 
			,prod.brand_name 
			,prod.zk_manu_des 
			,prod.zk_corp_des 
			,prod.zk_pack_des 
			,prod.counting_unit_a 
			,prod.dosage_unit_a 
			,prod.unit_a 
			,prod.app1_cod 
			,prod.app1_des 
			,prod.app1_des_c 
			,prod.app2_cod 
			,prod.app2_des 
			,prod.app2_des_c 
			,prod.app3_cod 
			,prod.app3_des 
			,prod.app3_des_c 
			,prod.atc1_cod 
			,prod.atc1_des 
			,prod.atc1_des_c 
			,prod.atc2_cod 
			,prod.atc2_des 
			,prod.atc2_des_c 
			,prod.atc3_cod 
			,prod.atc3_des 
			,prod.atc3_des_c 
			,prod.atc4_cod 
			,prod.atc4_des 
			,prod.atc4_des_c 
			,prod.bio_desc 
			,right(concat('00000',nvl(prod.cmps_cod,0 )),6) cmps_cod
			,prod.cmps_des 
			,prod.cmps_des_c 
			,prod.corp_cod 
			,prod.corp_des 
			,prod.corp_des_c 
			,prod.edl_desc 
			,prod.eth_otc_desc 
			,prod.gene_orig_desc 
			,prod.gqce_desc 
			,prod.manu_cod 
			,prod.manu_des 
			,prod.manu_des_c 
			,prod.mnfl_cod 
			,prod.mnfl_des 
			,prod.nrdl_desc 
			,prod.pack_des 
			,prod.stgh_des 
			,prod.pack_lch 
			,prod.paed_desc 
			,prod.prod_des 
			,prod.prod_des_c 
			,prod.tcm_desc 
			,prod.vbp_desc 
			,prod.unit 
			,prod.counting_unit 
			,prod.dosage_unit  
			,nvl(MOLE.NRDL_Entry_Date,'') 											      AS NRDL_ENTRY_DATE
			,'Non AZ Retail Related Market' 	 								  as market 
			,tmp3.bu
			,nvl(tmp3.extend_market_ratio,1)													as extend_market_ratio
			,IF(PROD.corp_des_c in('阿斯利康制药集团') ,'Y','N')      is_az

from  dwd.dwd_gnd_ext_retail_pack_property  prod
left join tmp3   on prod.product_id = tmp3.product_id
LEFT JOIN dwd.dwd_ims_td_pack_additional_attribute MOLE ON prod.iqvia_pack_code = MOLE.Pack_Code 
where tmp3.product_id is null


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

-----处理 Nifedipine 上传时有特殊字符，无法匹配----
update  tmp.tmp_zk_retail_pack_property 
set market='Nifedipine Market'
where market like 'Nifedipine%'

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

-- MAGIC %md
-- MAGIC ### brand拆分

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

--------------------------------------------获取省份文件中的品牌的数据,用于后续比例拆分--------------------------
insert overwrite table tmp.tmp_retail_brand 
SELECT
    YYYYMM,
    TA,
    case
        when zk_brand_category in ('氨氯地平阿托伐他汀钙片', '多达一', '天依宁', 'OTHERS') then zk_brand_category --
        else b.PROD_DES_C
    end as PROD_DES_C,
    province_city,
    brand_cat_type,
    A.zk_common_name,
    A.market,
    SUM(sales_val) sales_val,
    cast(SUM(sales_vol) as decimal(30,10)) sales_vol
FROM
    dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all A
    left JOIN (
        select
            distinct PROD_DES_C,
            ZK_Prod_C
        from
            dwd.dwd_inc_gnd_retail_b2c_label_total
    ) B ON nvl(A.zk_brand_category, '') = nvl(B.ZK_Prod_C, '')

where
    ranked_by = 'volume' --and brand_cat_type = '品牌' --and TA = 'CV'
GROUP BY
    YYYYMM,
    TA,
    case
        when zk_brand_category in ('氨氯地平阿托伐他汀钙片', '多达一', '天依宁', 'OTHERS') then zk_brand_category
        else b.PROD_DES_C
    end,
    province_city,
    A.market,
        A.zk_common_name,
    brand_cat_type

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

------------------------------------------------------------------------------------------------------------
--修改时间：20240903
--修改人：FanXujia
--修改内容：新增一个高血压的子市场《酒石酸美托洛尔》，实为高血压市场里的一个分子式，到pack、到省份粒度。品牌粒度文件中没有他。
-------------------------------------------------------------------------------------------------------------


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

---------------------------把品牌、全国数据拆分，并得到比例----------------------------710 修改 ------------------
insert overwrite table tmp.tmp_zk_retail_brandTotal
WITH MKT AS (
  ----------------MKT 总值------------
select a.YYYYMM,ta,a.province_city,a.brand_cat_type,a.market,
  a.sales_val
  ,a.sales_vol
  ,B.brand_mkt  marketmapping
from tmp.tmp_retail_brand a 
left join (SELECT DISTINCT brand_mkt,market_mapping  FROM dwd.dwd_gnd_ext_retail_corresponding_relationship WHERE category_name IS NOT NULL  AND TA IN ('CV','GI')) B 
on a.market=b.brand_mkt and B.brand_mkt = B.market_mapping
where brand_cat_type = '品类' 
and  A.TA IN ('CV','GI')  AND B.brand_mkt IS not null
), brand_df AS (
  -------归属于分子式子市场的品牌------
select a.YYYYMM,a.TA,a.PROD_DES_C,a.province_city
  ,case when a.market = 'pack-CV-高血压-化学药-全国.xlsx' then '氨氯地平,阿托伐他汀'
        when a.market  = 'pack-CV-他汀类+血脂康-全国.xlsx' then '氨氯地平,阿托伐他汀' else A.market  end  submkt
  ,cmps_des_c
  ,case when a.market = 'pack-CV-高血压-化学药-全国.xlsx' then '高血压用药' 
       when a.market  = 'pack-CV-他汀类+血脂康-全国.xlsx' then '他汀类+血脂康' else B.brand_mkt end  marketmapping
        -- when a.market  = 'pack-CV-他汀类+血脂康-全国.xlsx' then '他汀类+血脂康' else coalesce(B.brand_mkt,A.market )  end  marketmapping
  ,a.sales_val,a.sales_vol
from tmp.tmp_retail_brand a  
left join (select distinct brand_mkt,category_name from dwd.dwd_gnd_ext_retail_corresponding_relationship where category_name is not null  AND TA IN ('CV','GI') AND category_name<>brand_mkt) B 
ON a.market=b.category_name 
left join (select distinct  prod_des_c,cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) C ON A.PROD_DES_C=C.prod_des_c
where brand_cat_type = '品牌'  
and a.market not in (select distinct brand_mkt from dwd.dwd_gnd_ext_retail_corresponding_relationship where brand_mkt is not null )
and TA IN ('CV','GI')
),cmps_total as (
  ---------------------分子式汇总值------------
  select a.YYYYMM,ta,a.province_city,a.brand_cat_type  --,c.zk_common_name,c.cmps_des_c
  ,a.market,a.sales_val,a.sales_vol,B.brand_mkt marketmapping
from tmp.tmp_retail_brand a 
left join (SELECT DISTINCT brand_mkt,category_name  FROM dwd.dwd_gnd_ext_retail_corresponding_relationship WHERE category_name IS NOT NULL  AND TA IN ('CV','GI')) B 
on a.market=b.category_name and B.brand_mkt<> B.category_name
-- LEFT JOIN (select DISTINCT zk_common_name,cmps_des_c,marketmapping from brand_df) c on c.zk_common_name= a.market and B.brand_mkt = c.marketmapping 
where brand_cat_type = '品类'  
and A.TA IN ('CV','GI') and B.brand_mkt is not null
), mkt_brand as (
  ----直接归属mkt 下的brand ，不能按同一个比例处理，需单独减去------------------
  select a.YYYYMM,a.TA,a.province_city,
case when a.market = 'pack-CV-高血压-化学药-全国.xlsx' then '氨氯地平,阿托伐他汀'
        when a.market  = 'pack-CV-他汀类+血脂康-全国.xlsx' then '氨氯地平,阿托伐他汀' else A.market  end market
,concat('Others_cmps_',cmps_des_c )  cmps_des_c
,concat('Others_cmps_',a.market )  marketmapping
,a.PROD_DES_C
  ,case when a.market = 'pack-CV-高血压-化学药-全国.xlsx' then '氨氯地平,阿托伐他汀'
        when a.market  = 'pack-CV-他汀类+血脂康-全国.xlsx' then '氨氯地平,阿托伐他汀' else A.market  end  submkt
  ,a.sales_val,a.sales_vol
from tmp.tmp_retail_brand a  
left join (select distinct  prod_des_c,cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) C ON A.PROD_DES_C=C.prod_des_c
where brand_cat_type = '品牌'  
and case when a.market = 'pack-CV-高血压-化学药-全国.xlsx' then '氨氯地平,阿托伐他汀'
        when a.market  = 'pack-CV-他汀类+血脂康-全国.xlsx' then '氨氯地平,阿托伐他汀' else A.market  end   in (select distinct brand_mkt from dwd.dwd_gnd_ext_retail_corresponding_relationship where brand_mkt is not null  and ta   IN ('CV','GI'))
and TA IN ('CV','GI')
and a.PROD_DES_C not in (select  distinct PROD_DES_C from brand_df)
)
--------酒石酸美托洛尔
,metoprolol_tartrat as
(
select YYYYMM,
'CV' as TA,
zk_region as province_city,
'高血压用药' as marketmapping,
sum(sales_value) as sales_val,
sum(sales_unit) as sales_vol
from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all
where brand_flag = 'pack-CV-酒石酸美托洛尔.xlsx'
group by YYYYMM,zk_region
)
, brand_data AS (
  --------------------计算others_cmps-----------------
  --从others_cmps这里扣除酒石酸美托洛尔
select 
  MKT.YYYYMM
  ,MKT.ta,MKT.province_city
  ,MKT.market
  ,concat('Others_cmps_',mkt.market ) cmps_des_c
  ,concat('Others_cmps_',mkt.market ) prodmapping
  ,MKT.sales_val - b.sales_val - coalesce(val,0 ) - coalesce(metoprolol_tartrat.sales_val,0) sales_val
  ,MKT.sales_vol - b.sales_vol -coalesce(vol, 0 ) - coalesce(metoprolol_tartrat.sales_vol,0) sales_vol
FROM MKT inner JOIN
     -- (select YYYYMM,province_city,marketmapping,sum(sales_val) sales_val,sum(sales_vol) sales_vol from cmps_total  group by YYYYMM,province_city,marketmapping) b
            (
                  select 
                YYYYMM,province_city,marketmapping,sum(sales_val) sales_val,sum(sales_vol) sales_vol 
                from (
                      select YYYYMM,province_city,marketmapping,sum(sales_val) sales_val,sum(sales_vol) sales_vol from cmps_total  group by YYYYMM,province_city,marketmapping
                    union all 
                    select YYYYMM,province_city
                        ,case when market = 'pack-CV-高血压-化学药-全国.xlsx' then '高血压用药' 
                          when market  = 'pack-CV-他汀类+血脂康-全国.xlsx' then '他汀类+血脂康'  end  marketmapping 
                        ,sum(sales_val),sum(sales_vol)  
                    from tmp.tmp_retail_brand where PROD_DES_C in ('多达一', '氨氯地平阿托伐他汀钙片', '天依宁', 'OTHERS')  
                    group by YYYYMM,province_city
                          ,case when market = 'pack-CV-高血压-化学药-全国.xlsx' then '高血压用药' 
                      when  market  = 'pack-CV-他汀类+血脂康-全国.xlsx' then '他汀类+血脂康'  end 
                ) group by YYYYMM,province_city,marketmapping
          ) b
on MKT.YYYYMM=b.YYYYMM and MKT.province_city=b.province_city and MKT.market=b.marketmapping
left join (select YYYYMM,TA,province_city,market,marketmapping,sum(sales_val) val ,sum(sales_vol) vol 
           from mkt_brand
           group by YYYYMM,TA,province_city,market,marketmapping 
           ) mkt_brand on MKT.YYYYMM =mkt_brand.YYYYMM and MKT.market =mkt_brand.market and MKT.province_city=mkt_brand.province_city 
left join metoprolol_tartrat
on MKT.YYYYMM = metoprolol_tartrat.YYYYMM AND MKT.market = metoprolol_tartrat.marketmapping AND MKT.province_city = metoprolol_tartrat.province_city
union all 
--------------计算others_cmps_brand-----------------
--cmps_total里没有酒石酸美托洛尔，因此这里不用减酒石酸美托洛尔
SELECT  a.YYYYMM,a.ta,a.province_city
  ,a.marketmapping  market 
  ,cmps_des_c
  ,CASE WHEN A.MARKET ='其他拉唑类' THEN 'Others_其他拉唑类' ELSE  coalesce(concat('Others_',b.cmps_des_c),concat('Others_',A.marketmapping) ) END  prodmapping
  ,a.sales_val - coalesce(b.sales_val,0 )    sales_val
  ,a.sales_vol - coalesce(b.sales_vol,0 )    sales_vol
from cmps_total a 
LEFT JOIN (select YYYYMM,province_city,submkt,cmps_des_c,marketmapping,sum(sales_val) sales_val,sum(sales_vol) sales_vol from brand_df  GROUP BY YYYYMM,province_city,submkt,marketmapping,cmps_des_c ) b 
on a.YYYYMM=b.YYYYMM and a.province_city=b.province_city   and a.marketmapping=b.marketmapping and a.market=b.submkt 
union all 
--------------归属于分子式子市场的品牌-----------
SELECT 
  YYYYMM,TA,province_city
  ,marketmapping market
  ,cmps_des_c
  ,PROD_DES_C
  ,sales_val
  ,sales_vol
FROM brand_df
union all 
-------------直接归属于市场的品牌
select 
 YYYYMM,TA,province_city
  ,market
  ,cmps_des_c
  ,prod_des_c
  ,sales_val
  ,sales_vol
from mkt_brand
------这里不用union上酒石酸美托洛尔，因为他有pack粒度的数，不需要计算比例
)
 -----计算拆分比例------
SELECT 
  YYYYMM
  ,ta
  ,province_city
  ,market
  ,case when ta='CV' AND prodmapping ='OTHERS' THEN '氨氯地平,阿托伐他汀' 
        WHEN TA='GI' AND cmps_des_c IS NULL THEN '其他拉唑类'
    ELSE cmps_des_c END  cmps_des_c
  ,prodmapping
  ,sales_val
  ,sales_vol
  ,nvl(cast(sales_val as decimal(38,15))  / sum(sales_val) over(PARTITION BY YYYYMM,ta,market,prodmapping),0.0384615384615385)  valRate
  ,nvl(cast(sales_vol as decimal(38,15))  / sum(sales_vol) over(PARTITION BY YYYYMM,ta,market,prodmapping),0.0384615384615385)  volRate
from (
     -------------倒减ROC--------------
      SELECT 
        A.YYYYMM
        ,A.ta
        ,'ROC'  province_city
        ,A.market
        ,cmps_des_c
        ,case when A.prodmapping ='OTHERS' THEN 'Others_氨氯地平阿托伐他汀钙片' ELSE A.prodmapping END prodmapping
        ,A.sales_val - coalesce(B.sales_val ,0 ) sales_val
        ,A.sales_vol - coalesce(B.sales_vol,0)  sales_vol
      FROM brand_data A 
      LEFT JOIN (select YYYYMM,market,prodmapping,sum(sales_val) sales_val,sum(sales_vol) sales_vol from brand_data where province_city<>'全国' GROUP BY  YYYYMM,market,prodmapping)  B 
      ON A.YYYYMM =B.YYYYMM AND A.prodmapping=B.prodmapping AND A.markeT=B.market 
      WHERE A.province_city ='全国'  
      union all 
      ------计算好的比例拆分基础数据----
      select 
        YYYYMM,ta,province_city,market,cmps_des_c        
        ,case when A.prodmapping ='OTHERS' THEN 'Others_氨氯地平阿托伐他汀钙片' ELSE A.prodmapping END prodmapping

        ,sales_val,sales_vol
      FROM brand_data A 
      where province_city <>'全国'
)

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

-- ----------------------------------------------pack全国数据 rd 抗血栓2通用名 不用补数,niad GI,re 逻辑特殊，所以排除，仅保留CV 他汀、高血压数据-------------------------------------------------
------对pack 进行处理，关联维度表获取prod ,cmps,market 等数据，同时，对省份文件有的品牌填充原有品牌值，省份文件存在分子式但品牌不在的用others_cmps方式记录，其他为others_market,用于按特定比例拆分数据---
create or replace table tmp.tmp_zk_retail_pack 
using delta 
as
--将酒石酸美托洛尔的数据从高血压的数据中排除掉
------------------注意事项---------------------------------------------------------------
--使用相减的方法，每个省份都会剩下0.0几
--因此直接使用not in的方法
-----------------------------------------------------------------------------------------
with pack_without_metoprolol_tartrat as (

select *
from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all
-- 新数据中不含 酒石酸美托洛尔
            -- where zk_product_id not in (
            -- select distinct zk_product_id from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all
            -- where brand_flag = 'pack-CV-酒石酸美托洛尔.xlsx'
            -- )
)
,tmp as (
    select
    A.YYYYMM,
    a.zk_product_id,
    B.iqvia_pack_code,
    E.market_mapping market,
    case
        when b.cmps_des_c = '氨氯地平,阿托伐他汀'
        and b.prod_des_c is null then 'OTHERS_氨氯地平,阿托伐他汀'
        else B.prod_des_c
    end prod_des_c,
  
    case
        when b.cmps_des_c = '氨氯地平,阿托伐他汀'
        and b.prod_des_c is null 
    THEN  'Others_氨氯地平阿托伐他汀钙片'
    ELSE coalesce(C.prodmapping,concat('Others_',F.cmps_des_c),CONCAT('Others_cmps_',E.brand_mkt) ) 
    END  PROD_MAPPING,
        -- coalesce(C.prodmapping,concat('Others_',F.cmps_des_c),CONCAT('Others_cmps_',E.brand_mkt) ) PROD_MAPPING,

    SUM(A.sales_value) sales_value,
    sum(sales_unit) sales_unit,
    SUM(A.sales_unit * b.counting_unit) counting_unit
from
    pack_without_metoprolol_tartrat A
    left JOIN (
        select
            distinct product_id,
            prod_des_c,
            iqvia_pack_code,
            counting_unit/coalesce(unit,1) counting_unit,
            cmps_des_c
        from
            dwd.dwd_gnd_ext_retail_pack_property
    ) B ON A.zk_product_id = B.product_id 
    LEFT JOIN (
        SELECT
            DISTINCT brand_mkt,
            file_name,
            market_mapping
        FROM
            DWD.dwd_gnd_ext_retail_corresponding_relationship
    ) E ON A.brand_flag = E.file_name
    LEFT JOIN (
        SELECT
            DISTINCT YYYYMM,
            prodmapping,
            market
        FROM
            tmp.tmp_ZK_retail_brandTotal
    ) C ON A.YYYYMM = C.YYYYMM
    AND B.prod_des_c = C.prodmapping
    AND E.market_mapping = C.market ---NIAD 直接取pack数据，gi market 剔除逻辑，RE根据maket拆分 、RE包含雾化器数据-----
  left join (SELECT  DISTINCT cmps_des_c  FROM TMP.tmp_zk_retail_brandTotal where cmps_des_c is NOT null ) F 
  ON B.cmps_des_c=F.cmps_des_c 
where
    brand_flag NOT in (
        'pack-DM-口服降糖化学药.xlsx',
        'pack-GI-慢性胃炎胃溃疡-全国.xlsx',
        'pack-RE-慢阻肺-全国.xlsx',
        'pack-雾化器-全国&县域数据.xlsx',
        'pack-CV-抗血栓2通用名-全国.xlsx',
        'pack-RD-肾科-全国.xlsx'
        )
GROUP BY
    A.YYYYMM,
    a.zk_product_id,
    B.iqvia_pack_code,
    case
        when b.cmps_des_c = '氨氯地平,阿托伐他汀'
        and b.prod_des_c is null then 'OTHERS_氨氯地平,阿托伐他汀'
        else B.prod_des_c
    end,
    E.market_mapping,   
    --coalesce(C.prodmapping,concat('Others_',F.cmps_des_c),CONCAT('Others_cmps_',E.brand_mkt) )
    case
        when b.cmps_des_c = '氨氯地平,阿托伐他汀'
        and b.prod_des_c is null 
    THEN  'Others_氨氯地平阿托伐他汀钙片'
    ELSE coalesce(C.prodmapping,concat('Others_',F.cmps_des_c),CONCAT('Others_cmps_',E.brand_mkt) ) 
    END
)
-------美托洛尔 包含琥珀酸美托洛尔 需要通过market 重新定义拆分归属------------
  select 
        YYYYMM,zk_product_id,iqvia_pack_code,market,prod_des_c
        ,case when PROD_MAPPING ='Others_美托洛尔' and flag is not null then 'Others_美托洛尔' 
              when PROD_MAPPING ='Others_美托洛尔' and flag is  null then 'Others_cmps_高血压用药' 
              else PROD_MAPPING 
         end PROD_MAPPING 
        ,sales_value,sales_unit,counting_unit
from tmp 
left join  (select distinct iqvia_pack_code flag from tmp.tmp_zk_retail_pack_property where market ='Metoprolol Succinate Market') fl on tmp.iqvia_pack_code =fl.flag



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

-- MAGIC %md
-- MAGIC ### NIAD

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

-------------------niad 全国数据拆分-------------------------------------------------------------------
-- -----------------NIAD 加工 ,将拆分的数据替换成pack 源数据------------------------20240701修改版本------
insert overwrite table tmp.tmp_retail_sales_niad 
WITH national_pack as (
    -------获取niad 的全国pack 基础数据--------
    select
        A.YYYYMM,
        a.zk_product_id,
        B.iqvia_pack_code,
        E.market_mapping market,
        B.prod_des_c,
        CONCAT(E.brand_mkt, '_Others')  PROD_MAPPING,
        SUM(A.sales_value) sales_value,
        sum(sales_unit) sales_unit,
        SUM(A.sales_unit * b.counting_unit) counting_unit
    from
        dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all A
        left JOIN (
            select
                distinct product_id,
                prod_des_c,
                iqvia_pack_code,
                counting_unit/coalesce(unit,1 ) counting_unit
            from
                dwd.dwd_gnd_ext_retail_pack_property
        ) B ON A.zk_product_id = B.product_id
        LEFT JOIN (
            SELECT
                DISTINCT brand_mkt,
                file_name,
                market_mapping
            FROM
                DWD.dwd_gnd_ext_retail_corresponding_relationship
        ) E ON A.brand_flag = E.file_name
    where
        A.brand_flag in ('pack-DM-口服降糖化学药.xlsx')
        and A.zk_region = '全国' --and A.YYYYMM='202312' --,'pack-雾化器-全国&县域数据.xlsx')
    GROUP BY
        A.YYYYMM,
        a.zk_product_id,
        B.iqvia_pack_code,
        B.prod_des_c,
        CONCAT(E.brand_mkt, '_Others'),
        E.market_mapping
),
pack_prov as (
    -------获取niad 的省份pack 基础数据--------
    select
        A.YYYYMM,
        a.zk_product_id,
        B.iqvia_pack_code,
        E.market_mapping market,
        A.brand_flag,
        B.prod_des_c,
        NVL(B.PROD_DES_C, CONCAT(E.market_mapping, '_Others')) PROD_MAPPING,
        SUM(A.sales_value) sales_value,
        SUM(A.sales_unit) sales_unit,
        A.zk_region,
        sum(sales_unit * b.counting_unit) counting_unit
    from
        dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all A
        left JOIN (
            select
                distinct product_id,
                prod_des_c,
                iqvia_pack_code,
                counting_unit/coalesce(unit,1 ) counting_unit
            from
                dwd.dwd_gnd_ext_retail_pack_property
        ) B ON A.zk_product_id = B.product_id
        LEFT JOIN (
            SELECT
                DISTINCT brand_mkt,
                file_name,
                market_mapping
            FROM
                DWD.dwd_gnd_ext_retail_corresponding_relationship
        ) E ON A.brand_flag = E.file_name
    where
        A.brand_flag in ('pack-DM-口服降糖化学药.xlsx')
        and A.zk_region != '全国' --and A.YYYYMM='202312' --,'pack-雾化器-全国&县域数据.xlsx')
    GROUP BY
        A.YYYYMM,
        a.zk_product_id,
        B.iqvia_pack_code,
        B.prod_des_c,
        E.market_mapping,
        A.brand_flag,
        A.zk_region
)

/*
修改人：chenwu
修改时间：20250319
修改内容：NIAD不藏数据，用于页面藏数逻辑。brand_flag 默认都给1
*/
--拆分NIAD数据到ROC 
SELECT 
  A.YYYYMM
  ,A.iqvia_pack_code
  ,A.zk_product_id
  ,A.prod_des_c
  ,A.PROD_MAPPING
  ,'ROC'    province_city
  ,A.market 
  ,A.Sales_value  -  nvl(sales_V ,0) Sales_value
  ,A.sales_unit  -  nvl(SALES_U,0)    sales_unit
  ,A.counting_unit - nvl(COUNT_U,0)  counting_unit
  ,2 pack_flag
  ,1 brand_flag --case when c.mapp is null then 2 else 1 end  brand_flag
FROM national_pack A 
LEFT JOIN (
    SELECT 
      YYYYMM
      ,iqvia_pack_code
      ,zk_product_id
      ,prod_des_c
      ,PROD_MAPPING 
      ,market
      ,SUM(sales_value)  sales_V
      ,SUM(sales_unit)	 SALES_U
      ,SUM(counting_unit) COUNT_U
    FROM pack_prov
    GROUP BY 
      YYYYMM
      ,iqvia_pack_code
      ,zk_product_id
      ,prod_des_c
      ,PROD_MAPPING 
      ,market
)B  ON A.YYYYMM=B.YYYYMM
	AND A.zk_product_id=B.zk_product_id 
left join (select b.PROD_DES_C mapp from 
(select  distinct zk_brand_category from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all  where brand_cat_type='品牌'  and market='NIAD') a 
left join (select distinct PROD_DES_C,ZK_Prod_C  from dwd.dwd_inc_gnd_retail_b2c_label_total ) b on a.zk_brand_category =b.ZK_Prod_C )  c
on a.prod_des_c = c.mapp     
union all  
--pack+省份 原始数据
select
  YYYYMM
  ,iqvia_pack_code
  ,zk_product_id
  ,prod_des_c
  ,PROD_MAPPING 
  ,zk_region province_city
  ,market
  ,sales_value
  ,sales_unit
  ,counting_unit
  ,1 pack_flag
  ,1 brand_flag --case when b.mapp is null then 2 else 1 end  brand_flag
from pack_prov  a 
left join (select b.PROD_DES_C mapp from 
(select  distinct zk_brand_category from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all  where brand_cat_type='品牌'  and market='NIAD') a 
left join (select distinct PROD_DES_C,ZK_Prod_C  from dwd.dwd_inc_gnd_retail_b2c_label_total ) b on a.zk_brand_category =b.ZK_Prod_C )  b 
on a.PROD_MAPPING = b.mapp 

 

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

-- MAGIC %md
-- MAGIC ### RE

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

-- ------------------------------------------------------------RE 数据拆分-------------------------------------------
-- ------------------
-- insert overwrite table tmp.tmp_retail_result_re 
-- with Inhaled_tmp as (
-- ----拼接Respules Market数据---Inhaled 
-- 	select a.YYYYMM,a.zk_product_id,b.iqvia_pack_code,a.zk_region,sum(a.sales_value) sales_value,sum(sales_unit) sales_unit ,sum(a.sales_unit*(b.counting_unit/coalesce(b.unit,1 )))  counting_unit
-- 	 ,1 pack_flag
-- 	 ,1 brand_flag
-- 	 from  dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all  a 
-- 	 left join dwd.dwd_gnd_ext_retail_pack_property b on a.zk_product_id=b.product_id
-- 	 where brand_flag= 'pack-雾化器-全国&县域数据.xlsx'  and a.zk_region <>'全国'
--    -- and iqvia_pack_code in  (select iqvia_pack_code from tmp.tmp_zk_retail_pack_property where market ='Inhaled Extended Market by Brand')
-- 	 group by  a.YYYYMM,b.iqvia_pack_code,a.zk_region,a.zk_product_id
-- 	 union all 
-- 	-- -inhaled ROC数据----
-- 	  select  
-- 	  a.YYYYMM
-- 	  ,a.zk_product_id
-- 	  ,A.iqvia_pack_code
-- 	  ,'ROC'  province_city
-- 	  ,a.sales_value -inh.sales_value  sales_value
-- 	  ,a.sales_unit -inh.sales_unit    sales_unit
-- 	  ,a.counting_unit-inh.counting_unit  counting_unit
-- 	  ,2 pack_flag
-- 	  ,1 brand_flag
-- 	from (
-- 		  -----re 数据筛选--respules Market数据汇总到全国--
-- 		  select a.YYYYMM,a.zk_product_id,b.iqvia_pack_code,a.zk_region,sum(a.sales_value) sales_value,sum(sales_unit) sales_unit ,sum(a.sales_unit* (b.counting_unit/coalesce(b.unit,1 ))) counting_unit 
-- 		  from  dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all  a 
-- 		  left join dwd.dwd_gnd_ext_retail_pack_property b on a.zk_product_id=b.product_id
-- 		  where brand_flag='pack-RE-慢阻肺-全国.xlsx' 
-- 		  --  and b.iqvia_pack_code in (select iqvia_pack_code from dm.dm_zk_retail_pack_property where market ='Respules Market')
-- 		  and b.iqvia_pack_code in (select iqvia_pack_code from tmp.tmp_zk_retail_pack_property where market ='Respules Market')
-- 		  group by  a.YYYYMM,a.zk_region,b.iqvia_pack_code,a.zk_product_id
-- 	) a INNER join (
-- 		  select 
-- 			a.YYYYMM
-- 			,a.zk_product_id
-- 			,c.iqvia_pack_code
-- 			,coalesce(b.sales_value,0)sales_value
-- 			,coalesce(b.sales_unit,0)    sales_unit
-- 			,coalesce(b.counting_unit,0) counting_unit
-- 		  from (  select YYYYMM,zk_product_id from 
-- 					(select distinct zk_product_id from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all where zk_region <>'全国')
-- 					cross join (select distinct YYYYMM from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all where zk_region <>'全国')
-- 		  ) a 
-- 		  left join (select a.YYYYMM,a.zk_product_id,b.iqvia_pack_code,sum(a.sales_value) sales_value,sum(sales_unit) sales_unit,sum(a.sales_unit* (b.counting_unit/coalesce(b.unit,1 ))) counting_unit 
-- 		  from  dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all  a 
-- 		  left join dwd.dwd_gnd_ext_retail_pack_property b on a.zk_product_id=b.product_id
-- 		  where brand_flag= 'pack-雾化器-全国&县域数据.xlsx'  and a.zk_region <>'全国'
-- 		  group by  a.YYYYMM,b.iqvia_pack_code,a.zk_product_id) b 
-- 		  on a.YYYYMM=b.YYYYMM and a.zk_product_id=b.zk_product_id  
-- 			left join dwd.dwd_gnd_ext_retail_pack_property c on a.zk_product_id=c.product_id
-- 		-- WHERE b.iqvia_pack_code IN (select iqvia_pack_code from tmp.tmp_zk_retail_pack_property where market ='Inhaled Extended Market by Brand')

-- 	) inh on a.YYYYMM =inh.YYYYMM and a.zk_product_id=inh.zk_product_id
-- )
-- ,mkt as (
--   -------------补充mark 的roc 部分数据
-- 	select YYYYMM,ta,province_city,brand_cat_type,market,sales_val,sales_vol 
-- 	from tmp.tmp_retail_brand where ta='RE' AND brand_cat_type ='品类' and market ='慢性阻塞性肺疾病'
-- 	union all 
-- 	select 
-- 	a.YYYYMM,a.ta,'ROC' ,a.brand_cat_type,a.market,a.sales_val - b.sales_val sales_val ,a.sales_vol-b.sales_vol sales_vol
-- 	from 
-- 		(select YYYYMM,ta,brand_cat_type,market,sum(sales_val) sales_val,sum(sales_vol) sales_vol 
-- 		from tmp.tmp_retail_brand   where ta='RE' AND brand_cat_type ='品类' and market ='慢性阻塞性肺疾病'   and province_city ='全国'
-- 		group by 1,2,3,4
-- 	   ) a 
-- 	left join 
-- 	(
-- 	  select YYYYMM,ta,brand_cat_type,market,sum(sales_val) sales_val,sum(sales_vol) sales_vol 
-- 	  from tmp.tmp_retail_brand   where ta='RE' AND brand_cat_type ='品类' and market ='慢性阻塞性肺疾病'  and province_city <>'全国'
-- 	  group by 1,2,3,4
-- 	)b  on a.YYYYMM =b.YYYYMM and a.ta=b.ta and a.brand_cat_type=b.brand_cat_type  and a.market=b.market
-- )
-- ,total_no_inhaled as (
--   ----------------剔除inhaled 后剩余 MKT 总值------------
-- select a.YYYYMM,a.ta,a.province_city,a.brand_cat_type,a.market
--   ,B.brand_mkt  marketmapping
--   ,sales_val - coalesce(val,0 )   sales_val
--   ,sales_vol - coalesce(vol,0 )  sales_vol
--   -- ,sales_val - coalesce(sales_value,sal )   sales_val
--   -- ,sales_vol - coalesce(sales_unit,vol)  sales_vol
-- from mkt a 
-- left join (SELECT DISTINCT brand_mkt,market_mapping  FROM dwd.dwd_gnd_ext_retail_corresponding_relationship WHERE category_name IS NOT NULL  AND TA='RE') B 
-- on a.market=b.brand_mkt and B.brand_mkt = B.market_mapping
-- -- left join (select YYYYMM,zk_region,sum(sales_value) sales_value,sum(counting_unit) sales_unit from Inhaled_tmp  group by 1,2) c on a.province_city=c.zk_region and a.YYYYMM=c.YYYYMM
-- -- left join (select YYYYMM,'全国' zk_region,sum(sales_value) sal,sum(counting_unit) vol from Inhaled_tmp   group by 1) d on a.YYYYMM=d.YYYYMM and   a.province_city=d.zk_region
-- left join (
--           select YYYYMM,TA,province_city,brand_cat_type,market,sales_val  val,sales_vol vol from tmp.tmp_retail_brand where ta='RE' AND market ='Inhaled Extended Market' AND brand_cat_type ='品类'
--           UNION ALL 
--           select
--           a.YYYYMM,a.TA,'ROC',a.brand_cat_type,a.market
--           ,a.sales_val-b.sales_val
--           ,a.sales_vol-b.sales_vol 
--           from (select YYYYMM,TA,brand_cat_type,market,sales_val ,sales_vol from tmp.tmp_retail_brand where ta='RE' AND market ='Inhaled Extended Market' AND brand_cat_type ='品类' and province_city ='全国') a 
--           left join (select YYYYMM,TA,brand_cat_type,market,sum(sales_val) sales_val,sum(sales_vol) sales_vol from tmp.tmp_retail_brand where ta='RE' AND market ='Inhaled Extended Market' AND brand_cat_type ='品类' AND province_city<>'全国'
--           group by YYYYMM,TA,brand_cat_type,market ) b on a.YYYYMM=b.YYYYMM AND a.brand_cat_type=b.brand_cat_type AND a.market=b.market
-- ) c on     a.province_city=c.province_city and a.YYYYMM=c.YYYYMM 
-- where a.brand_cat_type = '品类' 
-- and  A.TA ='RE'  AND B.brand_mkt IS not null 
--  ), others as (
--   select 
-- a.YYYYMM,a.TA,a.PROD_DES_C,a.province_city,a.market,cmps_des_c,a.market
-- ,a.sales_val-b.sales_val sales_val, a.sales_vol-b.sales_vol  sales_vol
-- from (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='慢性阻塞性肺疾病' ) a 
-- inner join (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='Inhaled Extended Market') b 
-- on a.YYYYMM =b.YYYYMM and a.PROD_DES_C=b.PROD_DES_C and a.province_city =b.province_city 
-- left join (select distinct  prod_des_c,cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) C ON A.PROD_DES_C=C.prod_des_c
-- where a.sales_val-b.sales_val >0
-- union all 
-- select
-- a.YYYYMM,a.TA,a.PROD_DES_C,'ROC',a.market,a.cmps_des_c,a.market,a.sales_val - b.sales_val,a.sales_vol-b.sales_vol
-- from (
--       select 
--     a.YYYYMM,a.TA,a.PROD_DES_C,a.province_city,a.market,
--     cmps_des_c,a.market
--     ,a.sales_val-b.sales_val sales_val, a.sales_vol-b.sales_vol  sales_vol
--     from (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='慢性阻塞性肺疾病' ) a 
--     inner join (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='Inhaled Extended Market') b 
--     on a.YYYYMM =b.YYYYMM and a.PROD_DES_C=b.PROD_DES_C and a.province_city =b.province_city 
--     left join (select distinct  prod_des_c,cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) C ON A.PROD_DES_C=C.prod_des_c
--     where a.sales_val-b.sales_val >0 and a.province_city='全国'
-- ) a left join (
--     select 
--     a.YYYYMM,a.TA,a.PROD_DES_C,a.market,
--     cmps_des_c,a.market
--     ,sum(a.sales_val-b.sales_val) sales_val, sum(a.sales_vol-b.sales_vol)  sales_vol
--     from (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='慢性阻塞性肺疾病' ) a 
--     inner join (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='Inhaled Extended Market') b 
--     on a.YYYYMM =b.YYYYMM and a.PROD_DES_C=b.PROD_DES_C and a.province_city =b.province_city 
--     left join (select distinct  prod_des_c,cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) C ON A.PROD_DES_C=C.prod_des_c
--     where a.sales_val-b.sales_val >0 and a.province_city <> '全国'
-- group by     a.YYYYMM,a.TA,a.PROD_DES_C,a.market, cmps_des_c,a.market
-- ) b on a.YYYYMM=b.YYYYMM and a.PROD_DES_C=b.PROD_DES_C and a.market=b.market and a.cmps_des_c=b.cmps_des_c 
--  )

-- , brand_of_no_inhaled as (
--   -------------剔除inhaled 品牌数据及ROC数据------------
-- select a.YYYYMM,a.TA,a.PROD_DES_C,a.province_city
--   , A.market      submkt
--   ,cmps_des_c
--   ,B.brand_mkt   marketmapping
--   ,a.sales_val,a.sales_vol
-- from tmp.tmp_retail_brand a  
-- left join (select distinct brand_mkt,category_name from dwd.dwd_gnd_ext_retail_corresponding_relationship where category_name is not null  AND TA ='RE' AND category_name<>brand_mkt) B 
-- ON a.market=b.category_name 
-- left join (select distinct  prod_des_c,cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) C ON A.PROD_DES_C=C.prod_des_c
-- where brand_cat_type = '品牌'  
-- and a.market not in (select distinct brand_mkt from dwd.dwd_gnd_ext_retail_corresponding_relationship where brand_mkt is not null )
-- and TA ='RE'   AND A.market <> 'Inhaled Extended Market'
-- union all 
-- select 
--     a.YYYYMM,a.TA,a.PROD_DES_C,'ROC' province_city
--     ,a.submkt,a.cmps_des_c,a.marketmapping,a.sales_val-b.sales_val  sales_val,a.sales_vol-b.sales_vol  sales_vol
-- from (select a.YYYYMM,a.TA,a.PROD_DES_C,a.province_city
--         , A.market      submkt
--         ,cmps_des_c
--         ,B.brand_mkt   marketmapping
--         ,a.sales_val,a.sales_vol
--       from tmp.tmp_retail_brand a  
--       left join (select distinct brand_mkt,category_name from dwd.dwd_gnd_ext_retail_corresponding_relationship where category_name is not null  AND TA ='RE' AND category_name<>brand_mkt) B 
--       ON a.market=b.category_name 
--       left join (select distinct  prod_des_c,cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) C ON A.PROD_DES_C=C.prod_des_c
--       where brand_cat_type = '品牌'  
--       and a.market not in (select distinct brand_mkt from dwd.dwd_gnd_ext_retail_corresponding_relationship where brand_mkt is not null )
--       and TA ='RE'  AND A.market <> 'Inhaled Extended Market' and a.province_city='全国'
--       ) a 
--     left join (select a.YYYYMM,a.TA,a.PROD_DES_C 
--               , A.market      submkt
--               ,cmps_des_c
--               ,B.brand_mkt   marketmapping
--               ,sum(a.sales_val) sales_val,sum(a.sales_vol) sales_vol
--             from tmp.tmp_retail_brand a  
--             left join (select distinct brand_mkt,category_name from dwd.dwd_gnd_ext_retail_corresponding_relationship where category_name is not null  AND TA ='RE' AND category_name<>brand_mkt) B 
--             ON a.market=b.category_name 
--             left join (select distinct  prod_des_c,cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) C ON A.PROD_DES_C=C.prod_des_c
--             where brand_cat_type = '品牌'  
--             and a.market not in (select distinct brand_mkt from dwd.dwd_gnd_ext_retail_corresponding_relationship where brand_mkt is not null )
--             and TA ='RE'   AND A.market <> 'Inhaled Extended Market' and a.province_city<>'全国'
--             group by 1,2,3,4,5,6
--       ) b on a.YYYYMM =b.YYYYMM and a.PROD_DES_C =b.PROD_DES_C and a.submkt=b.submkt and a.marketmapping=b.marketmapping  
-- union all 
-- ----拼接 re 部分比inhaled 多出的brand 数据 
--  select * from others 

-- ),cmps_total as ( 
--   ------cmps 剔除inhaled 后 的基础数据----
--       select a.YYYYMM,ta,a.province_city,a.brand_cat_type,a.market,B.brand_mkt marketmapping
--         ,a.sales_val,a.sales_vol
--       from tmp.tmp_retail_brand a 
--       left join (SELECT DISTINCT brand_mkt,category_name  FROM dwd.dwd_gnd_ext_retail_corresponding_relationship WHERE category_name IS NOT NULL  AND TA='RE') B 
--       on a.market=b.category_name and B.brand_mkt<> B.category_name
--       where brand_cat_type = '品类'  
--       and A.TA ='RE' and B.brand_mkt is not null AND a.market <>'Inhaled Extended Market'
--       union all 
--       select
--           a.YYYYMM,a.ta,'ROC' ,a.brand_cat_type,a.market,a.marketmapping,a.sales_val-b.sales_val sales_val,a.sales_vol-b.sales_vol sales_vol  
--       from ( select a.YYYYMM,ta,a.province_city,a.brand_cat_type  --,c.zk_common_name,c.cmps_des_c
--             ,a.market,a.sales_val,a.sales_vol,B.brand_mkt marketmapping
--           from tmp.tmp_retail_brand a 
--           left join (SELECT DISTINCT brand_mkt,category_name  FROM dwd.dwd_gnd_ext_retail_corresponding_relationship WHERE category_name IS NOT NULL  AND TA='RE') B 
--           on a.market=b.category_name and B.brand_mkt<> B.category_name
--           where brand_cat_type = '品类'  
--           and A.TA ='RE' and B.brand_mkt is not null AND a.market <>'Inhaled Extended Market' and a.province_city = '全国'
--       )a left join (
--             select a.YYYYMM,ta,a.brand_cat_type ,B.brand_mkt marketmapping
--               ,a.market,sum(a.sales_val) sales_val,sum(a.sales_vol)  sales_vol
--             from tmp.tmp_retail_brand a 
--             left join (SELECT DISTINCT brand_mkt,category_name  FROM dwd.dwd_gnd_ext_retail_corresponding_relationship WHERE category_name IS NOT NULL  AND TA='RE') B 
--             on a.market=b.category_name and B.brand_mkt<> B.category_name
--             where brand_cat_type = '品类'  
--             and A.TA ='RE' and B.brand_mkt is not null AND a.market <>'Inhaled Extended Market' and a.province_city<>'全国'
--             group by 1,2,3,4,5
--       )b  on a.YYYYMM =b.YYYYMM and a.ta=b.ta and a.marketmapping=b.marketmapping and b.market =a.market and a.brand_cat_type=b.brand_cat_type 
-- union all 
-- select 
-- a.YYYYMM,a.TA,a.province_city,'',cmps_des_c,a.market
-- ,a.sales_val-b.sales_val sales_val, a.sales_vol-b.sales_vol  sales_vol
-- from (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='慢性阻塞性肺疾病' ) a 
-- inner join (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='Inhaled Extended Market') b 
-- on a.YYYYMM =b.YYYYMM and a.PROD_DES_C=b.PROD_DES_C and a.province_city =b.province_city 
-- left join (select distinct  prod_des_c,cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) C ON A.PROD_DES_C=C.prod_des_c
-- where a.sales_val-b.sales_val >0
-- union all 
-- select
-- a.YYYYMM,a.TA,'ROC','',a.cmps_des_c,a.market,a.sales_val - b.sales_val,a.sales_vol-b.sales_vol
-- from (
--       select 
--     a.YYYYMM,a.TA,a.PROD_DES_C,a.province_city,a.market,
--     cmps_des_c,a.market
--     ,a.sales_val-b.sales_val sales_val, a.sales_vol-b.sales_vol  sales_vol
--     from (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='慢性阻塞性肺疾病' ) a 
--     inner join (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='Inhaled Extended Market') b 
--     on a.YYYYMM =b.YYYYMM and a.PROD_DES_C=b.PROD_DES_C and a.province_city =b.province_city 
--     left join (select distinct  prod_des_c,cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) C ON A.PROD_DES_C=C.prod_des_c
--     where a.sales_val-b.sales_val >0 and a.province_city='全国'
-- ) a left join (
--     select 
--     a.YYYYMM,a.TA,a.PROD_DES_C,a.market,
--     cmps_des_c,a.market
--     ,sum(a.sales_val-b.sales_val) sales_val, sum(a.sales_vol-b.sales_vol)  sales_vol
--     from (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='慢性阻塞性肺疾病' ) a 
--     inner join (select YYYYMM,TA,PROD_DES_C,province_city,market,sales_val,sales_vol from tmp.tmp_retail_brand where TA='RE' AND brand_cat_type ='品牌' and market ='Inhaled Extended Market') b 
--     on a.YYYYMM =b.YYYYMM and a.PROD_DES_C=b.PROD_DES_C and a.province_city =b.province_city 
--     left join (select distinct  prod_des_c,cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) C ON A.PROD_DES_C=C.prod_des_c
--     where a.sales_val-b.sales_val >0 and a.province_city <> '全国'
-- group by     a.YYYYMM,a.TA,a.PROD_DES_C,a.market, cmps_des_c,a.market
-- ) b on a.YYYYMM=b.YYYYMM and a.PROD_DES_C=b.PROD_DES_C and a.market=b.market and a.cmps_des_c=b.cmps_des_c 





--  ),ratio as (
--   select 
--   YYYYMM
--   ,ta
--   ,province_city
--   ,market
--   ,cmps_des_c  
--   ,prodmapping
--   ,sales_val
--   ,sales_vol
--   ,sales_val  /sum(sales_val)over(PARTITION BY YYYYMM,ta,market,prodmapping)   radio_val
--   ,sales_vol / sum(sales_vol)over(PARTITION BY YYYYMM,ta,market,prodmapping)  radio_vol

-- from (
--       select 
--         MKT.YYYYMM
--         ,ta,MKT.province_city
--         ,market
--         ,concat('Others_cmps_',market ) cmps_des_c
--         ,concat('Others_cmps_',market ) prodmapping
--         ,MKT.sales_val - b.sales_val  sales_val
--         ,MKT.sales_vol - b.sales_vol  sales_vol
--       FROM total_no_inhaled MKT inner JOIN (select YYYYMM,province_city,marketmapping,sum(sales_val) sales_val,sum(sales_vol) sales_vol from cmps_total  group by YYYYMM,province_city,marketmapping) b
--       on MKT.YYYYMM=b.YYYYMM and MKT.province_city=b.province_city and MKT.market=b.marketmapping
--       where mkt.province_city <>'全国'
--       union all 
--       ------------计算others_cmps_brand-----------------
--       SELECT  a.YYYYMM,a.ta,a.province_city
--         ,a.marketmapping  market 
--         ,cmps_des_c
--         , coalesce(concat('Others_',b.cmps_des_c),concat('Others_',A.marketmapping) )  prodmapping
--         ,a.sales_val - coalesce(b.sales_val,0 )    sales_val
--         ,a.sales_vol - coalesce(b.sales_vol,0 )    sales_vol
--       from cmps_total a 
--       LEFT JOIN (select YYYYMM,province_city,submkt,cmps_des_c,marketmapping,sum(sales_val) sales_val,sum(sales_vol) sales_vol from brand_of_no_inhaled  GROUP BY YYYYMM,province_city,submkt,marketmapping,cmps_des_c ) b 
--       on a.YYYYMM=b.YYYYMM and a.province_city=b.province_city   and a.marketmapping=b.marketmapping and a.market=b.submkt
--       where a.province_city <> '全国'

--       union all 
--       SELECT 
--         YYYYMM,TA,province_city
--         ,marketmapping market
--         ,cmps_des_c
--         ,PROD_DES_C
--         ,sales_val
--         ,sales_vol
--       FROM brand_of_no_inhaled
--       where province_city <> '全国'
-- )
--  )
 

 
--  select   TMP.YYYYMM,tmp.zk_product_id,iqvia_pack_code,province_city,sales_value*radio_val sales_value,tmp.sales_unit*radio_vol sales_unit ,tmp.counting_unit*radio_vol counting_unit
-- ,2 pack_flag,case when tmp.PROD_MAPPING  like 'Others_%' then 2 else 1 end  brand_flag
-- from (
--   select a.YYYYMM,a.zk_product_id
--   ,b.prod_des_c,b.cmps_des_c
--   ,coalesce(c.PROD_DES_C,concat('Others_',F.cmps_des_c),concat('Others_cmps_',e.brand_mkt)) PROD_MAPPING
--   ,b.iqvia_pack_code,a.zk_region,sum(a.sales_value) sales_value,sum(sales_unit) sales_unit,sum(a.sales_unit* (b.counting_unit/coalesce(b.unit,1 ))) counting_unit 
--  from  dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all  a 
--  left join dwd.dwd_gnd_ext_retail_pack_property  b on a.zk_product_id=b.product_id
--  left join (select distinct PROD_DES_C  from tmp.tmp_retail_brand where TA='RE' AND market ='慢性阻塞性肺疾病' and brand_cat_type ='品牌') c on b.prod_des_c =c.PROD_DES_C
--  left join (select distinct cmps_des_c  from ratio  where cmps_des_c not in ( select cmps_des_c from others ))f on b.cmps_des_c =f.cmps_des_c
--  LEFT JOIN (SELECT DISTINCT brand_mkt,file_name FROM DWD.dwd_gnd_ext_retail_corresponding_relationship) E ON A.brand_flag = E.file_name 
--  where brand_flag='pack-RE-慢阻肺-全国.xlsx'
--        AND b.iqvia_pack_code NOT IN (
--               select b.iqvia_pack_code  from  dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all  a 
--               left join dwd.dwd_gnd_ext_retail_pack_property b on a.zk_product_id=b.product_id
--               where brand_flag= 'pack-雾化器-全国&县域数据.xlsx'  and a.zk_region <>'全国')
--       --  AND b.iqvia_pack_code  not in  (select iqvia_pack_code from tmp.tmp_zk_retail_pack_property where market ='Inhaled Extended Market by Brand')
--        AND b.iqvia_pack_code   in  (select iqvia_pack_code from tmp.tmp_zk_retail_pack_property where market ='RE Market')

--  group by  a.YYYYMM,b.iqvia_pack_code,a.zk_region,a.zk_product_id ,b.prod_des_c,b.cmps_des_c
--   ,coalesce(c.PROD_DES_C,concat('Others_',F.cmps_des_c),concat('Others_cmps_',e.brand_mkt))
-- ) tmp inner join ratio rate on TMP.YYYYMM=rate.YYYYMM  and   nvl(tmp.PROD_MAPPING ,'' )= nvl(rate.prodmapping,'' )  
--  union all
-- select 
--   a.YYYYMM
--   ,a.zk_product_id
--   ,A.iqvia_pack_code
--   ,zk_region province_city
--   ,sales_value
--   ,sales_unit
--   ,counting_unit,pack_flag,brand_flag
-- from Inhaled_tmp  a

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

-- MAGIC %md
-- MAGIC ## brand 比例拼接到 pack

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

-- MAGIC %md
-- MAGIC ### Inhaled 直取pack

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

/*
修改日期：20250311
修改人：chenwu
修改内容：新增Inhaled直取逻辑属于 RE 慢性阻塞性肺疾病
*/
create or replace temporary view Inhaled_pack_direct
as
with 
	---Inhaled 
Inhaled_prov as (
	select
    a.YYYYMM,
    a.zk_product_id,
    b.iqvia_pack_code,
    a.zk_region,
    '慢性阻塞性肺疾病' market,
    B.prod_des_c,
    SUM(A.sales_value) sales_value,
    sum(sales_unit) sales_unit,
    SUM(A.sales_unit * b.counting_unit) counting_unit
  from
    dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all a
    left JOIN (
      select
        distinct product_id,
        prod_des_c,
        iqvia_pack_code,
        counting_unit / coalesce(unit, 1) counting_unit
      from
        dwd.dwd_gnd_ext_retail_pack_property
    ) B ON A.zk_product_id = B.product_id
	where
		brand_flag = 'pack-雾化器-全国&县域数据.xlsx'
		and a.zk_region <> '全国' -- and iqvia_pack_code in  (select iqvia_pack_code from tmp.tmp_zk_retail_pack_property where market ='Inhaled Extended Market by Brand')
  group by 1,2,3,4,5,6
)
--全国的数据
,Inhaled_all as (
  select
    a.YYYYMM,
    a.zk_product_id,
    b.iqvia_pack_code,
    '全国' zk_region,
    '慢性阻塞性肺疾病' market,
    B.prod_des_c,
    SUM(A.sales_value) sales_value,
    sum(sales_unit) sales_unit,
    SUM(A.sales_unit * b.counting_unit) counting_unit
  from
    dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all a
    left JOIN (
      select
        distinct product_id,
        prod_des_c,
        iqvia_pack_code,
        counting_unit / coalesce(unit, 1) counting_unit
      from
        dwd.dwd_gnd_ext_retail_pack_property
    ) B ON A.zk_product_id = B.product_id
  where
    brand_flag = 'pack-雾化器-全国&县域数据.xlsx'
    and a.zk_region = '全国'
  group by 1,2,3,4,5,6
)

--倒减ROC部分 = 全国 - 省份
SELECT 
  A.YYYYMM
  ,A.iqvia_pack_code
  ,A.zk_product_id
  ,A.prod_des_c
  ,'ROC'    province_city
  ,A.market 
  ,A.Sales_value  -  nvl(sales_V ,0)  Sales_value
  ,A.sales_unit  -  nvl(SALES_U,0)    sales_unit
  ,A.counting_unit - nvl(COUNT_U,0)   counting_unit
  ,2 pack_flag
  ,1 brand_flag
FROM Inhaled_all A 
LEFT JOIN (
    SELECT 
      YYYYMM
      ,zk_product_id
      ,iqvia_pack_code
      ,SUM(sales_value)  sales_V
      ,SUM(sales_unit)	 SALES_U
      ,SUM(counting_unit) COUNT_U
    FROM Inhaled_prov --省份
    GROUP BY 
      YYYYMM
      ,zk_product_id
      ,iqvia_pack_code
)B  ON A.YYYYMM=B.YYYYMM AND A.zk_product_id=B.zk_product_id 
--拼接省份的数据
union all
select
    YYYYMM,
    iqvia_pack_code,
    zk_product_id,
    prod_des_c,
    zk_region as province_city,
    market,
    Sales_value,
    sales_unit,
    counting_unit,
    1 pack_flag,
    1 brand_flag
from Inhaled_prov

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

-- MAGIC %md
-- MAGIC ### 布地奈德雾化溶液 直取pack

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

/*
修改日期：20250311
修改人：chenwu
修改内容：新增 布地奈德雾化溶液 直取逻辑,属于 RE 慢性阻塞性肺疾病
*/
create or replace temporary view bdnd_pack_direct
as
with 
	---布地奈德雾化溶液
bdnd_prov as (
	select
    a.YYYYMM,
    a.zk_product_id,
    b.iqvia_pack_code,
    a.zk_region,
    '慢性阻塞性肺疾病' market,
    B.prod_des_c,
    SUM(A.sales_value) sales_value,
    sum(sales_unit) sales_unit,
    SUM(A.sales_unit * b.counting_unit) counting_unit
  from
    dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all a
    left JOIN (
      select
        distinct product_id,
        prod_des_c,
        iqvia_pack_code,
        counting_unit / coalesce(unit, 1) counting_unit
      from
        dwd.dwd_gnd_ext_retail_pack_property
    ) B ON A.zk_product_id = B.product_id
	where
		zk_common_name ='布地奈德' and brand_flag = 'pack-RE-慢阻肺-全国.xlsx'
		and a.zk_region <> '全国' -- and iqvia_pack_code in  (select iqvia_pack_code from tmp.tmp_zk_retail_pack_property where market ='Inhaled Extended Market by Brand')
  group by 1,2,3,4,5,6
)
--全国的数据
,bdnd_all as (
  select
    a.YYYYMM,
    a.zk_product_id,
    b.iqvia_pack_code,
    '全国' zk_region,
    '慢性阻塞性肺疾病' market,
    B.prod_des_c,
    SUM(A.sales_value) sales_value,
    sum(sales_unit) sales_unit,
    SUM(A.sales_unit * b.counting_unit) counting_unit
  from
    dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all a
    left JOIN (
      select
        distinct product_id,
        prod_des_c,
        iqvia_pack_code,
        counting_unit / coalesce(unit, 1) counting_unit
      from
        dwd.dwd_gnd_ext_retail_pack_property
    ) B ON A.zk_product_id = B.product_id
  where
    zk_common_name ='布地奈德' and brand_flag = 'pack-RE-慢阻肺-全国.xlsx'
    and a.zk_region = '全国'
  group by 1,2,3,4,5,6
)

--倒减ROC部分 = 全国 - 省份
SELECT 
  A.YYYYMM
  ,A.iqvia_pack_code
  ,A.zk_product_id
  ,A.prod_des_c
  ,'ROC'    province_city
  ,A.market 
  ,A.Sales_value  -  nvl(sales_V ,0)  Sales_value
  ,A.sales_unit  -  nvl(SALES_U,0)    sales_unit
  ,A.counting_unit - nvl(COUNT_U,0)   counting_unit
  ,2 pack_flag
  ,1 brand_flag
FROM bdnd_all A 
LEFT JOIN (
    SELECT 
      YYYYMM
      ,zk_product_id
      ,iqvia_pack_code
      ,SUM(sales_value)  sales_V
      ,SUM(sales_unit)	 SALES_U
      ,SUM(counting_unit) COUNT_U
    FROM bdnd_prov --省份
    GROUP BY 
      YYYYMM
      ,zk_product_id
      ,iqvia_pack_code
)B  ON A.YYYYMM=B.YYYYMM AND A.zk_product_id=B.zk_product_id 
--拼接省份的数据
union all
select
    YYYYMM,
    iqvia_pack_code,
    zk_product_id,
    prod_des_c,
    zk_region as province_city,
    market,
    Sales_value,
    sales_unit,
    counting_unit,
    1 pack_flag,
    1 brand_flag
from bdnd_prov


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

-- MAGIC %md
-- MAGIC ### 补数据 pack直取

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

/*
修改日期：20250311
修改人：chenwu
修改内容：
补数据
--1 月份数据会比季度数据提前来，没有拆分比例的时候，保留全国的数据。
--2 因为没有买 高血压总分子式 的数据，所以 高血压其他分子式 数据会漏掉，补进来。
*/

create or replace temporary view tmp_zk_retail_pack_lost
as
with all_with_no_brand_province as (
  select a.*,
    case when YYYYMM > b.last_yyymm then 'new_m' else 'lost' end as datatype
  from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all a 
  left join (select max(yyyymm) last_yyymm from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all ) b
  where YYYYMM > b.last_yyymm
      or (
        brand_flag ='pack-CV-高血压-化学药-全国.xlsx'
    )
)

,tmp as (
    select
    A.YYYYMM,
    a.zk_product_id,
    B.iqvia_pack_code,
    E.market_mapping market,
    case
        when b.cmps_des_c = '氨氯地平,阿托伐他汀'
        and b.prod_des_c is null then 'OTHERS_氨氯地平,阿托伐他汀'
        else B.prod_des_c
    end prod_des_c,
  
    case
        when b.cmps_des_c = '氨氯地平,阿托伐他汀'
        and b.prod_des_c is null 
    THEN  'Others_氨氯地平阿托伐他汀钙片'
    ELSE coalesce(C.prodmapping,concat('Others_',F.cmps_des_c),CONCAT('Others_cmps_',E.brand_mkt) ) 
    END  PROD_MAPPING,
        -- coalesce(C.prodmapping,concat('Others_',F.cmps_des_c),CONCAT('Others_cmps_',E.brand_mkt) ) PROD_MAPPING,
    datatype,
    SUM(A.sales_value) sales_value,
    sum(sales_unit) sales_unit,
    SUM(A.sales_unit * b.counting_unit) counting_unit
from
    all_with_no_brand_province A
    left JOIN (
        select
            distinct product_id,
            prod_des_c,
            iqvia_pack_code,
            counting_unit/coalesce(unit,1) counting_unit,
            cmps_des_c
        from
            dwd.dwd_gnd_ext_retail_pack_property
    ) B ON A.zk_product_id = B.product_id
    LEFT JOIN (
        SELECT
            DISTINCT brand_mkt,
            file_name,
            market_mapping
        FROM
            DWD.dwd_gnd_ext_retail_corresponding_relationship
    ) E ON A.brand_flag = E.file_name
    LEFT JOIN (
        SELECT
            DISTINCT YYYYMM,
            prodmapping,
            market
        FROM
            tmp.tmp_ZK_retail_brandTotal
    ) C ON A.YYYYMM = C.YYYYMM
    AND B.prod_des_c = C.prodmapping
    AND E.market_mapping = C.market ---NIAD 直接取pack数据，gi market 剔除逻辑，RE根据maket拆分 、RE包含雾化器数据-----
  left join (SELECT  DISTINCT cmps_des_c  FROM TMP.tmp_zk_retail_brandTotal where cmps_des_c is NOT null ) F 
  ON B.cmps_des_c=F.cmps_des_c 
where
    brand_flag  in (

        'pack-GI-慢性胃炎胃溃疡-全国.xlsx',
        "pack-CV-高血压-化学药-全国.xlsx"
        ,'pack-CV-他汀类+血脂康-全国.xlsx'

        -- 'pack-雾化器-全国&县域数据.xlsx',
        --'pack-DM-口服降糖化学药.xlsx'
        --'pack-RE-慢阻肺-全国.xlsx',
        -- ,'pack-RD-肾科-全国.xlsx' ,'pack-CV-抗血栓2通用名-全国.xlsx' 下面会单独拉取全部
        )
GROUP BY
    A.YYYYMM,
    a.zk_product_id,
    B.iqvia_pack_code,
    case
        when b.cmps_des_c = '氨氯地平,阿托伐他汀'
        and b.prod_des_c is null then 'OTHERS_氨氯地平,阿托伐他汀'
        else B.prod_des_c
    end,
    E.market_mapping,   
    --coalesce(C.prodmapping,concat('Others_',F.cmps_des_c),CONCAT('Others_cmps_',E.brand_mkt) )
    case
        when b.cmps_des_c = '氨氯地平,阿托伐他汀'
        and b.prod_des_c is null 
    THEN  'Others_氨氯地平阿托伐他汀钙片'
    ELSE coalesce(C.prodmapping,concat('Others_',F.cmps_des_c),CONCAT('Others_cmps_',E.brand_mkt) ) 
    END
    ,datatype
    )

, result(
  select 
        YYYYMM,zk_product_id,iqvia_pack_code,market,prod_des_c
        ,case when datatype = 'lost' and PROD_MAPPING ='Others_美托洛尔' and flag is not null then 'Others_美托洛尔' 
              when datatype = 'lost' and PROD_MAPPING ='Others_美托洛尔' and flag is  null then 'Others_cmps_高血压用药' 
              else PROD_MAPPING 
         end PROD_MAPPING 
         ,datatype
        ,sales_value,sales_unit,counting_unit
from tmp 
left join  (select distinct iqvia_pack_code flag from tmp.tmp_zk_retail_pack_property where market ='Metoprolol Succinate Market') fl on tmp.iqvia_pack_code =fl.flag
)

select yyyymm,zk_product_id,iqvia_pack_code,market,prod_des_c,PROD_MAPPING
,sales_value
,sales_unit
,counting_unit
from result
where (PROD_MAPPING = 'Others_cmps_高血压用药' and datatype='lost') or datatype <> 'lost'

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

-- MAGIC %md
-- MAGIC ### 小儿咳喘

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

--小儿咳喘的brand省份拆分比例
insert overwrite table tmp.tmp_zk_retail_brandTotal_xiaoer
with 
xiaoer_mkt as (--小儿咳喘 总数（ 包含布地奈德）
  select 
  YYYYMM
  ,TA
  ,province_city
  ,market
  ,zk_brand_category
  ,zk_common_name
  ,sum(sales_val) sales_val
  ,sum(sales_vol) sales_vol
  from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all
  where  market ='小儿咳喘' and TA = 'RE' and brand_cat_type = '品类'
  and ranked_by = 'volume'
  group by 1,2,3,4,5,6 
)
--布地奈德雾化溶液
,bdnd as (
  select 
  YYYYMM
  ,TA
  ,province_city
  ,market
  ,zk_brand_category
  ,zk_common_name
  ,sum(sales_val) sales_val
  ,sum(sales_vol) sales_vol
  from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all
  where market ='布地奈德雾化溶液' and  zk_brand_category = '布地奈德雾化溶液' and TA = 'RE' and brand_cat_type = '品类'
  and ranked_by = 'volume'
  group by 1,2,3,4,5,6
)
--小快克,可威,希舒美,美林,易坦静
,xiaoer_brand as (
  select 
  YYYYMM
  ,TA
  ,province_city
  ,market
  ,zk_brand_category
  ,zk_common_name
  ,sum(sales_val) sales_val
  ,sum(sales_vol) sales_vol
  from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all
  where market ='小儿咳喘' and TA = 'RE' and brand_cat_type = '品牌'
  and zk_brand_category in (
      '可威-颗粒' --奥司他韦
      ,'可威-胶囊' --奥司他韦
      ,'美林-混悬滴剂' --布洛芬
      ,'美林-混悬液'   --布洛芬
      -- ,'普米克令舒' --布地奈德
      ,'速福达-干混悬剂' --玛巴洛沙韦
      ,'速福达-片剂'     --玛巴洛沙韦
      -- ,'天晴速畅' --布地奈德
      ,'希舒美'  --阿奇霉素
      ,'小快克'  --氨酚黄那敏
      ,'易坦静'  --氨溴特罗
        )
  and ranked_by = 'volume'
  group by 1,2,3,4,5,6
)
--小儿 - (布地奈得 和 各品牌的 )= 小儿others
,xiaoer_mkt_m_bdnd as (
  select 
  A.YYYYMM
  ,A.TA
  ,A.province_city
  ,A.market
  ,'others_xiaoer' zk_brand_category
  ,'others_xiaoer' zk_common_name 
  ,case when sum(A.sales_val) - coalesce(sum(B.sales_val),0) - coalesce(sum(c.sales_val),0) <=0 then 0 
        else sum(A.sales_val) - coalesce(sum(B.sales_val),0) - coalesce(sum(c.sales_val),0) end sales_val
  ,case when sum(A.sales_vol) - coalesce(sum(B.sales_vol),0) - coalesce(sum(c.sales_vol),0) <=0 then 0 
        else sum(A.sales_vol) - coalesce(sum(B.sales_vol),0) - coalesce(sum(c.sales_vol),0) end sales_vol
  from xiaoer_mkt A
  left join (
    select
    YYYYMM
    ,province_city
    ,sum(sales_val) sales_val
    ,sum(sales_vol) sales_vol 
    from bdnd
    group by 1,2
  ) B on A.YYYYMM = B.YYYYMM and A.province_city = B.province_city 
  left join (
    select 
    YYYYMM
    ,province_city
    ,sum(sales_val) sales_val
    ,sum(sales_vol) sales_vol 
    from xiaoer_brand
    group by 1,2
  ) C on A.YYYYMM = C.YYYYMM and A.province_city = C.province_city
  group by 1,2,3,4,5,6
) 

-- 所有的数据
, all_xiaoer_formated as (
  select   *
  from xiaoer_mkt_m_bdnd 
  union all
  select   *
  from xiaoer_brand 
)

-- 倒减去roc
, all_xiaoer_w_roc as (
  select
  A.YYYYMM
  ,A.TA
  ,'ROC' province_city
  ,A.market
  ,A.zk_brand_category
  ,A.zk_common_name
  ,A.sales_val - coalesce(B.sales_val,0)  as sales_val
  ,A.sales_vol - coalesce(B.sales_vol,0)  as sales_vol 
  from all_xiaoer_formated A
  left join (
    select
    YYYYMM
    ,TA
    ,market
    ,zk_brand_category
    ,zk_common_name
    ,sum(sales_val) sales_val
    ,sum(sales_vol) sales_vol 
    from all_xiaoer_formated 
    where province_city <> '全国'
    group by 1,2,3,4,5
    ) B on A.TA = B.TA and A.YYYYMM=B.YYYYMM 
      and A.market = B.market 
      and A.zk_brand_category= B.zk_brand_category 
      and A.zk_common_name = B.zk_common_name
  where A.province_city = '全国'
union all
select
  YYYYMM
  ,TA
  ,province_city
  ,market
  ,zk_brand_category
  ,zk_common_name
  ,sales_val
  ,sales_vol 
from all_xiaoer_formated
where province_city <> '全国'
)
-- 计算拆分比例
SELECT 
  YYYYMM
  ,TA
  ,province_city
  ,market
  ,zk_common_name 
  ,zk_brand_category
  ,sales_val
  ,sales_vol
  ,nvl(cast(sales_val as decimal(38,15)) / sum(sales_val) over(PARTITION BY YYYYMM,TA,market,zk_brand_category),0.0384615384615385) valRate
  ,nvl(cast(sales_vol as decimal(38,15)) / sum(sales_vol) over(PARTITION BY YYYYMM,TA,market,zk_brand_category),0.0384615384615385) volRate
from all_xiaoer_w_roc


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

create or replace temporary view tmp_pack_xiaoer
as
with xiaoer_all_pack as (
  select
    a.YYYYMM,
    a.zk_product_id,
    a.zk_common_name,
    a.zk_product_name,
    a.zk_category_name,
    b.iqvia_pack_code,
    -- -- c.province_city,
    '' market,
    B.prod_des_c,
    SUM(A.sales_value) sales_value,
    sum(A.sales_unit) sales_unit,
    SUM(A.sales_unit * b.counting_unit) counting_unit
  from
    dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all a
    left JOIN (
      select
        distinct product_id,
        prod_des_c,
        iqvia_pack_code,
        counting_unit / coalesce(unit, 1) counting_unit
      from
        dwd.dwd_gnd_ext_retail_pack_property
    ) B ON A.zk_product_id = B.product_id
	where
		brand_flag = 'pack-RE-慢阻肺-全国.xlsx'	and a.zk_region = '全国' 
    and zk_common_name <>'布地奈德'
  group by 1,2,3,4,5,6,7,8
)

,tmp_brand_2_pack_xiaoer as (
  select   1 id  ,'布洛芬' b_cmp  ,'美林-混悬滴剂' b_brand  ,'布洛芬' p_cmp  ,'美林' p_product  ,'布洛芬混悬滴剂' p_category  union all
  select   2 id  ,'布洛芬' b_cmp  ,'美林-混悬液' b_brand  ,'布洛芬' p_cmp  ,'美林' p_product  ,'布洛芬混悬液' p_category  union all
  select   3 id  ,'奥司他韦' b_cmp  ,'可威-颗粒' b_brand  ,'奥司他韦' p_cmp  ,'可威' p_product  ,'磷酸奥司他韦颗粒' p_category  union all
  select   8 id  ,'奥司他韦' b_cmp  ,'可威-胶囊' b_brand  ,'奥司他韦' p_cmp  ,'可威' p_product  ,'磷酸奥司他韦胶囊' p_category  union all
  select   4 id  ,'阿奇霉素' b_cmp  ,'希舒美' b_brand  ,'阿奇霉素' p_cmp  ,'希舒美' p_product  ,'阿奇霉素干混悬剂' p_category  union all
  select   5 id  ,'氨酚黄那敏' b_cmp  ,'小快克' b_brand  ,'氨酚黄那敏' p_cmp  ,'小快克' p_product  ,'小儿氨酚黄那敏颗粒' p_category  union all
  select   6 id  ,'氨溴特罗' b_cmp  ,'易坦静' b_brand  ,'氨溴特罗' p_cmp  ,'易坦静' p_product  ,'氨溴特罗口服溶液' p_category  union all
  select   9 id  ,'玛巴洛沙韦' b_cmp  ,'速福达-片剂' b_brand  ,'玛巴洛沙韦' p_cmp  ,'速福达' p_product  ,'玛巴洛沙韦片' p_category  union all
  select   10 id  ,'玛巴洛沙韦' b_cmp  ,'速福达-干混悬剂' b_brand  ,'玛巴洛沙韦' p_cmp  ,'速福达' p_product  ,'玛巴洛沙韦干混悬剂' p_category  union all
  select   7 id  ,'others_xiaoer' b_cmp  ,'others_xiaoer' b_brand  ,'others_xiaoer' p_cmp  ,'others_xiaoer' p_product  ,'others_xiaoer' p_category
)

select 
A.YYYYMM
,A.zk_product_id
,A.iqvia_pack_code
,case when c.YYYYMM is null then '全国' else c.province_city end as province_city --如果季度拆分比例没有则不扩展province，直接为全国
,'小儿咳喘' market
,A.prod_des_c
,'小儿咳喘' PROD_MAPPING
,case when C.YYYYMM is null then A.sales_value else cast(A.sales_value * C.ValRate as decimal(38, 10)) end as sales_value
,case when C.YYYYMM is null then A.sales_unit else cast(A.sales_unit * C.VolRate as decimal(38, 10)) end as sales_unit
,case when C.YYYYMM is null then A.counting_unit else cast(A.counting_unit * C.VolRate as decimal(38, 10)) end as counting_unit
-- ,C.ValRate
-- ,B.B_cmp
,case when c.YYYYMM is null then 1 else 2 end as pack_flag --2 按比例拆分的，1 不拆分比例的
,case when c.YYYYMM is not null and B.B_cmp is null then 2 else 1 end as brand_flag --2 others_xiaoer 倒减出来的比例，1 是brand省份的比例
from xiaoer_all_pack A --pack数据
left join tmp_brand_2_pack_xiaoer B --brand 和 pack 的mapping
  on A.zk_common_name = B.p_cmp 
  and A.zk_product_name = B.p_product
  and A.zk_category_name = B.p_category
left join (select * from tmp.tmp_zk_retail_brandTotal_xiaoer ) C --拆分比例
  on A.YYYYMM = C.YYYYMM
  and case when B.B_cmp is null then 'others_xiaoer' else B.B_cmp end = C.zk_common_name -- 没有拆分比例的 归为others
  and case when B.B_brand is null then 'others_xiaoer' else B.B_brand end = C.zk_brand_category
  -- where A.iqvia_pack_code = '1092502'
  -- and A.YYYYMM = '202412'


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

-- MAGIC %md
-- MAGIC ### pack 拼接省份比例

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

---------------------------------------------将所有数据拆分到省份拼接--------------------------------------------------------------------
insert overwrite table tmp.tmp_retail_result 
with tmp as (
  -- 选取pack 粒度基础数据 用于筛选 抗血栓2通用名 和 rd 数据
	select A.YYYYMM,a.zk_product_id,B.iqvia_pack_code,E.market_mapping market,A.brand_flag,B.prod_des_c,NVL(B.PROD_DES_C,CONCAT(E.market_mapping,'_Others')) PROD_MAPPING,SUM(A.sales_value) sales_value,SUM(A.sales_unit) sales_unit,A.zk_region,sum(sales_unit * b.counting_unit) counting_unit 
from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all A 
left JOIN (select distinct product_id, prod_des_c,iqvia_pack_code,counting_unit/coalesce(unit,1 ) counting_unit from dwd.dwd_gnd_ext_retail_pack_property) B ON A.zk_product_id = B.product_id
LEFT JOIN (SELECT DISTINCT brand_mkt,file_name,market_mapping FROM DWD.dwd_gnd_ext_retail_corresponding_relationship) E ON A.brand_flag = E.file_name 
where A.zk_product_id not in (
  select distinct zk_product_id from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all
  where brand_flag = 'pack-CV-酒石酸美托洛尔.xlsx'
)
	GROUP BY A.YYYYMM,a.zk_product_id,B.iqvia_pack_code,B.prod_des_c, E.market_mapping ,A.brand_flag,A.zk_region
	
) ,GI AS (
--- gi 数据需要从 GI 文件中获取 market 为gi 的数据---
select A.YYYYMM,a.zk_product_id,B.iqvia_pack_code,E.market_mapping market,B.prod_des_c
,coalesce(C.prodmapping,concat('Others_',F.cmps_des_c),CONCAT('Others_cmps_',E.brand_mkt) ) PROD_MAPPING
,SUM(A.sales_value) sales_value,sum(sales_unit) sales_unit ,SUM(A.sales_unit * b.counting_unit) counting_unit
from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all A 
left JOIN (select distinct product_id, prod_des_c,iqvia_pack_code,counting_unit/coalesce(unit,1 ) counting_unit,CASE WHEN cmps_cod IN ('001658','002782','008617','001051','001853','008467') THEN '其他拉唑类' ELSE cmps_des_c END cmps_des_c from dwd.dwd_gnd_ext_retail_pack_property ) B ON A.zk_product_id = B.product_id
LEFT JOIN (SELECT DISTINCT brand_mkt,file_name,market_mapping FROM DWD.dwd_gnd_ext_retail_corresponding_relationship) E ON A.brand_flag = E.file_name
LEFT JOIN (SELECT DISTINCT YYYYMM,prodmapping,market FROM tmp.tmp_zk_retail_brandTotal) C ON A.YYYYMM = C.YYYYMM AND B.prod_des_c = C.prodmapping AND E.market_mapping = C.market
left join (SELECT  DISTINCT cmps_des_c  FROM TMP.tmp_zk_retail_brandTotal where cmps_des_c is NOT null ) F on b.cmps_des_c =f.cmps_des_c

where brand_flag   in   ( 'pack-GI-慢性胃炎胃溃疡-全国.xlsx')  
and (
  
     b.product_id in ( select distinct a.product_id from  tmp.tmp_zk_retail_pack_property a  where a.market ='Antacid anti GI swelling agent+PPI Oral' )
          -- b.product_id in ( select distinct a.product_id from  dws.dws_zk_retail_pack_property a  where a.market ='Retail_GI Market' )

)
GROUP BY A.YYYYMM,a.zk_product_id,B.iqvia_pack_code,B.prod_des_c
,coalesce(C.prodmapping,concat('Others_',F.cmps_des_c),CONCAT('Others_cmps_',E.brand_mkt) )
,E.market_mapping
)
--酒石酸美托洛尔
,national_metoprolol_tartrat as (
  select
    t1.YYYYMM,
    t2.iqvia_pack_code,
    t1.zk_product_id,
    t2.prod_des_c,
    NVL(t2.prod_des_c, '酒石酸美托洛尔_Others') PROD_MAPPING,
    t1.zk_region as province_city,
    '酒石酸美托洛尔' as market,
    t1.sales_value,
    t1.sales_unit,
    t1.sales_unit * NVL(t2.counting_unit,1) as counting_unit
  from
    dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all t1
    left JOIN (
      select
        distinct product_id,
        prod_des_c,
        iqvia_pack_code,
        counting_unit / coalesce(unit, 1) counting_unit
      from
        dwd.dwd_gnd_ext_retail_pack_property
    ) t2 ON t1.zk_product_id = t2.product_id
  where
    t1.brand_flag = 'pack-CV-酒石酸美托洛尔.xlsx'
    and t1.zk_region = '全国'
),
prov_metoprolol_tartrat as (
  select
    t1.YYYYMM,
    t2.iqvia_pack_code,
    t1.zk_product_id,
    t2.prod_des_c,
    NVL(t2.prod_des_c, '酒石酸美托洛尔_Others') PROD_MAPPING,
    t1.zk_region as province_city,
    '酒石酸美托洛尔' as market,
    t1.sales_value,
    t1.sales_unit,
    t1.sales_unit * NVL(t2.counting_unit,1) as counting_unit
  from
    dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all t1
    left JOIN (
      select
        distinct product_id,
        prod_des_c,
        iqvia_pack_code,
        counting_unit / coalesce(unit, 1) counting_unit
      from
        dwd.dwd_gnd_ext_retail_pack_property
    ) t2 ON t1.zk_product_id = t2.product_id
  where
    t1.brand_flag = 'pack-CV-酒石酸美托洛尔.xlsx'
    and t1.zk_region <> '全国'
),
all_metoprolol_tartrat as (
  select
    t1.YYYYMM,
    t1.iqvia_pack_code,
    t1.zk_product_id,
    t1.prod_des_c,
    t1.PROD_MAPPING,
    'ROC' as province_city,
    t1.market,
    t1.sales_value - t2.sales_value sales_value,
    t1.sales_unit - t2.sales_unit sales_unit,
    t1.counting_unit - t2.counting_unit counting_unit,
    2 as pack_flag,
    2 as brand_flag
  from
    national_metoprolol_tartrat t1
    left join (
      select
        YYYYMM,
        zk_product_id,
        sum(sales_value) sales_value,
        sum(sales_unit) sales_unit,
        sum(counting_unit) counting_unit
      from
        prov_metoprolol_tartrat
      group by
        YYYYMM,
        zk_product_id
    ) t2 on t1.YYYYMM = t2.YYYYMM
    and t1.zk_product_id = t2.zk_product_id
  union all
  select
    YYYYMM,
    iqvia_pack_code,
    zk_product_id,
    prod_des_c,
    PROD_MAPPING,
    province_city,
    market,
    sales_value,
    sales_unit,
    counting_unit,
    1 as pack_flag,
    2 as brand_flag
  from
    prov_metoprolol_tartrat
)

---他汀类+血脂康  +  高血压用药-----------
---拆分、倒减 pack_flag brand_flag 为0 ，取源数据为1
--这里tmp_zk_retail_pack和tmp_zk_retail_brandTotal都已将酒石酸美托洛尔排除
  select A.YYYYMM,a.iqvia_pack_code,a.zk_product_id,a.prod_des_c,A.PROD_MAPPING,b.province_city,A.market,cast(a.sales_value*b.ValRate as decimal(38,10)) sales_value,cast(a.sales_unit*b.VolRate  as decimal(38,10)) sales_unit,cast(a.counting_unit * b.VolRate  as decimal(38,10)) counting_unit
,2 pack_flag
,case when  PROD_MAPPING IN   ('多达一', '氨氯地平阿托伐他汀钙片', '天依宁', 'OTHERS_氨氯地平,阿托伐他汀') or flag is null  then 2 else 1 end  brand_flag
from tmp.tmp_zk_retail_pack a 
inner join tmp.tmp_zk_retail_brandTotal b on a.YYYYMM = b.YYYYMM and nvl(a.PROD_MAPPING ,'' )= nvl(b.prodmapping,'' ) and a.market = b.market
left join (select distinct PROD_DES_C flag from tmp.tmp_retail_brand) on a.prod_des_c = flag   
UNION ALL 
--- NIAD 
SELECT YYYYMM,iqvia_pack_code,zk_product_id,prod_des_c, PROD_MAPPING,province_city,market,sales_value,sales_unit ,counting_unit,pack_flag,brand_flag  from tmp.tmp_retail_sales_niad
--- GI 
UNION ALL 
select A.YYYYMM,a.iqvia_pack_code,a.zk_product_id,a.prod_des_c,A.PROD_MAPPING,b.province_city,A.market,a.sales_value*b.ValRate sales_value,a.sales_unit*b.VolRate sales_unit,a.counting_unit * b.VolRate counting_unit
,2 pack_flag
-- ,case when a.PROD_MAPPING =concat('Others_',a.market )   then 2 else 1 end brand_flag
,case when (a.PROD_MAPPING  like 'Others_%' and a.PROD_MAPPING not like 'Others_cmps%') or flag is null  then 2 else 1 end  brand_flag
from GI a 
inner join tmp.tmp_zk_retail_brandTotal b on a.YYYYMM = b.YYYYMM and a.PROD_MAPPING = b.prodmapping and a.market = b.market
left join (select distinct PROD_DES_C flag from tmp.tmp_retail_brand) on a.prod_des_c = flag   
--RD和抗血栓
union all 
select 
YYYYMM
,iqvia_pack_code
,zk_product_id
,prod_des_c
,PROD_MAPPING
,'全国'    zk_region
,case when brand_flag = 'pack-RD-肾科-全国.xlsx'  then 'RD Market' else 'Brilinta Market'  end market 
,sales_value
,sales_unit
,counting_unit
,2 pack_flag
,2 brand_flag
from tmp  
where brand_flag in ( 'pack-CV-抗血栓2通用名-全国.xlsx','pack-RD-肾科-全国.xlsx')  
-- 补充漏掉的部分
union all 
select
a.YYYYMM
,a.iqvia_pack_code
,a.zk_product_id
,a.prod_des_c
,a.PROD_MAPPING
,'全国'    zk_region
,a.market
,a.sales_value
,a.sales_unit
,a.counting_unit
,1 pack_flag
,1 brand_flag
FROM tmp_zk_retail_pack_lost a

-----拼接re结果
-- union all 
-- select 
-- a.YYYYMM
-- ,a.iqvia_pack_code
-- ,a.zk_product_id
-- ,b.prod_des_c
-- ,b.prod_des_c
-- ,a.province_city
-- ,'慢性阻塞性肺疾病'   market
-- ,a.sales_value
-- ,a.sales_unit
-- ,a.counting_unit
-- ,pack_flag
-- ,case when c.mapp is null then 2 else 1 end brand_flag
-- from tmp.tmp_retail_result_re a 
-- left join dwd.dwd_gnd_ext_retail_pack_property b 
-- on a.zk_product_id =b.product_id 
-- left join (select b.PROD_DES_C mapp from 
--           (select  distinct zk_brand_category from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all  where brand_cat_type='品牌'  and market='慢性阻塞性肺疾病') a 
--           left join (select distinct PROD_DES_C,ZK_Prod_C  from dwd.dwd_inc_gnd_retail_b2c_label_total ) b on a.zk_brand_category =b.ZK_Prod_C )  c
--           on b.prod_des_c  = c.mapp 
-- --酒石酸美托洛尔
-- union all
-- select
-- YYYYMM,
-- iqvia_pack_code,
-- zk_product_id,
-- prod_des_c,
-- PROD_MAPPING,
-- province_city,
-- market,
-- sales_value,
-- sales_unit,
-- counting_unit,
-- pack_flag,
-- brand_flag
-- from all_metoprolol_tartrat

-- inhaled直取数据
union all 
select
    YYYYMM,
    iqvia_pack_code,
    zk_product_id,
    prod_des_c,
    '' PROD_MAPPING,
    province_city,
    market,
    Sales_value,
    sales_unit,
    counting_unit,
    pack_flag,
    brand_flag
from Inhaled_pack_direct
--bdnd数据
union all 
select
    YYYYMM,
    iqvia_pack_code,
    zk_product_id,
    prod_des_c,
    '' PROD_MAPPING,
    province_city,
    market,
    Sales_value,
    sales_unit,
    counting_unit,
    pack_flag,
    brand_flag
from bdnd_pack_direct
--小儿咳喘
union all 
select
    YYYYMM,
    iqvia_pack_code,
    zk_product_id,
    prod_des_c,
    '' PROD_MAPPING,
    province_city,
    market,
    Sales_value,
    sales_unit,
    counting_unit,
    pack_flag,
    brand_flag
from tmp_pack_xiaoer 

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

-- MAGIC %md
-- MAGIC ## 写入dws

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

------------------------------将数据写入dws 并获取同期数据----------------------------
insert overwrite table dws.dws_zk_retail_sales 
select 
	 YYYYMM
	,iqvia_pack_code
	,zk_product_id
	,prod_des_c
	,PROD_MAPPING
	,province_city
	,market
	,sum(sales_value)      		  as sales_value      
	,sum(sales_value_ly)        as sales_value_ly   
	,sum(sales_unit)            as sales_unit       
	,sum(sales_unit_ly)         as sales_unit_ly    
	,sum(counting_unit)         as counting_unit    
	,sum(counting_unit_ly)      as counting_unit_ly 
	,pack_flag
	,brand_flag
  ,from_utc_timestamp(current_timestamp(),'UTC+8') 	    AS etl_insert_dt
  ,from_utc_timestamp(current_timestamp(),'UTC+8') 	    AS etl_update_dt
from (
	select
		YYYYMM 
		,iqvia_pack_code
		,zk_product_id
		,prod_des_c
		,PROD_MAPPING
		,province_city
		,market
		,sales_value    		    as sales_value      
		,0                      as sales_value_ly   
		,sales_unit             as sales_unit       
		,0                      as sales_unit_ly    
		,counting_unit          as counting_unit    
		,0                      as counting_unit_ly 
		,pack_flag
		,brand_flag
	  from tmp.tmp_retail_result
	union all 
	select
		cast(YYYYMM + 100 as int)   
		,iqvia_pack_code
		,zk_product_id
		,prod_des_c
		,PROD_MAPPING
		,province_city
		,market
		,0			    		        as sales_value      
		,sales_value            as sales_value_ly   
		,0			                as sales_unit       
		,sales_unit             as sales_unit_ly    
		,0					            as counting_unit    
		,counting_unit          as counting_unit_ly 
		,pack_flag
		,brand_flag
    from tmp.tmp_retail_result
	where YYYYMM + 100  <=(select max(yyyymm) from tmp.tmp_retail_result)
)
group by 
	 YYYYMM
	,iqvia_pack_code
	,zk_product_id
	,prod_des_c
	,PROD_MAPPING
	,province_city
	,market
	,pack_flag
	,brand_flag





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

/*
剔除利尿剂数据
*/
insert overwrite table tmp.tmp_zk_retail_sales 
select a.* from dws.dws_zk_retail_sales a 

left join dwd.dwd_gnd_ext_retail_pack_property c on a.zk_product_id = c.product_id 
where lower( c.atc2_cod ) <> 'c03' and a.market = 'RD Market'

union all 
select * from dws.dws_zk_retail_sales a 
where a.market <> 'RD Market' 

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

-- MAGIC %md
-- MAGIC ## 将ta总数写入 dwd层，保持原有逻辑不动

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

-- /*
-- 修改时间：20250311
-- 修改人：chenwu
-- 修改内容：由于TA大数没有了，所以要用pack的数据往上汇总，写入到原先的 dwd.dwd_gnd_ext_retail_nataional_ta_top_corp中
-- */
-- with corp_type_mapping as (-- corp 的类型
--   select  
--   case when CORP_COD ='F+N' then 'A5Z' ELSE CORP_COD END CORP_COD
--   ,case when upper(MNFL_DES) = 'LOCAL' then 'LOCAL' else 'MNC' end CORP_TYPE
--   from dwd.dwd_inc_gnd_retail_b2c_label_total
--   group by 
--   case when CORP_COD ='F+N' then 'A5Z' ELSE CORP_COD END
--   ,case when upper(MNFL_DES) = 'LOCAL' then 'LOCAL' else 'MNC' end  
-- )

-- ,packcode_corp_mapping as (--pack 和 corp 关系
--   select iqvia_pack_code, nvl(max(corp_cod),'others') corp_cod,nvl( max(corp_des), 'others') corp_des, nvl(MAX(corp_des_c),'others') corp_des_c
--   from tmp.tmp_zk_retail_pack_property
--   group by 1
-- )
-- ,market_ta_mapping as (
--   select '慢性阻塞性肺疾病' market ,'RE' ta union all
--   select '慢性胃炎、胃溃疡' market ,'GI' ta union all
--   select 'Inhaled Extended Market' market ,'RE' ta union all
--   select 'NIAD' market ,'NIAD' ta union all
--   select 'Brilinta Market' market ,'CV-2' ta union all
--   select '他汀类+血脂康' market ,'CV-3' ta union all
--   select '小儿咳喘' market ,'XIAOER' ta union all
--   select '高血压用药' market ,'CV-1' ta union all
--   select 'RD Market' market ,'RD' ta 
-- )
-- ,ta_sumall as (
--   select 
--   B.corp_des_c as corp_desc
--   ,B.CORP_COD as corp_name --code 暂时替代 英文名称
--   -- ,B.corp_cod as corp_cod
--   ,C.CORP_TYPE as corp_type
--   ,A.YYYYMM as sales_quarter
--   ,cast(sum(sales_value)/1000000.000000 as decimal(38,6))  sales_amount
--   ,D.ta as source_name_old
--   from  tmp.tmp_zk_retail_sales A
--   left join packcode_corp_mapping B on A.iqvia_pack_code = B.iqvia_pack_code
--   left join corp_type_mapping C on B.corp_cod = C.CORP_COD
--   left join market_ta_mapping D on D.market = A.market
--   where a.market<>'小儿咳喘' --and A.YYYYMM ='202412'
--   group by 
--     B.corp_des_c 
--     ,B.corp_cod 
--     -- ,B.corp_cod 
--     ,C.CORP_TYPE 
--     ,A.YYYYMM 
--     ,D.ta)  

-- ,sumall (--cv、niad
--   select *,'CV' AS  source_name  from ta_sumall where left(source_name_old,2)='CV' UNION ALL
--   select *,'CVRM' AS  source_name  from ta_sumall where left(source_name_old,2)='CV' OR source_name_old IN ('NIAD', 'RD') UNION ALL
--   select *,'GI' AS  source_name from ta_sumall where source_name_old='GI' UNION ALL
--   select *,'RD' AS  source_name from ta_sumall where source_name_old='RD' UNION ALL
--   select *,'RE' AS  source_name from ta_sumall where source_name_old='RE' UNION ALL
--   select *,'NIAD' AS  source_name from ta_sumall where source_name_old='NIAD' 
--   )  
-- ,rankresult ( --计算排名
--   select 
--     row_number() over(partition by sales_quarter,source_name order by sales_amount desc) rank1
--     ,corp_desc
--     ,corp_name
--     ,corp_type
--     ,sales_quarter
--     ,sales_amount
--     ,source_name
--   from sumall
-- )

-- -- 写入数据到 dwd层
-- insert into table dwd.dwd_gnd_ext_retail_nataional_ta_top_corp
-- (rank1,
-- corp_desc,
-- corp_name,
-- corp_type,
-- source_name,
-- sales_quarter,
-- sales_amount
-- -- ytd_gr,
-- -- ytd_ms,
-- -- ytd_delta_ms,
-- -- builtinarchivedate,
-- -- source_file_path,
-- -- source_file_name,
-- -- etl_insert_dt
-- )
-- select 
--   rank1
--   ,corp_desc
--   ,corp_name
--   ,corp_type
--   ,source_name
--   ,sales_quarter
--   ,sales_amount
-- from rankresult
-- where rank1<=20 --数量太多
-- -- and sales_quarter ='202412' and source_name='NIAD'


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

----------------将 ta_top_corp 的数据处理到dws-------------------
insert overwrite table dws.dws_zk_retail_ta_top_corp --------------------获取 ta total 本同期数据,用于后续取 total 及 az 值------------------------
SELECT
	source_name,
	rank1,
	corp_desc,
	CORP_COD,
	yyyymm,
	sum(sales_amount) 		sales_amount,
	sum(sales_amount_ly) 	sales_amount_ly
FROM
	(
	SELECT
		a.rank1,
		source_name,
    A.corp_desc,
    B.CORP_COD, --A.corp_name as CORP_COD, --自有的corp_name 是 corp_code
		CAST(sales_quarter AS int ) yyyymm ,
		sales_amount * 1000000  AS sales_amount,
		0 						AS sales_amount_ly
	FROM dwd.dwd_gnd_ext_retail_nataional_ta_top_corp a
	LEFT JOIN (SELECT DISTINCT ZK_Corp_C,CORP_COD FROM dwd.dwd_inc_gnd_retail_b2c_label_total) b
	ON	a.corp_desc = b.ZK_Corp_C
UNION ALL
	SELECT
		a.rank1,
		source_name,
    A.corp_desc,
		B.CORP_COD, --A.corp_name as CORP_COD, --自有的corp_name 是 corp_code
		CAST(sales_quarter + 100 AS int ) yyyymm ,
		0 						AS sales_amount,
		sales_amount * 1000000  AS sales_amount_ly
	FROM dwd.dwd_gnd_ext_retail_nataional_ta_top_corp a
	LEFT JOIN (SELECT DISTINCT ZK_Corp_C,CORP_COD FROM dwd.dwd_inc_gnd_retail_b2c_label_total) b
	ON	a.corp_desc = b.ZK_Corp_C
	WHERE
		CAST(sales_quarter + 100 AS int )
			<=( SELECT max(CAST(sales_quarter AS int ))
				FROM dwd.dwd_gnd_ext_retail_nataional_ta_top_corp ) 
)
GROUP BY
	1,2,3,4,5


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

--------------------------------------------------将 top_corp 的数据处理到dws----------------------------------------------------
--------------------获取 全国total 本同期数据------------------------
insert overwrite table dws.dws_zk_retail_top_corp 
SELECT
	yyyymm,
	rank1,
	corp_desc,
	corporation,
	-- source_name,
	sum(sales_amount) 		    as sales_amount,
	sum(sales_amount_ly) 	    as sales_amount_ly
FROM
	(
	SELECT
		nvl(rank1,'' )          as rank1 ,
		corp_desc,
		corporation,
		corp_type,
		-- source_name,
		CAST(sales_quarter AS int )                   as yyyymm,
		sales_amount * 1000000 	AS sales_amount,
		0 						          AS sales_amount_ly
	FROM dwd.dwd_gnd_ext_retail_nataional_top_corp
	UNION ALL
	SELECT
		nvl(rank1,'' ) rank1,
		corp_desc,
		corporation,
		corp_type,
		-- source_name,
		CAST(sales_quarter + 100 AS int )             as yyyymm,	
		0 						           AS sales_amount,
		sales_amount * 1000000   AS sales_amount_ly
	FROM dwd.dwd_gnd_ext_retail_nataional_top_corp
	WHERE CAST(sales_quarter + 100 AS int )
		 <=(SELECT
				max(CAST(sales_quarter AS int ))
			FROM dwd.dwd_gnd_ext_retail_nataional_top_corp ) )
GROUP BY
	yyyymm,
	rank1,
	corp_desc,
	corporation
	-- source_name

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

------------------对 配置表的pack 对应公司、分子式 根据 dwd_gnd_tbl_corp_change 表进行产品重新归属---------
insert overwrite table tmp.tmp_zk_retail_pack_property_corp 
select distinct a.iqvia_pack_code
  ,COALESCE(c.corp_cod,b.corp_cod,a.corp_cod)  corp_cod
  ,COALESCE(c.corp_des,b.corp_des,a.corp_des)  corp_des
  ,COALESCE(d.corp_des_c,a.corp_des_c )        corp_des_c
  ,COALESCE(c.manu_cod,b.manu_cod,a.manu_cod)  manu_cod 
  ,COALESCE(c.manu_des,b.manu_des,a.manu_des)  manu_des 
  ,COALESCE(e.manu_des_c,a.manu_des_c)         manu_des_c
from (select DISTINCT iqvia_pack_code,corp_cod,corp_des,corp_des_c,right(concat('000000',manu_cod),6 ) manu_cod,manu_des,manu_des_c,prod_cod from tmp.tmp_zk_retail_pack_property) a 
left join (select right(concat('000000000',prod_cod ),9) prod_cod,corp_cod,corp_des,right(concat('000000',manu_cod),6 )  manu_cod,manu_des from dwd.dwd_gnd_tbl_corp_change where pack_cod is  null  ) b 
on right(concat('000000000',A.prod_cod ),9)= b.prod_cod
left join (select if( pack_cod REGEXP '^[0-9]',right(concat('000000000000',pack_cod),12),pack_cod) pack_cod,corp_cod,corp_des,right(concat('000000',manu_cod),6 )  manu_cod,manu_des from dwd.dwd_gnd_tbl_corp_change where pack_cod is  not null  ) c 
on a.iqvia_pack_code = c.pack_cod
left join (select distinct corp_cod,corp_des_c from dwd.dwd_gnd_ext_retail_pack_property) d 
on coalesce(C.corp_cod,B.corp_cod ) = d.corp_cod 
Left join (select distinct right(concat('000000',manu_cod),6 )  manu_cod,manu_des,manu_des_c  from dwd.dwd_gnd_ext_retail_pack_property) e 
On coalesce(C.manu_cod,B.manu_cod )  = e.manu_cod 

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

-----------------------------计算others ta 和 cv others ---------------------------------------
-- 他汀类+血脂康  高血压用药  Brilinta Market  三个标签属于CV 需要倒减 cv others 及az_others --
--增加酒石酸美托洛尔，这个也属于CV
-------------------------------------CV OTHERS-------------------------------------------
-- total - market 
insert overwrite table tmp.tmp_dm_zk_retail_sales_tmp 
WITH  TEMP_CV AS (
 -------------------------------------------------CV数据关联 去重pack，避免数据重复-----------------------------------------------------
	SELECT    
	  a.YYYYMM
	  ,a.iqvia_pack_code
	  ,a.zk_product_id
	  ,a.prod_des_c
	  ,a.province_city
	  ,a.sales_value
	  ,a.sales_value_ly
	  ,a.sales_unit
	  ,a.sales_unit_ly
	  ,a.counting_unit
	  ,a.counting_unit_ly
	  ,pack_flag
	  ,brand_flag
	  ,collect_list(a.market)  source
	FROM tmp.tmp_zk_retail_sales a 
	inner join (
	 ---CV 中对重复数据任意取一个文件来源的 pack ，避免数据重复---
      SELECT    
            YYYYMM
            ,iqvia_pack_code
            ,zk_product_id
            ,prod_des_c
            ,province_city
            ,max(market)         market 
      FROM tmp.tmp_zk_retail_sales 
      where market in ('他汀类+血脂康','高血压用药','Brilinta Market','酒石酸美托洛尔') 
      group by 1,2,3,4,5
	) b on a.yyyymm = b.yyyymm 
      and a.iqvia_pack_code =b.iqvia_pack_code 
      and a.zk_product_id = b.zk_product_id  
      and a.province_city = b.province_city 
      and a.market = b.market
	where a.market in ('他汀类+血脂康','高血压用药','Brilinta Market','酒石酸美托洛尔') 
	group by 1,2,3,4,5,6,7,8,9,10,11,12,13
)
, CV_PACK_CORP_DATA AS (
-------------------------------------获取pack + corp 粒度 的CV数据 并倒减cv下 az others 值-----------------------------------------------
--------------------拼接CV 原始AZ PACK数据--------------------
    SELECT  
        YYYYMM
        ,A.iqvia_pack_code
        ,A.zk_product_id
        ,A.prod_des_c
        ,A.province_city
        ,B.corp_cod
        ,sales_value
        ,sales_value_ly
        ,sales_unit
        ,sales_unit_ly
        ,counting_unit
        ,counting_unit_ly  
        ,source
        ,pack_flag
        ,brand_flag
    from TEMP_CV A 
    inner JOIN (select distinct iqvia_pack_code,corp_cod,corp_des,corp_des_c from tmp.tmp_zk_retail_pack_property_corp) b  
    ON A.iqvia_pack_code=B.iqvia_pack_code 
    where corp_cod ='A5Z'
  UNION ALL 
--------------------CV下的AZ OTHERS,即 cv 下的 AZ 汇总值减 pack 粒度az 汇总--------------------
  SELECT 
        CV_PACK.YYYYMM
        ,'CV_AZ_OTHERS'                                      as iqvia_pack_code 
        ,'CV_AZ_OTHERS'                                      as zk_product_id
        ,null                                                as prod_des_c
        ,'ROC'                                               as province_city
        ,CV_PACK.corp_cod
        ,0--,case when Total.sales_amount is not null then Total.sales_amount - CV_PACK.sales_value else 0 end        as sales_value
        ,0--,case when Total.sales_amount_ly is not null then Total.sales_amount_ly - CV_PACK.sales_value_ly else 0 end  as sales_value_ly
        ,0,0,0,0
        ,null 
        ,0  pack_flag
        ,0  brand_flag
  FROM (
        SELECT 
          YYYYMM
          ,corp_cod
          ,sum(sales_value)			      sales_value		
          ,sum(sales_value_ly)        sales_value_ly
        from TEMP_CV a 
        inner join (select distinct iqvia_pack_code,corp_cod,corp_des,corp_des_c from tmp.tmp_zk_retail_pack_property_corp ) b 
        on a.iqvia_pack_code = b.iqvia_pack_code  
         where corp_cod ='A5Z'
        group by 1,2
  ) CV_PACK
  LEFT JOIN ( select yyyymm,sales_amount,sales_amount_ly from dws.dws_zk_retail_ta_top_corp where source_name='CV' --AND CORP_COD='A5Z'
              AND CORP_COD ='A5Z'
            )  Total
  ON CV_PACK.YYYYMM = Total.YYYYMM
  UNION ALL 
--------------------拼接CV CORP非az 值--------------------
  SELECT  
        YYYYMM
        ,A.iqvia_pack_code
        ,A.zk_product_id
        ,A.prod_des_c
        ,A.province_city
        ,B.corp_cod
        ,sales_value
        ,sales_value_ly
        ,sales_unit
        ,sales_unit_ly
        ,counting_unit
        ,counting_unit_ly  
        ,source
        ,pack_flag
        ,brand_flag
  from TEMP_CV A 
  left JOIN (select distinct iqvia_pack_code,corp_cod,corp_des,corp_des_c from tmp.tmp_zk_retail_pack_property_corp ) b  
  ON A.iqvia_pack_code=B.iqvia_pack_code 
  where nvl(corp_cod,'') <>'A5Z'
) 
   ------ 倒减完 AZ 的所有CV PACK 数据---------
  SELECT  
      YYYYMM
      ,iqvia_pack_code
      ,zk_product_id
      ,prod_des_c
      ,province_city
      ,corp_cod
      ,sales_value
      ,sales_value_ly
      ,sales_unit
      ,sales_unit_ly
      ,counting_unit
      ,counting_unit_ly  
      ,source
      ,pack_flag
      ,brand_flag
      ,'CV' FLAG 
  from CV_PACK_CORP_DATA 
  UNION ALL 
------- 获取 CV_OTHERS----------
  SELECT 
       CV_PACK.YYYYMM
      ,'CV_OTHERS'                                      as iqvia_pack_code 
      ,'CV_OTHERS'                                      as zk_product_id
      ,null                                             as prod_des_c
      ,'ROC'                                           as province_city
      ,'CV_OTHERS'                                      AS corp_cod
      ,0--,case when Total.sales_amount is not null then Total.sales_amount - CV_PACK.sales_value else 0 end         as sales_value
      ,0--,case when Total.sales_amount_ly is not null then Total.sales_amount_ly - CV_PACK.sales_value_ly else 0 end as sales_value_ly
      ,0,0,0,0
      ,null 
      ,0                                                pack_flag
      ,0                                                brand_flag
      ,'CV'
  FROM (
    -------获取所有CV 数据-------
      SELECT 
           YYYYMM
          ,sum(sales_value)			                        sales_value		
          ,sum(sales_value_ly)                          sales_value_ly
      from CV_PACK_CORP_DATA
      group by 1
  ) CV_PACK
  LEFT JOIN ( select yyyymm,sales_amount,sales_amount_ly from dws.dws_zk_retail_ta_top_corp where source_name='CV' --AND CORP_COD='A5Z'
              AND rank1 ='Total'
             )  Total 
  ON CV_PACK.YYYYMM = Total.YYYYMM
  UNION ALL 
---------- 除了 CV 外的其他数据----------
  SELECT  DISTINCT 
      YYYYMM
      ,A.iqvia_pack_code
      ,A.zk_product_id
      ,A.prod_des_c
      ,A.province_city
      ,B.corp_cod
      ,round(sales_value,2)			sales_value
      ,round(sales_value_ly,2)      sales_value_ly
      ,round(sales_unit,2)          sales_unit
      ,round(sales_unit_ly,2)       sales_unit_ly
      ,round(counting_unit,2)       counting_unit
      ,round(counting_unit_ly,2)    counting_unit_ly
      ,collect_list(market)         source 
      ,pack_flag
      ,brand_flag
      ,CASE WHEN A.market IN ('NIAD','RD Market') THEN 'CVRM' ELSE '' END FLAG
  from tmp.tmp_zk_retail_sales A 
  LEFT JOIN  (select distinct iqvia_pack_code,corp_cod from tmp.tmp_zk_retail_pack_property_corp ) b  
  ON A.iqvia_pack_code=B.iqvia_pack_code 
  where market  not in ('他汀类+血脂康','高血压用药','Brilinta Market','酒石酸美托洛尔')  
  group by 
      YYYYMM
      ,A.iqvia_pack_code
      ,A.zk_product_id
      ,A.prod_des_c
      ,A.province_city
      ,B.corp_cod
      ,round(sales_value,2)			 
      ,round(sales_value_ly,2)       
      ,round(sales_unit,2)           
      ,round(sales_unit_ly,2)        
      ,round(counting_unit,2)        
      ,round(counting_unit_ly,2)  
      ,pack_flag
      ,brand_flag
      ,CASE WHEN A.market IN ('NIAD','RD Market') THEN 'CVRM' ELSE '' END  




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

--------------------------------------------------------------------倒减cvrm az 及cvrm total -----------------------------------------------------
insert overwrite table tmp.tmp_dm_zk_retail_sales_tmp2 
with alldata as (
  SELECT 
      YYYYMM
      ,iqvia_pack_code
      ,zk_product_id
      ,prod_des_c
      ,province_city
      ,corp_cod
      ,sales_value
      ,sales_value_ly
      ,sales_unit
      ,sales_unit_ly
      ,counting_unit
      ,counting_unit_ly  
      ,source
      ,pack_flag
      ,brand_flag
      ,FLAG
FROM TMP.tmp_dm_zk_retail_sales_tmp
UNION ALL 
SELECT  
        A.YYYYMM
        ,'CVRM_AZ_OTHERS'   iqvia_pack_code
        ,'CVRM_AZ_OTHERS'   zk_product_id
        ,NULL                prod_des_c
        ,'ROC'
        ,A.corp_cod
        ,0--,case when B.sales_amount is not null then B.sales_amount - A.sales_value else 0 end    sales_value
        ,0--case when B.sales_amount_ly is not null then B.sales_amount_ly - A.sales_value_ly else 0 end  sales_value_ly
        ,0    sales_unit
        ,0    sales_unit_ly
        ,0    counting_unit
        ,0    counting_unit_ly  
        ,NULL source
        ,0    pack_flag
        ,0    brand_flag
        ,'CVRM'
FROM (  SELECT 
          YYYYMM,corp_cod    
          ,SUM(sales_value)  sales_value
          ,SUM(sales_value_ly) sales_value_ly
        FROM tmp.tmp_dm_zk_retail_sales_tmp WHERE corp_cod ='A5Z' AND FLAG IN ('CV','CVRM') 
        GROUP BY 1,2
) A 
LEFT JOIN (select CORP_COD,yyyymm,sales_amount,sales_amount_ly from dws.dws_zk_retail_ta_top_corp where source_name='CVRM' AND CORP_COD='A5Z') B 
ON A.YYYYMM=B.yyyymm AND A.corp_cod=B.CORP_COD
)

    SELECT 
      YYYYMM
      ,iqvia_pack_code
      ,zk_product_id
      ,prod_des_c
      ,province_city
      ,corp_cod
      ,sales_value
      ,sales_value_ly
      ,sales_unit
      ,sales_unit_ly
      ,counting_unit
      ,counting_unit_ly  
      ,source
      ,pack_flag
      ,brand_flag
      ,FLAG
from  alldata
union all 
SELECT  
        A.YYYYMM
        ,'CVRM_OTHERS'   iqvia_pack_code
        ,'CVRM_OTHERS'   zk_product_id
        ,NULL            prod_des_c
        ,'ROC'
        ,'CVRM_OTHERS'   corp_cod
        ,0--,case when B.sales_amount is not null then B.sales_amount - A.sales_value else 0 end    sales_value
        ,0--case when B.sales_amount_ly is not null then B.sales_amount_ly - A.sales_value_ly else 0 end  sales_value_ly
        ,0    sales_unit
        ,0    sales_unit_ly
        ,0    counting_unit
        ,0    counting_unit_ly  
        ,NULL source
        ,0    pack_flag
        ,0    brand_flag
        ,'CVRM'
FROM (  SELECT 
          YYYYMM    
          ,SUM(sales_value)  sales_value
          ,SUM(sales_value_ly) sales_value_ly
        FROM  alldata  WHERE  FLAG IN ('CV','CVRM') 
        GROUP BY 1
) A 
LEFT JOIN (select yyyymm,sales_amount,sales_amount_ly from dws.dws_zk_retail_ta_top_corp where corp_desc='CVRM') B 
ON A.YYYYMM=B.yyyymm  


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

------------------------8.2不上线，但后续肯定会上线-----------------
-- -----Non AZ Retail Related Market 逻辑，仅需要全国数据，该市场均为各文件中没有市场的数据，逻辑中存在仅取 gi re 文件数据，原因是 高血压、他汀血脂康、niad 在拆分时 使用的时整个文件拆分，而RD 、抗血栓 整个文件是全取，non az 市场数据若有，就已经存在结果表里; 反之 GI 、RE(含Inhaled) 拆分时通过 tblmarket 框选了对应市场的数据进行拆分，其他数据被排除了(即NON AZ 市场数据，该数据不属于任何TA ,但归属于TOTAL)，后续这部分数据需要 在pbi 展示,所有需要在倒减 others_ta 时加上。
-- INSERT INTO tmp.tmp_dm_zk_retail_sales_tmp2
-- select 
--       YYYYMM
--       ,iqvia_pack_code
--       ,zk_product_id
--       ,prod_des_c
--       ,province_city
--       ,corp_cod
--       ,SUM(sales_value)		sales_value
--       ,SUM(sales_value_ly)    sales_value_ly
--       ,SUM(sales_unit)        sales_unit
--       ,SUM(sales_unit_ly)     sales_unit_ly
--       ,sum(counting_unit)     counting_unit
--       ,sum(counting_unit_ly)  counting_unit_ly
--       ,source
--       ,pack_flag
--       ,brand_flag
--       ,flag
-- from (

--       --------------获取本期、同期数据----------
--       select 
--             a.YYYYMM
--             ,b.iqvia_pack_code
--             ,a.zk_product_id
--             ,b.prod_des_c
--             ,'ROC'                       province_city
--             ,coalesce(c.corp_cod,b.corp_cod )  corp_cod
--             ,sum(a.sales_value)   sales_value
--             ,0                    sales_value_ly
--             ,sum(a.sales_unit)    sales_unit
--             ,0                    sales_unit_ly
--             ,sum(a.sales_unit *(a.counting_unit / coalesce(b.unit,1 )))    counting_unit
--             ,0                    counting_unit_ly
--             ,collect_list(a.brand_flag )  source
--             ,2    pack_flag
--             ,case when prod.prod_des_c  is null  THEN 2 else  1 END  brand_flag
--             ,null flag 
--       from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all  a 
--       left join dwd.dwd_gnd_ext_retail_pack_property b on a.zk_product_id = b.product_id 
--       left join tmp.tmp_zk_retail_pack_property_corp c on c.iqvia_pack_code = right(concat('0000000',b.iqvia_pack_code ),7 ) 
--       left join (SELECT DISTINCT b.prod_des_c FROM TMP.tmp_zk_retail_nataional_brand_union A 
--             LEFT JOIN (SELECT DISTINCT iqvia_pack_code,prod_des_c FROM tmp.tmp_zk_retail_pack_property) b  on a.iqvia_pack_code=b.iqvia_pack_code
--             ) prod on b.prod_des_c=prod.prod_des_c 
--       where b.iqvia_pack_code in ( select distinct iqvia_pack_code from tmp.tmp_zk_retail_pack_property where market = 'Non AZ Retail Related Market' ) 
--             and a.zk_region ='全国'
--             AND a.brand_flag in ('pack-GI-慢性胃炎胃溃疡-全国.xlsx','pack-RE-慢阻肺-全国.xlsx')
--             and b.iqvia_pack_code not in (select distinct iqvia_pack_code from tmp.tmp_dm_zk_retail_sales_tmp )
--       group by a.YYYYMM,b.iqvia_pack_code,a.zk_product_id,b.prod_des_c,a.brand_flag
--             ,coalesce(c.corp_cod,b.corp_cod )
--             ,case when prod.prod_des_c  is null  
--                   THEN 2 else  1 
--             END 
--       union all 
--       select 
--             cast(a.YYYYMM + 100  as int )            YYYYMM
--             ,b.iqvia_pack_code
--             ,a.zk_product_id
--             ,b.prod_des_c
--             ,'ROC'                       province_city
--             ,coalesce(c.corp_cod,b.corp_cod )  corp_cod
--             ,0                    sales_value
--             ,sum(a.sales_value)   sales_value_ly
--             ,0                    sales_unit
--             ,sum(a.sales_unit)    sales_unit_ly
--             ,0                    counting_unit
--             ,sum(a.sales_unit *(a.counting_unit / coalesce(b.unit,1 )))    counting_unit_ly
--             ,collect_list(a.brand_flag )  source
--             ,2    pack_flag
--             ,case when prod.prod_des_c  is null  THEN 2 else  1 END  brand_flag
--             ,null flag 
--       from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all  a 
--       left join dwd.dwd_gnd_ext_retail_pack_property b on a.zk_product_id = b.product_id 
--       left join tmp.tmp_zk_retail_pack_property_corp c on c.iqvia_pack_code = right(concat('0000000',b.iqvia_pack_code ),7 ) 
--       left join (SELECT DISTINCT b.prod_des_c FROM TMP.tmp_zk_retail_nataional_brand_union A 
--             LEFT JOIN (SELECT DISTINCT iqvia_pack_code,prod_des_c FROM tmp.tmp_zk_retail_pack_property) b  on a.iqvia_pack_code=b.iqvia_pack_code
--             ) prod on b.prod_des_c=prod.prod_des_c 
--       where b.iqvia_pack_code in ( select distinct iqvia_pack_code from tmp.tmp_zk_retail_pack_property where market = 'Non AZ Retail Related Market' ) 
--             and a.zk_region ='全国'
--             AND a.brand_flag in ('pack-GI-慢性胃炎胃溃疡-全国.xlsx','pack-RE-慢阻肺-全国.xlsx')
--             and b.iqvia_pack_code not in (select distinct iqvia_pack_code from tmp.tmp_dm_zk_retail_sales_tmp )
--             and a.YYYYMM + 100  <= (select max(YYYYMM) from  tmp.tmp_dm_zk_retail_sales_tmp )
--       group by cast(a.YYYYMM + 100  as int )   
--             ,b.iqvia_pack_code,a.zk_product_id,b.prod_des_c,a.brand_flag
--             ,coalesce(c.corp_cod,b.corp_cod )
--             ,case when prod.prod_des_c  is null  
--                   THEN 2 else  1 
--             END        
-- ) group by 
--       YYYYMM,iqvia_pack_code,zk_product_id,prod_des_c,province_city,corp_cod,source,pack_flag,brand_flag,flag


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

-------------------------------------------------倒减OTHERS TA\ others az 并写入dm-----------------------------------------
insert overwrite table dm.dm_zk_retail_sales 
with all_ta_data as (
    SELECT    
        YYYYMM
        ,iqvia_pack_code
        ,zk_product_id
        ,prod_des_c
        ,province_city
        ,corp_cod
        ,sales_value
        ,sales_value_ly
        ,sales_unit
        ,sales_unit_ly
        ,counting_unit
        ,counting_unit_ly
        ,source
        ,pack_flag
        ,brand_flag
        ,FLAG
    from tmp.tmp_dm_zk_retail_sales_tmp2
    UNION ALL 
    SELECT 
        mkt.YYYYMM
        ,'OTHERS_AZ_TA'                                      as iqvia_pack_code 
        ,'OTHERS_AZ_TA'                                      as zk_product_id
        ,null                                           	   as prod_des_c
        ,'ROC'                                          	   as province_city
        ,mkt.corp_cod
        ,case when Total.sales_amount is not null then Total.sales_amount - mkt.sales_value           else 0 end as sales_value
        ,case when Total.sales_amount_ly is not null then Total.sales_amount_ly - mkt.sales_value_ly  else 0 end as sales_value_ly
        ,0,0,0,0
        ,null 
        ,0 pack_flag
        ,0 brand_flag
        ,'TA_AZ'
    FROM (
      SELECT 
          YYYYMM
          ,corp_cod
          ,sum(sales_value)			                            sales_value		
          ,sum(sales_value_ly)                              sales_value_ly
      from tmp.tmp_dm_zk_retail_sales_tmp2 A  
      where nvl(corp_cod,'') ='A5Z'
      group by YYYYMM,corp_cod
      ) mkt
    LEFT JOIN (SELECT yyyymm,sales_amount,sales_amount_ly FROM dws.dws_zk_retail_top_corp WHERE corp_desc='阿斯利康')  TOTAL
    ON mkt.YYYYMM = Total.YYYYMM
)
,Result as (
  ------------------------------- 所有pack数据 -------------------------------
SELECT    
	  YYYYMM
	  ,iqvia_pack_code
	  ,zk_product_id
	  ,prod_des_c
	  ,province_city
	  ,corp_cod
	  ,sales_value
	  ,sales_value_ly
	  ,sales_unit
	  ,sales_unit_ly
	  ,counting_unit
	  ,counting_unit_ly
	  ,source
	  ,pack_flag
	  ,brand_flag
    ,FLAG
from all_ta_data
  ----------------------------------OTHERS_TA 倒减数据-----------------------------------
union all 
SELECT 
	  mkt.YYYYMM
	  ,'OTHERS_TA'                                      	 as iqvia_pack_code 
	  ,'OTHERS_TA'                                      	 as zk_product_id
	  ,null                                           	 	 as prod_des_c
	  ,'ROC'                                          	 	 as province_city
	  ,'OTHERS_TA'										 	                   as corp_cod
	  ,Total.sales_amount - mkt.sales_value        	  	 	 as sales_value
	  ,Total.sales_amount_ly - mkt.sales_value_ly   	  	 as sales_value_ly
	  ,0,0,0,0
	  ,null 
	  ,0 pack_flag
	  ,0 brand_flag
    ,'OTHERS_TA'
FROM (
  SELECT 
      YYYYMM
      ,sum(sales_value)			    sales_value		
      ,sum(sales_value_ly)        sales_value_ly
  from all_ta_data A  
  group by YYYYMM
  ) mkt
LEFT JOIN (SELECT yyyymm,sales_amount,sales_amount_ly FROM dws.dws_zk_retail_top_corp WHERE rank1='Total')  TOTAL
ON mkt.YYYYMM = Total.YYYYMM
)
----------------------------------------数据写入DM------------------------------------------------
select   
    pack.YYYYMM
    ,pack.iqvia_pack_code      
    ,NVL(CASE WHEN PACK.province_city ='全国' then 'ROC' ELSE T2.geo_key END ,'ROC')                AUDIT_COD
    ,CASE WHEN PACK.province_city ='全国' then 'ROC' ELSE PACK.province_city END                    province_city
    ,corp_cod
    ,sum(pack.sales_unit) sales_unit
    ,sum(pack.sales_unit_ly) sales_unit_ly
    ,sum(pack.sales_value) sales_value
    ,sum(pack.sales_value_ly) sales_value_ly
    ,sum(pack.counting_unit) counting_unit
    ,sum(pack.counting_unit_ly) counting_unit_ly
    ,'Retail (Quarterly)'        DATA_SOURCE
	,source
  	,pack_flag
	  ,brand_flag
    ,FLAG
    ,from_utc_timestamp(current_timestamp(),'UTC+8') 					            AS etl_insert_dt
    ,from_utc_timestamp(current_timestamp(),'UTC+8') 					            AS etl_update_dt
from result pack
left join dm.dm_zk_retail_geo t2 
on pack.province_city = t2.province_city
group by     pack.YYYYMM
    ,pack.iqvia_pack_code      
    ,NVL(CASE WHEN PACK.province_city ='全国' then 'ROC' ELSE T2.geo_key END ,'ROC')                 
    ,CASE WHEN PACK.province_city ='全国' then 'ROC' ELSE PACK.province_city END 
    ,source
   	,pack_flag
	  ,brand_flag
    ,corp_cod
    ,FLAG


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

---------------------------------------------将倒减的pack 补充到ppack info 
insert overwrite table dws.dws_zk_retail_pack_property 
select 
*
  ,from_utc_timestamp(current_timestamp(),'UTC+8')	etl_insert_dt
	,from_utc_timestamp(current_timestamp(),'UTC+8')	etl_update_dt 
from tmp.tmp_zk_retail_pack_property 
union all 
------事实表中倒减出来的pack 需要补充到配置表中
  select  
			''              iqvia_lineno 
			,iqvia_pack_code  PACK_CODE
			,''               iqvia_prod_code 		 
			,''               iqvia_notes 
			,''               new_pack_flag 
			,''               Is_exists_chpa 
			,iqvia_pack_code   product_id 
			,''             prescription_nature 
			,''             medicine_type 
			,''             zk_medicine_tier1 
			,''             zk_medicine_tier2 
			,''             zk_medicine_tier3 
			,''             zk_medicine_tier4 
			,''             common_name 
			,''             dosage_form 
			,''             user_type 
			,''             category_name 
			,''             product_name 
			,''             brand 
			,''             zk_manu_des 
			,''             zk_corp_des
			,''             zk_pack_des 
			,''             counting_unit_a 
			,''             dosage_unit_a 
			,''             unit_a 
			,''             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_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 
			,'Others'             prod_des 
			,'Others'             prod_des_c 
			,''             tcm_desc 
			,''             vbp_desc 
			,''             unit 
			,''             counting_unit 
			,''             dosage_unit  
			,''             NRDL_ENTRY_DATE
			,'Non AZ Retail Related Market'           market 
			,''             bu
			,1              extend_market_ratio
			,CASE WHEN iqvia_pack_code LIKE '%AZ%' THEN 'Y' ELSE 'N' END	        is_az
			,from_utc_timestamp(current_timestamp(),'UTC+8')	etl_insert_dt
			,from_utc_timestamp(current_timestamp(),'UTC+8')	etl_update_dt
from (select distinct  iqvia_pack_code,corp_cod
 from DM.dm_zk_retail_sales  where iqvia_pack_code not in (select distinct iqvia_pack_code from tmp.tmp_zk_retail_pack_property))



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

-- dwd_gnd_tbl_corp_change
---------------------------------------对公司归属从新划分----------------------
insert overwrite table tmp.tmp_dm_zk_retail_pack_property 
select distinct 
   a.iqvia_lineno
  ,a.iqvia_pack_code 
  ,a.PROD_COD 
  ,a.iqvia_notes 
  ,a.new_pack_flag 
  ,a.is_exists_chpa 
  ,a.product_id 
  ,a.prescription_nature 
  ,a.medicine_type 
  ,a.zk_medicine_tier1 
  ,a.zk_medicine_tier2 
  ,a.zk_medicine_tier3 
  ,a.zk_medicine_tier4 
  ,a.common_name 
  ,a.dosage_form 
  ,a.user_type 
  ,a.category_name 
  ,a.product_name 
  ,a.brand_name 
  ,a.zk_manu_des 
  ,a.zk_corp_des 
  ,a.zk_pack_des 
  ,a.counting_unit_a 
  ,a.dosage_unit_a 
  ,a.unit_a 
  ,a.app1_cod 
  ,a.app1_des 
  ,a.app1_des_c 
  ,a.app2_cod 
  ,a.app2_des 
  ,a.app2_des_c 
  ,a.app3_cod 
  ,a.app3_des 
  ,a.app3_des_c 
  ,a.atc1_cod 
  ,a.atc1_des 
  ,a.atc1_des_c 
  ,a.atc2_cod 
  ,a.atc2_des 
  ,a.atc2_des_c 
  ,a.atc3_cod 
  ,a.atc3_des 
  ,a.atc3_des_c 
  ,a.atc4_cod 
  ,a.atc4_des 
  ,a.atc4_des_c 
  ,a.bio_desc 
  ,a.cmps_cod 
  ,a.cmps_des 
  ,a.cmps_des_c 
  ,COALESCE(c.corp_cod,b.corp_cod,a.corp_cod)  corp_cod
  ,COALESCE(c.corp_des,b.corp_des,a.corp_des)  corp_des
  ,COALESCE(d.corp_des_c,a.corp_des_c )        corp_des_c
  ,a.edl_desc 
  ,a.eth_otc_desc 
  ,a.gene_orig_desc 
  ,a.gqce_desc 
  ,COALESCE(c.manu_cod,b.manu_cod,a.manu_cod)  manu_cod 
  ,COALESCE(c.manu_des,b.manu_des,a.manu_des)  manu_des 
  ,COALESCE(e.manu_des_c,a.manu_des_c)         manu_des_c
  ,a.mnfl_cod 
  ,a.mnfl_des 
  ,a.nrdl_desc 
  ,a.pack_des 
  ,a.stgh_des 
  ,a.pack_lch 
  ,a.paed_desc 
  ,a.prod_des 
  ,a.prod_des_c 
  ,a.tcm_desc 
  ,a.vbp_desc 
  ,a.unit 
  ,a.counting_unit 
  ,a.dosage_unit 
  ,a.NRDL_ENTRY_DATE 
  -- ,CASE WHEN a.market=  'Antacid anti GI swelling agent+PPI Oral'  THEN  'Retail_GI Market'  ELSE a.MARKET  END MARKET
  ,A.MARKET
  ,a.bu 
  ,a.extend_market_ratio 
  ,a.is_az  
from dws.dws_zk_retail_pack_property a 
left join (select right(concat('000000000',prod_cod ),9) prod_cod,corp_cod,corp_des,  right(concat('000000',manu_cod ),6)    manu_cod,manu_des from dwd.dwd_gnd_tbl_corp_change where pack_cod is  null  ) b 
on right(concat('000000000',A.prod_cod ),9)= b.prod_cod
left join (select if( pack_cod REGEXP '^[0-9]',right(concat('000000000000',pack_cod),12),pack_cod) pack_cod,corp_cod,corp_des, right(concat('000000',manu_cod ),6)   manu_cod,manu_des from dwd.dwd_gnd_tbl_corp_change where pack_cod is  not null  ) c 
on a.iqvia_pack_code = c.pack_cod
left join (select distinct corp_cod,corp_des_c from dwd.dwd_gnd_ext_retail_pack_property) d 
on coalesce(C.corp_cod,B.corp_cod ) = d.corp_cod 
Left join (select distinct right(concat('000000',manu_cod ),6)  manu_cod,manu_des,manu_des_c  from dwd.dwd_gnd_ext_retail_pack_property) e 
On coalesce(C.manu_cod,B.manu_cod )  = e.manu_cod 

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

 ------------------------------获取pack  market 的对应关系----------------------------
--  drop table if exists dm.dm_zk_retail_pack_property;
-- create table if not exists dm.dm_zk_retail_pack_property
-- using delta as
INSERT OVERWRITE table dm.dm_zk_retail_pack_property
SELECT  MARKET_PACK_KEY
,iqvia_pack_code        iqvia_pack_code
,MAX(pack_des)           pack_des
,MAX(stgh_des)           stgh_des
,MAX(pack_lch)          pack_lch
,MAX(Family_Code)       Family_Code
,MAX(Family_Name)       Family_Name
,MAX(iqvia_prod_code)   iqvia_prod_code
,MAX(prod_des)          prod_des
,MAX(prod_des_c)        prod_des_c
,MAX(cmps_cod)          cmps_cod
,MAX(CMPS_DES)          CMPS_DES
,MAX(cmps_des_c)        cmps_des_c
,MAX(atc1_cod)          atc1_cod
,MAX(atc2_cod)          atc2_cod
,MAX(atc3_cod)          atc3_cod
,MAX(atc4_cod)          atc4_cod
,MAX(APP1_COD)          APP1_COD
,MAX(APP2_COD)          APP2_COD
,MAX(APP3_COD)          APP3_COD
,MAX(BIO_DESC)          BIO_DESC
,MAX(gene_orig_desc)    gene_orig_desc
,MAX(ETH_OTC_DESC)      ETH_OTC_DESC
,MAX(nrdl_desc)         nrdl_desc
,MAX(NRDL_ENTRY_DATE)   NRDL_ENTRY_DATE
,MAX(edl_desc)          edl_desc
,MAX(TCM_DESC)          TCM_DESC
,MAX(PAED_DESC)         PAED_DESC
,MAX(GQCE_DESC)         GQCE_DESC
,MAX(VBP_DESC)          VBP_DESC
,MAX(MANU_COD)          MANU_COD
,MAX(MANU_DES)          MANU_DES
,MAX(MANU_DES_C)        MANU_DES_C
,MAX(MNFL_COD)          MNFL_COD
,MAX(MNFL_DES)          MNFL_DES
,MAX(corp_cod)          corp_cod
,MAX(corp_des)          corp_des
,MAX(CORP_DES_C)        CORP_DES_C
,MAX(BrandType)         BrandType
,MAX(market)            market
,MAX(KEY_COMPETITOR)    KEY_COMPETITOR
,MAX(is_az)             is_az
,MAX(AZ_MAIN)           AZ_MAIN
,MAX(AZ_Related)        AZ_Related
,MAX(atc1_des)          atc1_des
,MAX(atc1_des_c)        atc1_des_c
,MAX(atc2_des)          atc2_des
,MAX(atc2_des_c)        atc2_des_c
,MAX(atc3_des)          atc3_des
,MAX(atc3_des_c)        atc3_des_c
,MAX(atc4_des)          atc4_des
,MAX(atc4_des_c)        atc4_des_c
,MAX(app1_des)          app1_des
,MAX(app1_des_c)        app1_des_c
,MAX(app2_des)          app2_des
,MAX(app2_des_c)        app2_des_c
,MAX(app3_des)          app3_des
,MAX(app3_des_c)        app3_des_c
,MAX(class)             class
,MAX(TA)                TA
FROM (
 select  distinct 
 CASE  when a.iqvia_pack_code  IN ('CVRM_AZ_OTHERS','CVRM_OTHERS') THEN 'CVRM_OTHER Market'
       WHEN a.iqvia_pack_code  IN ('CV_AZ_OTHERS','CV_OTHERS') THEN 'CV_OTHER Market' 
       WHEN A.iqvia_pack_code IN ('OTHERS_AZ_TA','OTHERS_TA') THEN 'OTHERS Market'
         ELSE a.market    
 END ||'_'||a.iqvia_pack_code   MARKET_PACK_KEY
,a.iqvia_pack_code
,c.pack_des
,c.stgh_des
,c.pack_lch
,'' Family_Code      
,'' Family_Name      
,c.iqvia_prod_code
,case when c.iqvia_pack_code like '%O%' THEN 'Others' else c.prod_des end  prod_des
,case when c.iqvia_pack_code like '%O%' THEN 'Others' else c.prod_des_c end  prod_des_c
,c.cmps_cod
,c.CMPS_DES
,c.cmps_des_c
,c.atc1_cod
,c.atc2_cod   
,c.atc3_cod   
,c.atc4_cod   
,c.APP1_COD
,c.APP2_COD
,c.APP3_COD
,c.BIO_DESC
,c.gene_orig_desc
,c.ETH_OTC_DESC
,c.nrdl_desc
,nvl(MOLE.NRDL_Entry_Date,'') 											      AS NRDL_ENTRY_DATE
,c.edl_desc
,c.TCM_DESC
,c.PAED_DESC
,c.GQCE_DESC
,c.VBP_DESC
,a.MANU_COD
,a.MANU_DES
,a.MANU_DES_C
,c.MNFL_COD
,c.MNFL_DES
,CASE WHEN A.iqvia_pack_code LIKE '%AZ%' THEN 'A5Z' else a.CORP_COD END              
								corp_cod 
,CASE WHEN A.iqvia_pack_code LIKE '%AZ%' THEN 'ASTRAZENECA'  else case when a.CORP_DES  like '% GROUP%' THEN replace(a.CORP_DES ,' GROUP','' ) ELSE a.CORP_DES  END  END   
					      corp_des 
,CASE WHEN A.iqvia_pack_code LIKE '%AZ%' THEN '阿斯利康制药集团'  else a.CORP_DES_C  END    
                CORP_DES_C
,'' BrandType
,CASE  when a.iqvia_pack_code  IN ('CVRM_AZ_OTHERS','CVRM_OTHERS') THEN 'CVRM_OTHER Market'
       WHEN a.iqvia_pack_code  IN ('CV_AZ_OTHERS','CV_OTHERS') THEN 'CV_OTHER Market' 
       WHEN A.iqvia_pack_code IN ('OTHERS_AZ_TA','OTHERS_TA') THEN 'OTHERS Market'
        ELSE a.market    
 END  market
,t5.KEY_COMPETITOR
,IF(a.corp_des_c in('阿斯利康制药集团') ,'Y','N')      is_az
,''  AZ_MAIN 
,''  AZ_Related
,c.atc1_des
,c.atc1_des_c
,c.atc2_des
,c.atc2_des_c
,c.atc3_des
,c.atc3_des_c
,c.atc4_des
,c.atc4_des_c
,c.app1_des
,c.app1_des_c
,c.app2_des
,c.app2_des_c
,c.app3_des
,c.app3_des_c
-- ,nvl(cla.class,'Others')   class 
,nvl(t3.class,'Others')   class 
,case when a.market ='Antacid anti GI swelling agent+PPI Oral' then 'GI' 
  when a.MARKET ='RD Market' then 'RD' 
  ELSE
  CASE when a.iqvia_pack_code IN ('CVRM_AZ_OTHERS','CVRM_OTHERS')  THEN 'CVRM'
       WHEN a.iqvia_pack_code  IN ('CV_AZ_OTHERS','CV_OTHERS')  THEN 'CV' 
         ELSE t2.ta END  
 END  TA   
from tmp.tmp_dm_zk_retail_pack_property a 
left join dwd.dwd_gnd_ext_retail_pack_property c on a.product_id = c.product_id
LEFT JOIN dwd.dwd_ims_td_pack_additional_attribute MOLE ON a.iqvia_pack_code = MOLE.Pack_Code 
left join dws.dws_ext_retail_td_ta t2   on a.market = t2.market
left join dwd.dwd_gnd_tblclass t3  on a.MARKET = case when t3.market is null then a.market else t3.market end  
 and nvl(a.iqvia_pack_code,'') = case when t3.pack_code is null   then  nvl(a.iqvia_pack_code,'') else if( t3.pack_code REGEXP '^[0-9]',right(concat('000000000000',t3.pack_code),12),t3.pack_code) end 
 and nvl(a.PROD_COD,'') = case when t3.product_code	  is null then nvl(a.PROD_COD       ,'')  else  right(concat('000000000', t3.product_code),9 )	  end
 and nvl(a.cmps_cod,'') = case when t3.molecule_code	  is null then nvl(a.cmps_cod ,'') else right(concat('000000', t3.molecule_code	 ),6 )  end 
 and nvl(a.corp_cod,'') = case when t3.Corporation_code  is null then nvl(a.corp_cod       ,'') else  t3.Corporation_code  end
  and nvl(a.manu_cod,'')=case when t3.Manufacturer_Code is null then nvl(a.manu_cod,'') else t3.Manufacturer_Code end  
  and nvl(c.atc1_cod,'') = case when t3.ATC1_Code is null then  nvl(c.atc1_cod       ,'') else  t3.ATC1_Code end 
 and nvl(c.ATC2_COD,'') = case when t3.ATC2_Code  is null then  nvl(c.ATC2_COD       ,'') else  t3.ATC2_Code  end  
 and nvl(c.ATC3_COD,'') = case when t3.ATC3_Code is null then  nvl(c.ATC3_COD       ,'') else  t3.ATC3_Code end
 and nvl(c.ATC4_COD,'') = case when t3.ATC4_Code is null then  nvl(c.ATC4_COD       ,'') else  t3.ATC4_Code end
 and nvl(c.app1_cod,'') = case when t3.NFC1_Code is null then  nvl(c.app1_cod       ,'') else  t3.NFC1_Code end
 and nvl(c.APP2_COD,'') = case when t3.NFC2_Code is null then  nvl(c.APP2_COD       ,'') else  t3.NFC2_Code end
 and nvl(c.APP3_COD,'') = case when t3.NFC3_Code is null then  nvl(c.APP3_COD       ,'') else  t3.NFC3_Code end
 and nvl(c.stgh_des,'') = case when t3.Strength  is null then  nvl(c.stgh_des       ,'') else  t3.Strength  end
left join (select distinct MARKET,PACK_COD,KEY_COMPETITOR from dm.dm_ims_td_pack_property) t5
on a.market = t5.MARKET
and a.iqvia_pack_code = t5.PACK_COD
)

group by  MARKET_PACK_KEY
,iqvia_pack_code


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

------------获取pack  market  ta 对应关系表-------------
insert overwrite table dm.dm_zk_retail_market_property
select distinct 
iqvia_pack_code,market,ta
from dm.dm_zk_retail_pack_property  

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

------------rc 拆分逻辑处理-----------
insert overwrite table dws.dws_ext_rc_ratio 
select 
    a.sub_bu
    ,a.mkt
    ,a.region_code
    ,nvl(b.PROVINCE_CODE,c.PROVINCE_CODE )   PROVINCE_CODE
    ,nvl(b.AUDIT_COD,c.PROVINCE_CODE)        city_code
    ,a.ratio
    ,From_utc_timestamp(CURRENT_TIMESTAMP(), 'UTC+8') etl_insert_dt
    ,From_utc_timestamp(CURRENT_TIMESTAMP(), 'UTC+8') etl_update_dt
 from dwd.dwd_gnd_ext_rc_ratio a 
 left join  dm.dm_ims_td_geo b 
 on a.city = b.CITY_C
  left join (select province_code,province_name from  dm.dm_td_geography group by province_name,province_code) c 
 on a.city = c.province_name

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

----------------rc拆分逻辑处理到dm
insert overwrite table dm.dm_ext_rc_ratio 
select 
    sub_bu
    ,mkt
    ,region_code
    ,PROVINCE_CODE
    ,city_code
    ,ratio
    ,From_utc_timestamp(CURRENT_TIMESTAMP(), 'UTC+8') etl_insert_dt
    ,From_utc_timestamp(CURRENT_TIMESTAMP(), 'UTC+8') etl_update_dt
 from dws.dws_ext_rc_ratio 

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

-- MAGIC %md
-- MAGIC ## DTP维度表

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

-- -----dtp 维度表处理------------------
-- --第一步：
-- --先处理 Extend_Market 和 NOT_IN_FLAG 都为空的数据，代表有明确定义得市场
insert overwrite table tmp.tmp_zk_retail_dtp_market 
select distinct 
t1.region_type
,t1.higher_level_region
,t2.market
,if( t1.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',t1.iqvia_pack_code),12),t1.iqvia_pack_code) iqvia_pack_code --right(concat('0000000', t1.iqvia_pack_code ),7)
,right(concat('000000000', t1.iqvia_prod_code ),9) iqvia_prod_code --right(concat('00000', t1.iqvia_prod_code ),5)
,t1.iqvia_notes 
,t1.new_pack_flag 
,t1.is_exists_chpa 
,t1.prescription_nature 
,t1.common_name 
,t1.dosage_form 
,t1.category_name 
,t1.product_name 
,t1.brand_name 
,t1.zk_corp_des 
,t1.zk_pack_des 
,t1.counting_unit_a 
,t1.app1_cod 
,t1.app1_des 
,t1.app1_des_c 
,t1.app2_cod 
,t1.app2_des 
,t1.app2_des_c 
,t1.app3_cod 
,t1.app3_des 
,t1.app3_des_c 
,t1.atc1_cod 
,t1.atc1_des 
,t1.atc1_des_c 
,t1.atc2_cod 
,t1.atc2_des 
,t1.atc2_des_c 
,t1.atc3_cod 
,t1.atc3_des 
,t1.atc3_des_c 
,t1.atc4_cod 
,t1.atc4_des 
,t1.atc4_des_c 
,t1.bio_desc 
, right(concat('000000', t1.CMPS_COD ),6 )  cmps_cod 
,t1.cmps_des 
,t1.cmps_des_c 
,t1.corp_cod 
,t1.corp_des 
,t1.corp_des_c 
,t1.edl_desc 
,t1.eth_otc_desc 
,t1.gene_orig_desc 
,t1.gqce_desc 
,t1.manu_cod 
,t1.manu_des 
,t1.manu_des_c 
,t1.mnfl_cod 
,t1.mnfl_des 
,t1.nrdl_desc 
,t1.pack_des 
,t1.stgh_des 
,t1.pack_lch 
,t1.paed_desc 
,t1.prod_des 
,t1.prod_des_c 
,t1.tcm_desc 
,t1.vbp_desc 
,t1.unit 
,t1.counting_unit 
,t1.dosage_unit  
,nvl(MOLE.NRDL_Entry_Date,'') 											  AS NRDL_ENTRY_DATE
,t2.bu,CASE WHEN t2.extend_market_ratio IS NULL THEN '1' ELSE t2.extend_market_ratio END AS Market_Ratio
from dwd.dwd_gnd_ext_dtp_pack_property t1
LEFT JOIN dwd.dwd_ims_td_pack_additional_attribute MOLE ON t1.iqvia_pack_code = MOLE.Pack_Code 
left join (select * from dwd.dwd_gnd_dtp_tblmarket WHERE Extend_Market IS NULL AND ( NOT_IN_FLAG IS NULL or  NOT_IN_FLAG = '1' )) t2
-- on t1.cmps_cod=right(concat('000000', Molecule_Code),6 )
on  t1.ATC1_COD = case when t2.ATC1_Code is null then t1.ATC1_COD else t2.ATC1_Code end
and t1.ATC2_COD = case when t2.ATC2_Code is null then t1.ATC2_COD else t2.ATC2_Code end
and t1.ATC3_COD = case when t2.ATC3_Code is null then t1.ATC3_COD else t2.ATC3_Code end
and t1.ATC4_COD = case when t2.ATC4_Code is null then t1.ATC4_COD else t2.ATC4_Code end
and t1.APP1_COD = case when t2.NFC1_Code is null then t1.APP1_COD else t2.NFC1_Code end
and t1.APP2_COD = case when t2.NFC2_Code is null then t1.APP2_COD else t2.NFC2_Code end
and t1.APP3_COD = case when t2.NFC3_Code is null then t1.APP3_COD else t2.NFC3_Code end
and t1.CORP_COD = case when t2.corporation_code is null then t1.CORP_COD else t2.corporation_code end
and t1.MANU_COD = case when t2.Manufacturer_Code is null then t1.MANU_COD else t2.Manufacturer_Code end
and right(concat('000000000', t1.iqvia_prod_code ),9)  = case when t2.Product_Code is null then right(concat('000000000', t1.iqvia_prod_code ),9)  else  right(concat('000000000', t2.Product_Code ),9)  end
and if( t1.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',t1.iqvia_pack_code),12),t1.iqvia_pack_code)
  = case when t2.Pack_Code is null then if( t1.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',t1.iqvia_pack_code),12),t1.iqvia_pack_code)
    else if( t2.Pack_Code REGEXP '^[0-9]',right(concat('000000000000',t2.Pack_Code),12),t2.Pack_Code) end
and t1.STGH_DES = case when t2.Strength is null then t1.STGH_DES else t2.Strength end
and   right(concat('000000', t1.CMPS_COD ),6 )   = case when t2.Molecule_Code is null then  right(concat('000000', t1.CMPS_COD ),6 ) else right(concat('000000', Molecule_Code),6 ) end
where t2.market is not null
;


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

insert overwrite table tmp.tmp_zk_retail_dtp_market_del 
select distinct 
 t1.region_type
,t1.higher_level_region
,t2.market
,if( t1.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',t1.iqvia_pack_code),12),t1.iqvia_pack_code) iqvia_pack_code 
, right(concat('000000000', t1.iqvia_prod_code ),9)     iqvia_prod_code 	 
,t1.iqvia_notes 
,t1.new_pack_flag 
,t1.is_exists_chpa 
,t1.prescription_nature 
,t1.common_name 
,t1.dosage_form 
,t1.category_name 
,t1.product_name 
,t1.brand_name 
,t1.zk_corp_des 
,t1.zk_pack_des 
,t1.counting_unit_a 
,t1.app1_cod 
,t1.app1_des 
,t1.app1_des_c 
,t1.app2_cod 
,t1.app2_des 
,t1.app2_des_c 
,t1.app3_cod 
,t1.app3_des 
,t1.app3_des_c 
,t1.atc1_cod 
,t1.atc1_des 
,t1.atc1_des_c 
,t1.atc2_cod 
,t1.atc2_des 
,t1.atc2_des_c 
,t1.atc3_cod 
,t1.atc3_des 
,t1.atc3_des_c 
,t1.atc4_cod 
,t1.atc4_des 
,t1.atc4_des_c 
,t1.bio_desc 
, right(concat('000000', t1.CMPS_COD ),6 )   cmps_cod 
,t1.cmps_des 
,t1.cmps_des_c 
,t1.corp_cod 
,t1.corp_des 
,t1.corp_des_c 
,t1.edl_desc 
,t1.eth_otc_desc 
,t1.gene_orig_desc 
,t1.gqce_desc 
,t1.manu_cod 
,t1.manu_des 
,t1.manu_des_c 
,t1.mnfl_cod 
,t1.mnfl_des 
,t1.nrdl_desc 
,t1.pack_des 
,t1.stgh_des 
,t1.pack_lch 
,t1.paed_desc 
,t1.prod_des 
,t1.prod_des_c 
,t1.tcm_desc 
,t1.vbp_desc 
,t1.unit 
,t1.counting_unit 
,t1.dosage_unit  
,nvl(MOLE.NRDL_Entry_Date,'') 											  AS NRDL_ENTRY_DATE
,t2.bu,CASE WHEN t2.extend_market_ratio IS NULL THEN '1' ELSE t2.extend_market_ratio END AS Market_Ratio
	from dwd.dwd_gnd_ext_dtp_pack_property t1
LEFT JOIN dwd.dwd_ims_td_pack_additional_attribute MOLE ON t1.iqvia_pack_code = MOLE.Pack_Code 
left join (select * from dwd.dwd_gnd_dtp_tblmarket WHERE Extend_Market IS NULL AND  NOT_IN_FLAG = '0' ) t2
-- on t1.cmps_cod=right(concat('000000', Molecule_Code),6 )
	on  t1.ATC1_COD = case when t2.ATC1_Code is null then t1.ATC1_COD else t2.ATC1_Code end
	and t1.ATC2_COD = case when t2.ATC2_Code is null then t1.ATC2_COD else t2.ATC2_Code end
	and t1.ATC3_COD = case when t2.ATC3_Code is null then t1.ATC3_COD else t2.ATC3_Code end
	and t1.ATC4_COD = case when t2.ATC4_Code is null then t1.ATC4_COD else t2.ATC4_Code end
	and t1.APP1_COD = case when t2.NFC1_Code is null then t1.APP1_COD else t2.NFC1_Code end
	and t1.APP2_COD = case when t2.NFC2_Code is null then t1.APP2_COD else t2.NFC2_Code end
	and t1.APP3_COD = case when t2.NFC3_Code is null then t1.APP3_COD else t2.NFC3_Code end
	and t1.CORP_COD = case when t2.corporation_code is null then t1.CORP_COD else t2.corporation_code end
	and t1.MANU_COD = case when t2.Manufacturer_Code is null then t1.MANU_COD else t2.Manufacturer_Code end
and right(concat('000000000', t1.iqvia_prod_code ),9)  = case when t2.Product_Code is null then right(concat('000000000', t1.iqvia_prod_code ),9)  else  right(concat('000000000', t2.Product_Code ),9)  end
and if( t1.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',t1.iqvia_pack_code),12),t1.iqvia_pack_code)
	= case when t2.Pack_Code is null then if( t1.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',t1.iqvia_pack_code),12),t1.iqvia_pack_code)
	else if( t2.Pack_Code REGEXP '^[0-9]',right(concat('000000000000',t2.Pack_Code),12),t2.Pack_Code) end
	and t1.STGH_DES = case when t2.Strength is null then t1.STGH_DES else t2.Strength end
	and   right(concat('000000', t1.CMPS_COD ),6 )   = case when t2.Molecule_Code is null then  right(concat('000000', t1.CMPS_COD ),6 ) else right(concat('000000', Molecule_Code),6 ) end
	where t2.market is not null

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


-- --！！！注意此段是反选的规则，故执行的是从已经定义好的TempMKT中删除数据
-- --需要看TempMKT的字段结构，没有值的字段，在Value里面放个空值
MERGE INTO tmp.tmp_zk_retail_dtp_market AS t1
USING tmp.tmp_zk_retail_dtp_market_del AS t2
	 on  t1.ATC1_COD = t2.ATC1_COD
     and t1.ATC2_COD = t2.ATC2_COD
     and t1.ATC3_COD = t2.ATC3_COD
     and t1.ATC4_COD = t2.ATC4_COD
     and t1.APP1_COD = t2.APP1_COD
     and t1.APP2_COD = t2.APP2_COD
     and t1.APP3_COD = t2.APP3_COD
     and t1.CORP_COD = t2.CORP_COD
     and t1.MANU_COD = t2.MANU_COD
     and t1.iqvia_prod_code = t2.iqvia_prod_code
     and t1.iqvia_pack_code = t2.iqvia_pack_code
     and t1.STGH_DES = t2.STGH_DES
     and t1.CMPS_COD = t2.CMPS_COD
     and t1.market = t2.market 
WHEN MATCHED THEN DELETE 
;

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

------------------------将产品对应的公司重新归属
insert overwrite table tmp.tmp_zk_retail_dtp_market_corp 
select
A.region_type
,A.higher_level_region
,A.market
,A.iqvia_pack_code
,A.iqvia_prod_code
,A.iqvia_notes
,A.new_pack_flag
,A.is_exists_chpa
,A.prescription_nature
,A.common_name
,A.dosage_form
,A.category_name
,A.product_name
,A.brand_name
,A.zk_corp_des
,A.zk_pack_des
,A.counting_unit_a
,A.app1_cod
,A.app1_des
,A.app1_des_c
,A.app2_cod
,A.app2_des
,A.app2_des_c
,A.app3_cod
,A.app3_des
,A.app3_des_c
,A.atc1_cod
,A.atc1_des
,A.atc1_des_c
,A.atc2_cod
,A.atc2_des
,A.atc2_des_c
,A.atc3_cod
,A.atc3_des
,A.atc3_des_c
,A.atc4_cod
,A.atc4_des
,A.atc4_des_c
,A.bio_desc
,A.cmps_cod
,A.cmps_des
,A.cmps_des_c
  ,COALESCE(c.corp_cod,b.corp_cod,a.corp_cod)  corp_cod
  ,COALESCE(c.corp_des,b.corp_des,a.corp_des)  corp_des
  ,COALESCE(d.corp_des_c,a.corp_des_c )        corp_des_c
,A.edl_desc
,A.eth_otc_desc
,A.gene_orig_desc
,A.gqce_desc
  ,COALESCE(c.manu_cod,b.manu_cod,a.manu_cod)  manu_cod 
  ,COALESCE(c.manu_des,b.manu_des,a.manu_des)  manu_des 
  ,COALESCE(e.manu_des_c,a.manu_des_c)         manu_des_c
,A.mnfl_cod
,A.mnfl_des
,A.nrdl_desc
,A.pack_des
,A.stgh_des
,A.pack_lch
,A.paed_desc
,A.prod_des
,A.prod_des_c
,A.tcm_desc
,A.vbp_desc
,A.unit
,A.counting_unit
,A.dosage_unit
,A.NRDL_ENTRY_DATE
,A.bu
,A.Market_Ratio
from tmp.tmp_zk_retail_dtp_market a 
left join (select DISTINCT right(concat('000000000',prod_cod ),9) prod_cod,corp_cod,corp_des, right(concat('000000',manu_cod ),6) manu_cod,manu_des from dwd.dwd_gnd_tbl_corp_change where pack_cod is  null  ) b 
on right(concat('000000000',A.iqvia_prod_code ),9)= b.prod_cod
left join (select DISTINCT  if( pack_cod REGEXP '^[0-9]',right(concat('000000000000',pack_cod),12),pack_cod) pack_cod ,corp_cod,corp_des, right(concat('000000',manu_cod ),6) manu_cod,manu_des from dwd.dwd_gnd_tbl_corp_change where pack_cod is  not null  ) c 
on a.iqvia_pack_code = c.pack_cod
left join (select distinct corp_cod,corp_des_c from dwd.dwd_gnd_ext_dtp_pack_property) d 
on coalesce(C.corp_cod,B.corp_cod ) = d.corp_cod 
Left join (select distinct  right(concat('000000',manu_cod ),6) manu_cod,manu_des,manu_des_c  from dwd.dwd_gnd_ext_dtp_pack_property) e 
On coalesce(C.manu_cod,B.manu_cod )  = e.manu_cod 

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

-- MAGIC %md
-- MAGIC ## DTP 数据

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

/*
修改日期：20250515
修改人：chenwu
修改内容：新增 Anti-HER2 mBC 直取逻辑
*/
create or replace temporary view dtp_rawdata_Anti
as
with all_data as (
  select 
    left(dtp.year, 4) year,
    concat(left(dtp.year, 4), 'Q', cal.quarter) yq,
    cast(dtp.`time` as int) yyyymm,
    pack.iqvia_pack_code,
    pack.corp_cod,
    geo.geo_key,
    cast(dtp.counting_unit as double) count_unit,
    cast(dtp.average_price as double) average_price,
    cast(dtp.sales_amount as double) sales_value,
    cast(dtp.sales_volume as double) sales_unit,
    cast(dtp.counting_units_obversion as double) counting_units_obversion,
    cast(dtp.sales_volume * (pack.counting_unit / pack.unit) as double) counting_unit
    ,target_points market --使用dtp pack中的靶向点 字段来判断是否是需要拆分的市场
    ,pack.product_name brand_name
  from dwd.dwd_gnd_ext_retail_dtp_datasource dtp
  left join (
      select
            DISTINCT brand_name,
            iqvia_pack_code,
            corp_cod,
            zk_pack_des,
            product_name,
            counting_unit,
            unit
      from
            tmp.tmp_zk_retail_dtp_market_corp
  ) pack on dtp.pack_des = pack.zk_pack_des
    AND nvl(dtp.product_name, '') = nvl(pack.product_name, '') 
    and dtp.brand_name = pack.brand_name
  left join dm.dm_zk_retail_geo geo on dtp.region = geo.province_name
  left join ( select distinct yyyymm,quarter from dm.dm_td_calendar ) cal on cal.yyyymm = dtp.`time`
  where dtp.target_points = 'Anti-HER2 mBC'
)
  
--全国的数据
,quanguo_data as (
  select
    *
  from all_data
  where geo_key = 'CHT'
)
--省份数据
,province_data as (
  select
    *
  from all_data
  where geo_key <> 'CHT'
)
,province_sum_data as (
  select 
    A.year
    ,A.yq
    ,A.yyyymm
    ,A.iqvia_pack_code
    ,A.corp_cod
    ,max(A.count_unit) count_unit
    ,max(A.average_price) average_price
    ,sum(A.sales_value) as sales_value
    ,sum(A.sales_unit) as sales_unit
    ,sum(A.counting_units_obversion) as counting_units_obversion
    ,sum(A.counting_unit) as counting_unit
  from province_data A
  group by 1,2,3,4,5
)
-- select * from province_data
--倒减ROC部分 = 全国 - 省份
,roc_data as (
  select 
  A.year 
  ,A.yq
  ,A.yyyymm
  ,A.iqvia_pack_code
  ,A.corp_cod
  ,'ROC' geo_key
  ,A.count_unit
  ,if(B.average_price is not null, 2*A.average_price- B.average_price,A.average_price) as average_price
  ,A.sales_value - B.sales_value as sales_value
  ,A.sales_unit - B.sales_unit as sales_unit
  ,A.counting_units_obversion -B.counting_units_obversion AS counting_units_obversion
  ,A.counting_unit - B.counting_unit AS counting_unit
  from quanguo_data A 
  left join province_sum_data B on A.yyyymm = B.yyyymm and A.iqvia_pack_code = B.iqvia_pack_code and A.corp_cod = B.corp_cod 
)

select 
A.year
,A.yq
,A.yyyymm
,A.iqvia_pack_code
,A.corp_cod
,A.geo_key
,A.count_unit
,A.average_price
,A.sales_value as sales_value
,A.sales_unit as sales_unit
,A.counting_units_obversion as counting_units_obversion
,A.counting_unit as counting_unit
,1  pack_flag
,1  brand_flag
from province_data A
union all
select 
A.year
,A.yq
,A.yyyymm
,A.iqvia_pack_code
,A.corp_cod
,A.geo_key
,A.count_unit
,A.average_price
,A.sales_value as sales_value
,A.sales_unit as sales_unit
,A.counting_units_obversion as counting_units_obversion
,A.counting_unit as counting_unit
,1   pack_flag
,1  brand_flag
from roc_data A

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

----------------------------------------------------------
/*
修改时间：20250311
修改人：chenwu
修改内容：新增手动拆分DTP逻辑修改
*/
----------------------------------------------------------
create or replace table tmp.tmp_zk_retail_brandTotal_dtp
with dtp_egfr_tki_market(--brand中 EGFR TKI Market 的量
  select 
  YYYYMM
  ,market
  ,province_city
  ,cast(sales_value  * 1000000  as decimal(30,10)) 									    AS sales_val
  ,cast(sales_volume * 1000000  as decimal(30,10))                      AS sales_vol
  ,'EGFR TKI Market' marketmapping
  ,'--' cmps_des_c
  ,'--' prod_des_c
  from dwd.dwd_gnd_ext_dtp_zk_brand
  where type = '品类' --and TA = 'ONC'
  and ranked_by = 'value'
  and market = 'EGFR TKI'
)

,dtp_brand(--brand数据中 brand的量
  select 
  YYYYMM
  ,market
  ,province_city
  ,cast(sales_value  * 1000000  as decimal(30,10)) 									    AS sales_val
  ,cast(sales_volume * 1000000  as decimal(30,10))                      AS sales_vol
  ,'EGFR TKI Market' marketmapping
  ,case when zk_brand_category = '泰瑞沙' then '奥希替尼'
        when zk_brand_category = '凯美纳' then '埃克替尼'
        when zk_brand_category = '阿美乐' then '阿美替尼'
        when zk_brand_category = '艾弗沙' then '伏美替尼'
        else 'Others_cmps_EGFR TKI Market' end
    as cmps_des_c
  ,zk_brand_category as prod_des_c
  
  from dwd.dwd_gnd_ext_dtp_zk_brand
  where type = '品牌' 
  and ranked_by = 'value'
  and zk_brand_category in ('泰瑞沙','凯美纳','阿美乐','艾弗沙')
)

,other_dtp_cmps(--其他的
  select 
  A.YYYYMM
  ,A.market
  ,A.province_city
  ,A.sales_val - B.sales_val as sales_val
  ,A.sales_vol - B.sales_vol as sales_vol
  ,'other_EGFR TKI Market' marketmapping
  ,concat('Others_cmps_',A.market ) as cmps_des_c
  ,'Others_brand' as prod_des_c
from dtp_egfr_tki_market A
left join (
  select 
    YYYYMM
    ,province_city
    ,sum(sales_val) as sales_val
    ,sum(sales_vol) as sales_vol 
  from dtp_brand group by 1,2
 ) B 
  ON A.YYYYMM = B.YYYYMM and A.province_city=B.province_city 
)

--合并所有
,dtp_all as (
  select * from dtp_brand
  union all
  select * from other_dtp_cmps
)

--计算ROC部分
,dtp_roc(
  select 
    A.YYYYMM
    ,A.market
    ,'Other Low Tiers'  province_city
    ,A.marketmapping
    ,A.cmps_des_c
    ,A.prod_des_c
    ,A.sales_val-B.sales_val as sales_val
    ,A.sales_vol-B.sales_vol as sales_vol 
  from dtp_all A
  left join (
    select 
      YYYYMM
      ,market
      ,marketmapping
      ,cmps_des_c
      ,prod_des_c
      ,sum(sales_val) sales_val
      ,sum(sales_vol) sales_vol
    from dtp_all
    where province_city<>'全国'
    group by YYYYMM, market, marketmapping, cmps_des_c, prod_des_c
    ) B on A.YYYYMM=B.YYYYMM 
          and A.market=A.market 
          and A.marketmapping=B.marketmapping 
          and A.cmps_des_c=B.cmps_des_c 
          and A.prod_des_c=B.prod_des_c
  where A.province_city = '全国'

  union all
  --非全国部分
  select 
    YYYYMM
    ,market
    ,province_city
    ,marketmapping
    ,cmps_des_c
    ,prod_des_c
    ,sales_val
    ,sales_vol
  from dtp_all
  where province_city <> '全国'
)
,default_rate as (select cast(1.0/count( distinct province_city ) as decimal(38,15)) as d_rate  from dtp_roc )
,geo as (select distinct geo_Key ,province_name from dm.dm_zk_retail_geo)
select 
  YYYYMM
  ,market
  ,geo.geo_Key 
  -- ,province_city
  ,marketmapping
  ,cmps_des_c
  ,prod_des_c
  ,sales_val
  ,sales_vol
  ,nvl(cast(sales_val as decimal(38,15))  / sum(sales_val) over(PARTITION BY YYYYMM,market,marketmapping,cmps_des_c,prod_des_c),default_rate.d_rate ) valRate
  ,nvl(cast(sales_vol as decimal(38,15))  / sum(sales_vol) over(PARTITION BY YYYYMM,market,marketmapping,cmps_des_c,prod_des_c),default_rate.d_rate ) volRate
from dtp_roc 
left join default_rate --取默认比率
left join geo on geo.province_name = dtp_roc.province_city


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

---new dtp pack data
/*
修改人：chenwu
修改时间：20250415
修改内容：新增市场时，可能出现 一个pack对应多个市场的情况，所以直接用 tmp.tmp_zk_retail_dtp_market_corp 取pack和market的对应关系会导致数据重复，
故使用dtp pack中的[靶向点]字段来判断是否是需要拆分的市场。
*/
create or replace temporary view new_dtp_pack_data
as
with dtp_pack_old (--原有逻辑
select
     left(dtp.year, 4) year,
     concat(left(dtp.year, 4), 'Q', cal.quarter) yq,
     cast(dtp.`time` as int) yyyymm,
     pack.iqvia_pack_code,
     pack.corp_cod,
     geo.geo_key,
     cast(dtp.counting_unit as double) count_unit,
     cast(dtp.average_price as double) average_price,
     cast(dtp.sales_amount as double) sales_value,
     cast(dtp.sales_volume as double) sales_unit,
     cast(dtp.counting_units_obversion as double) counting_units_obversion,
     cast(dtp.sales_volume * (pack.counting_unit / pack.unit) as double) counting_unit
     ,target_points market --使用dtp pack中的靶向点 字段来判断是否是需要拆分的市场
     ,pack.product_name brand_name
from
     dwd.dwd_gnd_ext_retail_dtp_datasource dtp
     left join (
          select
               DISTINCT brand_name,
               iqvia_pack_code,
               corp_cod,
               zk_pack_des,
               product_name,
               counting_unit,
               unit
          from
               tmp.tmp_zk_retail_dtp_market_corp
     ) pack on dtp.pack_des = pack.zk_pack_des
     AND nvl(dtp.product_name, '') = nvl(pack.product_name, '') 
     and dtp.brand_name = pack.brand_name
     left join dm.dm_zk_retail_geo geo on dtp.region = geo.province_name
     left join ( select distinct yyyymm,quarter from dm.dm_td_calendar ) cal on cal.yyyymm = dtp.`time`
     where dtp.target_points <> 'Anti-HER2 mBC'
)

select 
A.year
,A.yq
,A.yyyymm
,A.iqvia_pack_code
,A.corp_cod
,Case when B.YYYYMM IS NULL and C.YYYYMM IS NULL then a.geo_key
      when B.YYYYMM IS NULL THEN C.geo_key ELSE B.geo_key end as geo_key
,A.count_unit
,A.average_price
,Case when B.YYYYMM IS NULL and C.YYYYMM IS NULL then 1
      when B.YYYYMM IS NULL THEN C.valRate ELSE B.valRate end * A.sales_value as sales_value
,Case when B.YYYYMM IS NULL and C.YYYYMM IS NULL then 1
      when B.YYYYMM IS NULL THEN C.volRate ELSE B.volRate end * A.sales_unit as sales_unit
,Case when B.YYYYMM IS NULL and C.YYYYMM IS NULL then 1
      when B.YYYYMM IS NULL THEN C.volRate ELSE B.volRate end * A.counting_units_obversion as counting_units_obversion
,Case when B.YYYYMM IS NULL and C.YYYYMM IS NULL then 1
      when B.YYYYMM IS NULL THEN C.volRate ELSE B.volRate end * A.counting_unit as counting_unit
-- ,a.market 
,1   pack_flag
,Case when B.YYYYMM IS NULL and C.YYYYMM IS NULL then 1
      when B.YYYYMM IS NULL THEN 2 else 1 end as brand_flag --如果 能够匹配上 brand的比例则 1,否则2
from dtp_pack_old A
left join tmp.tmp_zk_retail_brandTotal_dtp B
  on A.brand_name = B.prod_des_c and A.yyyymm = B.yyyymm
left join tmp.tmp_zk_retail_brandTotal_dtp C
  on B.YYYYMM IS NULL AND C.prod_des_c = 'Others_brand' and A.yyyymm = C.yyyymm
where a.market = 'EGFR TKI'
--not EGFR TKI Market
union all
select 
A.year
,A.yq
,A.yyyymm
,A.iqvia_pack_code
,A.corp_cod
,A.geo_key
,A.count_unit
,A.average_price
,A.sales_value as sales_value
,A.sales_unit as sales_unit
,A.counting_units_obversion as counting_units_obversion
,A.counting_unit as counting_unit
-- ,a.market
,1   pack_flag
,2  brand_flag
from dtp_pack_old A
where a.market <> 'EGFR TKI' 

--有省份数据的pack数据
UNION ALL
SELECT 
A.year
,A.yq
,A.yyyymm
,A.iqvia_pack_code
,A.corp_cod
,A.geo_key
,A.count_unit
,A.average_price
,A.sales_value 
,A.sales_unit 
,A.counting_units_obversion 
,A.counting_unit
,1   pack_flag
,1  brand_flag
FROM dtp_rawdata_Anti A

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

insert overwrite table dws.dws_retail_dtp_sales 
with pack_data as (
  --- 获取 dtp 底表数据 并进行格式转换----
  select * from new_dtp_pack_data
)
,AZ AS (
SELECT 
B.YQ
,A.yyyymm
,A.CORP_COD
,'DTP_AZ_OTHERS'   iqvia_pack_code
,A.sales_amount -AZ_SALE sales_value
FROM (select  cast(top_corp.sales_quarter as int ) yyyymm  
              ,top_corp.corp_name,top_corp.sales_amount *1000000.0  sales_amount ,pack.CORP_COD
      from dwd.dwd_gnd_ext_retail_dtp_top_copd top_corp 
      left join (select distinct  ZK_Corp_C,CORP_COD from  dwd.dwd_inc_gnd_retail_b2c_label_total ) pack on replace(top_corp.corp_name,'-','')=pack.ZK_Corp_C
      where pack.CORP_COD='A5Z') A 
 left join (SELECT  YQ,yyyymm , CORP_COD ,SUM(sales_value) AZ_SALE FROM pack_data WHERE corp_cod ='A5Z' group by corp_cod,yyyymm,YQ) B  ON A.CORP_COD=B.CORP_COD AND A.yyyymm=B.yyyymm
)
,total_dtp as (
  --获取total 数据--
SELECT 
  dtp_name
  ,sales_quarter
  ,sales_amount*1000000.0 as sales_amount 
FROM  dwd.dwd_gnd_ext_retail_dtp_overall_market
)
select 
  year
  ,yq
  ,yyyymm
  ,iqvia_pack_code
  ,geo_key
  ,count_unit
  ,average_price
  ,sales_value
  ,sales_unit
  ,counting_units_obversion
  ,counting_unit
  ,pack_flag
  ,brand_flag
from pack_data
union all 
select 
    year
    ,yq
    ,yyyymm
    ,'DTP_OTHERS'    iqvia_pack_code
    ,'CHT' geo_key --赋给全国
    ,0                                                 counting_unit
    ,0                                                 average_price
    ,total_dtp.sales_amount - pack_data.sales_value    sales_value 
    ,0                                                 sales_volume
    ,0                                                 counting_units_obversion
    ,0 
    ,0
    ,0
from (
--修改人：chenwu，修改时间：20250415，修改内容：dtp_overall_market中没有geo信息，故给全国，去除geo_key的group by信息,并修改关联键
SELECT year, yq,yyyymm,SUM(sales_value) sales_value 
FROM (  select year,yq,yyyymm,geo_key,sum(sales_value) sales_value from pack_data GROUP BY 1,2,3,4
  UNION ALL 
  SELECT left(YYYYMM,4 ),YQ,YYYYMM,'CHT' ,sales_value  FROM AZ  )
GROUP BY  yq,yyyymm ,year
  )  pack_data
left join total_dtp on pack_data.yyyymm=total_dtp.sales_quarter
UNION ALL 
SELECT 
left(yyyymm,4 ) ,YQ,yyyymm 
  ,iqvia_pack_code
  ,'CHT' geo_key
  ,0 count_unit
  ,0 average_price
  ,sales_value
  ,0 sales_unit
  ,0 counting_units_obversion
  ,0 counting_unit
  ,0   pack_flag
  ,0   brand_flag
FROM AZ 



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

-- dtp 数据获取本同期数据--
insert overwrite table dm.dm_zk_retail_dtp_sales
select 
  year
  ,yq
  ,yyyymm
  ,iqvia_pack_code
  ,CASE WHEN geo_key ='CHT' then 'ROC' else geo_key end as AUDIT_COD
  --- 全国写死为 ROC 
  -- ,'ROC'                   AUDIT_COD
  ,sum(sales_value)			sales_value       
  ,sum(sales_unit)            sales_unit            
  ,sum(counting_unit)         counting_unit
  ,sum(sales_value_ly)        sales_value_ly    
  ,sum(sales_unit_ly)         sales_unit_ly
  ,sum(counting_unit_ly)      counting_unit_ly
  ,pack_flag
  ,brand_flag 
 from (
      select 
        year
        ,yq
        ,yyyymm
        ,iqvia_pack_code
        ,geo_key
        ,sales_value            
        ,sales_unit                  
        ,counting_unit              counting_unit
        ,0                          sales_value_ly    
        ,0                          sales_unit_ly
        ,0                          counting_unit_ly
        ,brand_flag
        ,pack_flag
      from dws.dws_retail_dtp_sales
      union all 
      select 
        cast(year+1 as int )        year
        ,concat(cast(left(yq,4)+1 as int ),right(yq,2)) yq
        ,cast(yyyymm+100 as int )   yyyymm 
        ,iqvia_pack_code
        ,geo_key
        ,0                          sales_value            
        ,0                          sales_unit                  
        ,0                          counting_unit
        ,sales_value                sales_value_ly    
        ,sales_unit                 sales_unit_ly
        ,counting_unit              counting_unit_ly
        ,brand_flag
        ,pack_flag
      from dws.dws_retail_dtp_sales
      where yyyymm +100 <= (select max(yyyymm) from dws.dws_retail_dtp_sales)
 )
 group by 1,2,3,4,5,12,13


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

-- dtp 部分生成dws 
insert overwrite table dws.dws_zk_retail_dtp_market 
select distinct t1.* 
from tmp.tmp_zk_retail_dtp_market_corp t1 
union 
select 
  '' region_type
,'' higher_level_region
,'DTP_OTHER Market' market
,iqvia_pack_code 
,'' iqvia_prod_code 		 
,'' iqvia_notes 
,'' new_pack_flag 
,'' is_exists_chpa 
,'' prescription_nature 
,'' common_name 
,'' dosage_form 
,'' category_name 
,'' product_name 
,'' brand_name 
,'' zk_corp_des 
,'' zk_pack_des 
,'' counting_unit_a 
,'' 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 
,CASE WHEN  t1.iqvia_pack_code LIKE '%AZ%' THEN 'A5Z' ELSE ''  END corp_cod 
,CASE WHEN  t1.iqvia_pack_code LIKE '%AZ%' THEN 'ASTRAZENECA' ELSE ''  END corp_des 
,CASE WHEN  t1.iqvia_pack_code LIKE '%AZ%' THEN '阿斯利康制药集团' ELSE ''  END 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 
,'' unit 
,'' counting_unit 
,'' dosage_unit  
,null  NRDL_ENTRY_DATE
,null 
,'1'           Market_Ratio
from dws.dws_retail_dtp_sales t1
where iqvia_pack_code  LIKE '%OTHERS'

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

-- dtp 部分生成 dm 
------------获取对应class 及其他维度-------
insert overwrite table  dm.dm_zk_retail_dtp_pack_property 
select  distinct 
 A.market ||'_'|| A.iqvia_pack_code  MARKET_PACK_KEY 
,A.iqvia_pack_code
,A.pack_des
,A.stgh_des
,A.pack_lch
,''  Family_cod
,''  Family_Name
,A.iqvia_prod_code PROD_COD
,A.prod_des
,A.prod_des_c
,A.cmps_cod
,A.cmps_des
,A.cmps_des_c
,A.atc1_cod
,A.atc2_cod
,A.atc3_cod
,A.atc4_cod
,A.app1_cod
,A.app2_cod
,A.app3_cod
,A.BIO_DESC
,A.gene_orig_desc
,A.eth_otc_desc
,A.nrdl_desc
,A.NRDL_ENTRY_DATE
,A.edl_desc
,A.TCM_DESC 
,A.PAED_DESC 
,A.GQCE_DESC 
,A.VBP_DESC 
,A.MANU_COD 
,A.MANU_DES 
,A.MANU_DES_C 
,A.MNFL_COD 
,A.MNFL_DES 
,A.CORP_COD 
,CASE WHEN A.corp_des LIKE '% GROUP%' THEN replace(A.corp_des,' GROUP','' ) ELSE A.corp_des END corp_des
,CORP_DES_C 
,'' 			 BrandType 
,A.MARKET 
,t5.KEY_COMPETITOR 
,CASE WHEN CORP_COD ='A5Z' THEN 'Y' ELSE 'N' END  IS_AZ 
,''      AZ_MAIN
,case when dmt.TA is null then 'Others MKT' else 'AZ Related MKT' end as AZ_Related
,atc1_des 
,atc1_des_c 
,atc2_des 
,atc2_des_c 
,atc3_des 
,atc3_des_c 
,atc4_des 
,atc4_des_c 
,app1_des 
,app1_des_c 
,app2_des 
,app2_des_c 
,app3_des 
,app3_des_c 
,ifnull(t3.class,'Others') as Class
,from_utc_timestamp(current_timestamp(),'UTC+8')        etl_insert_dt
,from_utc_timestamp(current_timestamp(),'UTC+8')        etl_update_dt
from dws.dws_zk_retail_dtp_market   A
left join dwd.dwd_gnd_ims_tblmarket_ta_map dmt on dmt.Market = A.Market 
left join dwd.dwd_gnd_tblclass t3
  on  A.market   = ifnull(t3.market            , A.market  )
  and A.iqvia_pack_code = ifnull(if( t3.pack_code REGEXP '^[0-9]',right(concat('000000000000',t3.pack_code),12),t3.pack_code), A.iqvia_pack_code)
  and A.iqvia_prod_code = ifnull(right(concat('000000000', t3.product_code),9 ) , A.iqvia_prod_code)
  and A.cmps_cod = ifnull(right(concat('000000',t3.molecule_code ), 6)     , A.cmps_cod)
  and A.corp_cod = ifnull(t3.Corporation_code  , A.corp_cod)
  and A.manu_cod = ifnull(t3.Manufacturer_Code , A.manu_cod)
  and A.ATC1_COD = ifnull(t3.ATC1_Code         , A.ATC1_COD)
  and A.ATC2_COD = ifnull(t3.ATC2_Code         , A.ATC2_COD)
  and A.ATC3_COD = ifnull(t3.ATC3_Code         , A.ATC3_COD)
  and A.ATC4_COD = ifnull(t3.ATC4_Code         , A.ATC4_COD)
  and A.APP1_COD = ifnull(t3.NFC1_Code         , A.APP1_COD)
  and A.APP2_COD = ifnull(t3.NFC2_Code         , A.APP2_COD)
  and A.APP3_COD = ifnull(t3.NFC3_Code         , A.APP3_COD)
  and A.STGH_DES = ifnull(t3.Strength          , A.STGH_DES)
left join (select distinct MARKET,PACK_COD,KEY_COMPETITOR from dm.dm_ims_td_pack_property) t5
on A.market = t5.MARKET
and A.iqvia_pack_code = t5.PACK_COD
 

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

-- MAGIC %md
-- MAGIC ## 其他

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

------------------------将品牌数据进行处理，获取同期值（有挺多冗余，但懒得改了）--------------------------------------
insert overwrite table tmp.tmp_zk_retail_nataional_brand_union 
select distinct 
        bq.brand_cat_type
          ,bq.TA
          ,bq.market      market
          ,bq.zk_brand_category    			     zk_brand    
          -- ,IQVIA_PROD_CODE        
          ,coalesce(b.iqvia_pack_code,c.iqvia_pack_code,d.iqvia_pack_code ) iqvia_pack_code
          ,coalesce(b.corp_cod,c.corp_cod,d.corp_cod )                      corp_cod
          ,t2.geo_key  
          ,bq.province_city
          ,bq.YYYYMM
          ,bq.year
          ,bq.quarter
          ,bq.zk_common_name
          ,bq.zk_manu_des
          ,bq.rc_name_en
          ,bq.ytd
          ,bq.yq
          ,BQ.sales_val
          ,bq.sales_vol
          ,bq.price 						 
          ,bq.num_dist_rate 				 
          ,bq.weig_dist_rate 		
          ,bq.val_share 					 
          ,bq.vol_share 
          ,tq.sales_val						            sales_val_ly
          ,tq.sales_vol                       sales_vol_ly
          ,tq.price 						              price_ly 			
          ,tq.num_dist_rate 				          num_dist_rate_ly	
          ,tq.weig_dist_rate 	                weig_dist_rate_ly
          ,tq.val_share 					            val_share_ly
          ,tq.vol_share 					            vol_share_ly
          ,bq.key_brand_ytd 				 
          ,bq.key_brand_rank_ytd 		 
          ,bq.top_brand_ytd 				 
          ,bq.top_brand_ms_ytd 			 
          ,bq.top_brand_inc_ms_ytd 		 
          ,bq.top_brand_gr_ytd 			 
          ,bq.key_brand_qtd 				 
          ,bq.key_brand_rank_qtd 		 
          ,bq.top_brand_qtd 				 
          ,bq.top_brand_ms_qtd 			 
          ,bq.top_brand_inc_ms_qtd 		 
          ,bq.top_brand_gr_qtd 			 
          ,bq.ranked_by
          ,kpi.key_brand_ytd_ly 				 
          ,kpi.key_brand_rank_ytd_ly 	 
          ,kpi.top_brand_ytd_ly 			 
          ,kpi.top_brand_ms_ytd_ly 		 
          ,kpi.top_brand_inc_ms_ytd_ly 	 
          ,kpi.top_brand_gr_ytd_ly 		 
          ,kpi.key_brand_qtd_ly 			 
          ,kpi.key_brand_rank_qtd_ly 	 
          ,kpi.top_brand_qtd_ly 			 
          ,kpi.top_brand_ms_qtd_ly		 
          ,kpi.top_brand_inc_ms_qtd_ly 	 
          ,kpi.top_brand_gr_qtd_ly	 
from  dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all bq   
left join dm.dm_zk_retail_geo t2 on bq.province_city = t2.province_city
left join (select distinct ZK_Prod_C,PROD_DES_C  from dwd.dwd_inc_gnd_retail_b2c_label_total) t3 on zk_brand_category = t3.ZK_Prod_C 
left join  dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all tq 
on bq.market =tq.market and bq.zk_brand_category =tq.zk_brand_category
   and bq.YYYYMM = cast(tq.YYYYMM + 100 as int ) and bq.zk_common_name = tq.zk_common_name
   and bq.zk_manu_des = tq.zk_manu_des and bq.province_city =tq.province_city
   and bq.ranked_by = tq.ranked_by
   and bq.brand_cat_type = tq.brand_cat_type
   and tq.brand_cat_type ='品牌'
left join (
    select distinct
        left(ytd,3) || cast(substr(ytd,4,2)+1 as int) || right( ytd,2 ) ytd
        ,market
        ,province_city
        ,key_brand_ytd 				  key_brand_ytd_ly 				 
        ,key_brand_rank_ytd 		key_brand_rank_ytd_ly 	 
        ,top_brand_ytd 				  top_brand_ytd_ly 			 
        ,top_brand_ms_ytd 			top_brand_ms_ytd_ly 		 
        ,top_brand_inc_ms_ytd 	top_brand_inc_ms_ytd_ly 	 
        ,top_brand_gr_ytd 			top_brand_gr_ytd_ly 		 
        ,key_brand_qtd 				  key_brand_qtd_ly 			 
        ,key_brand_rank_qtd 		key_brand_rank_qtd_ly 	 
        ,top_brand_qtd 				  top_brand_qtd_ly 			 
        ,top_brand_ms_qtd 			top_brand_ms_qtd_ly		 
        ,top_brand_inc_ms_qtd 	top_brand_inc_ms_qtd_ly 	 
        ,top_brand_gr_qtd       top_brand_gr_qtd_ly	 
        ,ranked_by
    from  dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all    
    where brand_cat_type='品类' and left(ytd,3) || cast(substr(ytd,4,2)+1 as int) || right( ytd,2 )   <= (select max(ytd) from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all where brand_cat_type='品类')
) kpi on bq.market = kpi.market and bq.ytd=kpi.ytd and bq.province_city =kpi.province_city 
      and bq.ranked_by = tq.ranked_by 
left join (select max(iqvia_pack_code) iqvia_pack_code,prod_des_c,corp_cod  from dwd.dwd_gnd_ext_retail_pack_property 
where iqvia_pack_code in (select DISTINCT iqvia_pack_code from dm.dm_zk_retail_sales)
group by prod_des_c,corp_cod) b   on t3.PROD_DES_C=b.prod_des_c
left join (select max(iqvia_pack_code) iqvia_pack_code,prod_des_c,corp_cod  from dwd.dwd_gnd_ext_retail_pack_property 
where iqvia_pack_code in (select DISTINCT iqvia_pack_code from dm.dm_zk_retail_sales)
group by prod_des_c,corp_cod) c   on bq.zk_brand_category=c.prod_des_c
left join (select max(iqvia_pack_code) iqvia_pack_code,product_name,corp_cod  from dwd.dwd_gnd_ext_retail_pack_property
where iqvia_pack_code in (select DISTINCT iqvia_pack_code from dm.dm_zk_retail_sales)
 group by product_name,corp_cod) d   on bq.zk_brand_category=d.product_name
where bq.brand_cat_type ='品牌'  and   bq.zk_brand_category not in ('多达一','天依宁','氨氯地平阿托伐他汀钙片' ,'OTHERS')
 

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

------------------------将RX市场数据合并并处理数据格式获取code --------------------------------------
---------关联 corp code 
---------是否total 数据打标
---------格式处理
---------dm 使用 niad 数据
---------全国数据 地理编码 为 roc 
insert overwrite table tmp.tmp_zk_retail_rx_ta_and_top_corp 
select 
	distinct
		case WHEN right(corporation,3) ='TTL' THEN 'ta_total' else 'ta_corp' end    data_type
		,case when replace(a.rx_type,' TTL','') ='NIAD' THEN 'DM'
		      when   replace(a.rx_type,' TTL','')   ='GI-Rx+OTC' then 'GI'
					  ELSE   replace(a.rx_type,' TTL','')  END  ta 
		,nvl(b.CORP_COD ,CORP_COD)    CORP_COD
    ,CASE WHEN right(corporation,3) ='TTL' THEN 'Y' else 'N' end  TOTAL_FLAG
		,a.quarter_flag   YTD
    ,'ROC'            GEO_KEY
		,replace(a.top1_brand_val,'-','')  top1_brand_val
		,coalesce(cast(a.top1_brand_gr as double ),0 )  top1_brand_gr
		,replace(a.top2_brand_val,'-','')  top2_brand_val
 		,coalesce(cast(a.top2_brand_gr as double ),0 )  top2_brand_gr
		,replace(a.top1_incremental_brand,'-','')  top1_incremental_brand
 		,coalesce(cast(a.top1_incremental_gr as double ),0 )    top1_incremental_gr
		,replace(a.top2_incremental_brand,'-','')  top2_incremental_brand
 		,coalesce(cast(a.top2_incremental_gr as double ),0 )    top2_incremental_gr
	from dwd.dwd_gnd_ext_retail_rx_ta_top_corp a 
	left join (select  ZK_Corp_C,max(case when CORP_COD ='F+N' then 'A5Z' ELSE CORP_COD END ) CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total group by ZK_Corp_C ) b  
	on a.corporation = b.ZK_Corp_C
  WHERE  rx_type <>'DM'
UNION ALL 
  select 
	distinct
		case WHEN right(corporation,3) ='TTL' THEN 'ta_total' else 'ta_corp' end    data_type
		,replace(a.rx_type,' TTL','')       ta 
		,nvl(b.CORP_COD ,CORP_COD)    CORP_COD
    ,CASE WHEN right(corporation,3) ='TTL' THEN 'Y' else 'N' end  TOTAL_FLAG
		,a.quarter_flag   YTD
    ,'ROC'            GEO_KEY
		,replace(a.top1_brand_val,'-','')  top1_brand_val
		,coalesce(cast(a.top1_brand_gr as double ) ,0 ) top1_brand_gr
		,replace(a.top2_brand_val,'-','')  top2_brand_val
 		,coalesce(cast(a.top2_brand_gr as double ) ,0 ) top2_brand_gr
		,replace(a.top1_incremental_brand,'-','')  top1_incremental_brand
 		,coalesce(cast(a.top1_incremental_gr as double ),0 )    top1_incremental_gr
		,replace(a.top2_incremental_brand,'-','')  top2_incremental_brand
 		,coalesce(cast(a.top2_incremental_gr as double ) ,0 )   top2_incremental_gr
	from dwd.dwd_gnd_ext_retail_rx_ta_top_corp a 
	left join (select  ZK_Corp_C,max(case when CORP_COD ='F+N' then 'A5Z' ELSE CORP_COD END ) CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total group by ZK_Corp_C ) b  
	on a.corporation = b.ZK_Corp_C
  WHERE  rx_type ='NIAD'
  union all 
  select distinct
		'top_corp'         data_type
		,NULL          ta 
		,nvl(b.CORP_COD,a.corporation)   CORP_COD
    ,'Y'                            TOTAL_FLAG
 		,a.quarter_flag
    ,'ROC'        GEO_KEY
		,replace(a.top1_brand_val,'-','')  top1_brand_val
		,coalesce(cast(a.top1_brand_gr as double ),0 )  top1_brand_gr
		,replace(a.top2_brand_val,'-','')  top2_brand_val
 		,coalesce(cast(a.top2_brand_gr as double ),0 )  top2_brand_gr
		,replace(a.top1_incremental_brand,'-','')  top1_incremental_brand
 		,coalesce(cast(a.top1_incremental_gr as double ) ,0 )   top1_incremental_gr
		,replace(a.top2_incremental_brand,'-','')  top2_incremental_brand
 		,coalesce(cast(a.top2_incremental_gr as double )  ,0 )  top2_incremental_gr
	from  dwd.dwd_gnd_ext_retail_rx_top_corp a 
	left join (select  ZK_Corp,max(case when CORP_COD ='F+N' then 'A5Z' ELSE CORP_COD END ) CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total group by ZK_Corp ) b  
	on a.corporation = b.ZK_Corp
 

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

---------------------------------------将pack表的price 数据进行处理----------------------------------------
---------关联 corp code 
---------获取geo_key  后续的audit key 
---------获取ta 值
---------获取pack 文件中计算price 的所有指标 及加权铺货率、数字铺货率
insert overwrite table tmp.tmp_zk_retail_price 
with a as (
  select
    df.brand_flag,df.zk_product_id,YYYYMM,pack.iqvia_pack_code,corp.corp_cod,zk_region
    ,sum(sales_value)           sales_value
    ,sum(sales_unit)            sales_unit
    ,sum(weighted_spread_rate)  weighted_spread_rate
    ,sum(digital_spread_rate)   digital_spread_rate
    ,sum(sales_value_ly)        sales_value_ly
    ,sum(sales_unit_ly)         sales_unit_ly
    ,sum(weighted_spread_rate_ly) weighted_spread_rate_ly   
    ,sum(digital_spread_rate_ly)  digital_spread_rate_ly
from (
      select YYYYMM,zk_product_id,zk_region
        ,sales_value,sales_unit,weighted_spread_rate,digital_spread_rate
        ,0 sales_value_ly
        ,0 sales_unit_ly
        ,0 weighted_spread_rate_ly
        ,0 digital_spread_rate_ly
        ,brand_flag 
      from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all 
      union all 
      select cast( YYYYMM + 100  as int ),zk_product_id,zk_region
        ,0 sales_value
        ,0 sales_unit
        ,0 weighted_spread_rate
        ,0 digital_spread_rate
        ,sales_value sales_value_ly
        ,sales_unit sales_unit_ly 
        ,weighted_spread_rate weighted_spread_rate_ly
        ,digital_spread_rate  digital_spread_rate_ly
        ,brand_flag 
      from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all 
      where YYYYMM + 100 <= (select max(yyyymm) FROM dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all)
) df 
left join (select product_id,iqvia_pack_code from dwd.dwd_gnd_ext_retail_pack_property GROUP BY product_id,iqvia_pack_code) pack on pack.product_id = df.zk_product_id 
left join tmp.tmp_zk_retail_pack_property_corp corp  on pack.iqvia_pack_code = corp.iqvia_pack_code
group by YYYYMM,pack.iqvia_pack_code,corp.corp_cod,zk_region,df.brand_flag,df.zk_product_id
)
select distinct  YYYYMM,zk_product_id,d.ta,c.geo_key,a.iqvia_pack_code,b.iqvia_prod_code,b.prod_des_c,corp_cod,zk_region
,sales_value,sales_value_ly,sales_unit,sales_unit_ly,weighted_spread_rate,weighted_spread_rate_ly,digital_spread_rate,digital_spread_rate_ly
from a  
left join (select distinct iqvia_pack_code ,prod_des_c,prod_des,iqvia_prod_code  from dm.dm_zk_retail_pack_property) b on a.iqvia_pack_code=b.iqvia_pack_code
left join (select distinct geo_key,province_city from dm.dm_zk_retail_geo) c on a.zk_region = c.province_city
left join (select distinct iqvia_pack_code,ta from dm.dm_zk_retail_market_property  ) d on a.iqvia_pack_code=d.iqvia_pack_code 
where a.iqvia_pack_code in (select distinct iqvia_pack_code from dm.dm_zk_retail_sales)
 

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

-- MAGIC %md
-- MAGIC ## 直取

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

/*
修改时间：20250311
修改人：chenwu
修改内容：
sales_quarter 由原来的 yyyyQn 转换成 yyyymm
*/
---------------------------------------将直取部分数据处理成KPI形式-----------------------------------------
insert overwrite table dws.dws_zk_retail_sales_kpi 
with top_corp_total as (
      ----------------------处理top_corp  大数--------------------
---------关联 corp code  特殊：dwd_inc_gnd_retail_b2c_label_total 这个表 az 公司有两个code ，需要取A5Z 那个
---------是否total 数据打标
---------格式处理
---------dm 使用 niad 数据
---------全国数据 地理编码 为 roc 
  select 
    a.corp_desc,
    'top_corp'                     data_type
    ,b.CORP_COD 
    ,'Y'       TOTAL_FLAG
    ,a.sales_quarter
    ,cast(a.sales_quarter as int)     yyyymm
    ,'ROC'                                    geo_key
    ,a.sales_amount*1000000                   sales_amount
    ,c.sales_amount*1000000                   sales_amount_ly
    ,a.rank1
		,null                                     ta
     FROM dwd.dwd_gnd_ext_retail_nataional_top_corp  a 
  left join (select  ZK_Corp_C,max(case when CORP_COD ='F+N' then 'A5Z' ELSE CORP_COD END ) CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total group by ZK_Corp_C ) b  
	on a.corp_desc= b.ZK_Corp_C
  left join  dwd.dwd_gnd_ext_retail_nataional_top_corp c 
  on a.corp_desc= c.corp_desc and nvl(a.corp_type,'' ) = nvl(c.corp_type,'' ) and 
  cast(a.sales_quarter as int) =cast(c.sales_quarter as int)+100
  where a.rank1 is not null 
  unioN all
   
select 
    a.corp_desc,
    'top_ta_corp'                     data_type
    ,B.CORP_COD --A.corp_name as CORP_COD, --自有的corp_name 是 corp_code
    ,case when A.corp_name  is null then 'Y' ELSE 'N' END TOTAL_FLAG
    ,a.sales_quarter
    ,cast(a.sales_quarter as int)     yyyymm
    ,'ROC'                                    geo_key
    ,a.sales_amount*1000000                   sales_amount
    ,c.sales_amount*1000000                   sales_amount_ly
    ,a.rank1
    ,CASE WHEN  trim(a.source_name) ='GI-Rx+OTC' THEN 'GI' ELSE trim(a.source_name) END                         ta
     FROM dwd.dwd_gnd_ext_retail_nataional_ta_top_corp  a 
  left join (select  ZK_Corp_C,max(case when CORP_COD ='F+N' then 'A5Z' ELSE CORP_COD END ) CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total group by ZK_Corp_C ) b 
	on a.corp_desc= b.ZK_Corp_C
  left join  dwd.dwd_gnd_ext_retail_nataional_ta_top_corp c 
  on a.corp_desc= c.corp_desc and nvl(a.corp_type,'' ) = nvl(c.corp_type,'' ) and 
  cast(a.sales_quarter as int) =cast(c.sales_quarter as int)+100
  and a.source_name =c.source_name
WHERE trim(a.source_name) <>'DM'
UNION ALL 
  select 
    CASE WHEN a.corp_desc='NIAD' THEN 'DM' ELSE a.corp_desc END  corp_desc,
    'top_ta_corp'                     data_type
    ,B.CORP_COD --A.corp_name as CORP_COD, --自有的corp_name 是 corp_code
    ,case when A.corp_name  is null then 'Y' ELSE 'N' END TOTAL_FLAG
    ,a.sales_quarter
    ,cast(a.sales_quarter as int)     yyyymm
    ,'ROC'                                    geo_key
    ,a.sales_amount*1000000                   sales_amount
    ,c.sales_amount*1000000                   sales_amount_ly
    ,a.rank1
    ,'DM'                      ta
     FROM dwd.dwd_gnd_ext_retail_nataional_ta_top_corp  a 
  left join (select  ZK_Corp_C,max(case when CORP_COD ='F+N' then 'A5Z' ELSE CORP_COD END ) CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total group by ZK_Corp_C ) b 
	on a.corp_desc= b.ZK_Corp_C
  left join  dwd.dwd_gnd_ext_retail_nataional_ta_top_corp c 
  on a.corp_desc= c.corp_desc and nvl(a.corp_type,'' ) = nvl(c.corp_type,'' ) and 
  cast(a.sales_quarter as int) =cast(c.sales_quarter as int)+100
  and a.source_name =c.source_name
WHERE trim(a.source_name) ='NIAD'
)


-------  tmp_zk_retail_nataional_brand_union（品牌数据报表） 的数据ranked by这个字段 枚举值为 volume  value ，这会导致部分指标重复，取数时任意其一就行，具体如代码取数情况

  SELECT distinct
        val.iqvia_pack_code                  pack_code
        ,val.corp_cod 			         corp_cod         
        ,val.YYYYMM                      
        ,null                  ytd          
        ,val.geo_key                  	 
        ,'share'  		      	KPI_name		 
        ,val.val_share				kpi_val	 
        ,val.val_share_ly     	kpi_val_ly		 
        ,null							 str_kpi_val
        ,null							 str_kpi_val_ly
        ,val.vol_share				   kpi_vol   
        ,val.vol_share_ly          kpi_vol_ly      
        ,null							  str_kpi_vol
        ,null                    str_kpi_vol_ly      
		,'N'								 TOTAL_FLAG		
       ,ta 
	from tmp.tmp_zk_retail_nataional_brand_union  val 
  where val.ranked_by ='volume'
  union all 
	SELECT distinct 
        ''                               pack_code
        ,''                              corp_cod
        ,null                            
        ,val.ytd                         
        ,val.geo_key                  	 
        ,'key_brand_ytd'  		      	 
        ,NULL                			 
        ,NULL                  			 
        ,val.key_brand_ytd				 
        ,val.key_brand_ytd_ly			 
        ,null							 
        ,null                            
        ,vol.key_brand_ytd				 
        ,val.key_brand_ytd_ly            
		,'N'								 TOTAL_FLAG
       ,val.ta                          
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.ytd = vol.ytd 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value' and val.ytd is not null 
   
UNION all 
	SELECT distinct
        ''                               pack_code
        ,''                              corp_cod  
        ,null                              
        ,val.ytd                           
        ,val.geo_key                  	   
        ,'key_brand_rank_ytd'  		       
        ,val.key_brand_rank_ytd				 
        ,val.key_brand_rank_ytd_ly   	   
        ,NULL                   			 
        ,NULL                   			 
        ,vol.key_brand_rank_ytd		       
        ,vol.key_brand_rank_ytd_ly         
        ,NULL                 				 
        ,NULL                             
		,'N'								 TOTAL_FLAG		
       ,val.ta                            
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.ytd = vol.ytd 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value' and val.ytd is not null 
  union all 
  	SELECT distinct
        -- MAX(val.iqvia_pack_code)         pack_code
        -- ,MAX(val.corp_cod )			     corp_cod     
        ''                               pack_code
        ,''                              corp_cod 
        ,null                                                      
        ,val.ytd                           
        ,val.geo_key                  	   
        ,'top_brand_ytd'  		      		 
        ,NULL                				 
        ,NULL                  				 
        ,val.top_brand_ytd					 
        ,val.top_brand_ytd_ly				 
        ,null								 
        ,null                              
        ,vol.top_brand_ytd					 
        ,val.top_brand_ytd_ly              
		,'N'								 TOTAL_FLAG
       ,val.ta                            
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.ytd = vol.ytd 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value' and val.ytd is not null 
 -- GROUP BY val.market,val.ytd,val.geo_key,val.top_brand_ytd,val.top_brand_ytd_ly,vol.top_brand_ytd,vol.top_brand_ytd_ly,val.ta      

  union all 
  	SELECT distinct
        -- MAX(val.iqvia_pack_code)         pack_code
        -- ,MAX(val.corp_cod )			     corp_cod     
        ''                               pack_code
        ,''                              corp_cod   
        ,null                           
        ,val.ytd                          
        ,val.geo_key                  	  
        ,'top_brand_ms_ytd'  		      
        ,val.top_brand_ms_ytd			 
        ,val.top_brand_ms_ytd_ly     	  
        ,NULL                   		 
        ,NULL                   		 
        ,vol.top_brand_ms_ytd 		      
        ,vol.top_brand_ms_ytd_ly          
        ,NULL                 			 
        ,NULL                             
		,'N'								 TOTAL_FLAG
       ,val.ta                           
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.ytd = vol.ytd 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value' and val.ytd is not null 
union all 
  	SELECT distinct
	    -- MAX(val.iqvia_pack_code)         pack_code
        -- ,MAX(val.corp_cod )			     corp_cod     
        ''                               pack_code
        ,''                              corp_cod
        ,null                               
        ,val.ytd                            
        ,val.geo_key                  	  	 
        ,'top_brand_inc_ms_ytd'  		    
        ,val.top_brand_inc_ms_ytd			 
        ,val.top_brand_inc_ms_ytd_ly     	 
        ,NULL                   			 
        ,NULL                   			 
        ,vol.top_brand_inc_ms_ytd 		    
        ,vol.top_brand_inc_ms_ytd_ly        
        ,NULL                 				 
        ,NULL                               
		,'N'								 TOTAL_FLAG
       ,val.ta                             
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.ytd = vol.ytd 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value' and val.ytd is not null 
union all 
  	SELECT distinct
        -- MAX(val.iqvia_pack_code)         pack_code
        -- ,MAX(val.corp_cod )			     corp_cod     
        ''                               pack_code
        ,''                              corp_cod
        ,null                              
        ,val.ytd                           
        ,val.geo_key                  	   
        ,'top_brand_gr_ytd'  		       
        ,val.top_brand_gr_ytd				 
        ,val.top_brand_gr_ytd_ly          
        ,NULL                   			 
        ,NULL                   			 
        ,vol.top_brand_gr_ytd      		   
        ,vol.top_brand_gr_ytd_ly           
        ,NULL                 				 
        ,NULL    
		,'N'								 TOTAL_FLAG		
       ,val.ta 
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.ytd = vol.ytd 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value' and val.ytd is not null 
union all 
  	SELECT distinct
        -- MAX(val.iqvia_pack_code)         pack_code
        -- ,MAX(val.corp_cod )			     corp_cod     
        ''                               pack_code
        ,''                              corp_cod
        ,val.YYYYMM                        
        ,null                              
        ,val.geo_key                  	   
        ,'key_brand_qtd'  		      		 
        ,NULL                				 
        ,NULL                  				 
        ,val.key_brand_qtd					 
        ,val.key_brand_qtd_ly				 
        ,null								 
        ,null                              
        ,vol.key_brand_qtd					 
        ,val.key_brand_qtd_ly         
		,'N'								 TOTAL_FLAG		
       ,val.ta                            
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.YYYYMM = vol.YYYYMM 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value' 
union all 
  	SELECT distinct
        -- MAX(val.iqvia_pack_code)         pack_code
        -- ,MAX(val.corp_cod )			     corp_cod     
        ''                               pack_code
        ,''                              corp_cod
        ,val.YYYYMM                        
        ,null                              
        ,val.geo_key                  	   
        ,'key_brand_rank_qtd'  		      
        ,val.key_brand_rank_qtd	   	       
        ,val.key_brand_rank_qtd_ly        
        ,NULL                   			 
        ,NULL                   			 
        ,vol.key_brand_rank_qtd    		   
        ,vol.key_brand_rank_qtd_ly      
        ,NULL                 				 
        ,NULL      
				,'N'								 TOTAL_FLAG		                        
       ,val.ta                            
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.YYYYMM = vol.YYYYMM 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value'  
  union all 
  	SELECT distinct
        -- MAX(val.iqvia_pack_code)         pack_code
        -- ,MAX(val.corp_cod )			     corp_cod     
        ''                               pack_code
        ,''                              corp_cod
        ,val.YYYYMM                        
        ,null                              
        ,val.geo_key                  	   
        ,'top_brand_qtd'  		      		 
        ,NULL                				 
        ,NULL                  				 
        ,val.top_brand_qtd					 
        ,val.top_brand_qtd_ly				 
        ,null								 
        ,null                              
        ,vol.top_brand_qtd					 
        ,val.top_brand_qtd_ly              
		,'N'								 TOTAL_FLAG
       ,val.ta                            
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.YYYYMM = vol.YYYYMM 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value' 
  union all 
  	SELECT distinct
        -- MAX(val.iqvia_pack_code)         pack_code
        -- ,MAX(val.corp_cod )			     corp_cod     
        ''                               pack_code
        ,''                              corp_cod
        ,val.YYYYMM                    
        ,null                          
        ,val.geo_key                   
        ,'top_brand_ms_qtd'  		   
        ,val.top_brand_ms_qtd			 
        ,val.top_brand_ms_qtd_ly      
        ,NULL                   		 
        ,NULL                   		 
        ,vol.top_brand_ms_qtd 		   
        ,vol.top_brand_ms_qtd_ly       
        ,NULL                 			 
        ,NULL                          
		,'N'								 TOTAL_FLAG
       ,val.ta 
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.YYYYMM = vol.YYYYMM 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value'  
union all 
  	SELECT distinct
        -- MAX(val.iqvia_pack_code)         pack_code
        -- ,MAX(val.corp_cod )			     corp_cod     
        ''                               pack_code
        ,''                              corp_cod
        ,val.YYYYMM                         
        ,null                               
        ,val.geo_key                  	  	 
        ,'top_brand_inc_ms_qtd'  		    
        ,val.top_brand_inc_ms_qtd			 
        ,val.top_brand_inc_ms_qtd_ly     	 
        ,NULL                   			 
        ,NULL                   			 
        ,vol.top_brand_inc_ms_qtd 		    
        ,vol.top_brand_inc_ms_qtd_ly        
        ,NULL                 				 
        ,NULL                               
		,'N'								 TOTAL_FLAG
       ,val.ta                             
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.YYYYMM = vol.YYYYMM 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value'
  union all 
  	SELECT distinct
        -- MAX(val.iqvia_pack_code)         pack_code
        -- ,MAX(val.corp_cod )			     corp_cod     
        ''                               pack_code
        ,''                              corp_cod
        ,val.YYYYMM                     
        ,null                           
        ,val.geo_key                  	 
        ,'top_brand_gr_qtd'  		    
        ,val.top_brand_gr_qtd			 
        ,val.top_brand_gr_qtd_ly        
        ,NULL                   		 
        ,NULL                   		 
        ,vol.top_brand_gr_qtd      		 
        ,vol.top_brand_gr_qtd_ly        
        ,NULL                 			 
        ,NULL                           
		,'N'								 TOTAL_FLAG
       ,val.ta                         
	from tmp.tmp_zk_retail_nataional_brand_union  val 
	left join tmp.tmp_zk_retail_nataional_brand_union vol 
	on val.market = vol.market 
      and val.YYYYMM = vol.YYYYMM 
      and val.geo_key = vol.geo_key
      and vol.ranked_by ='volume'
	where val.ranked_by ='value'
-----------------------------------处理 top 数据
union all 	
  SELECT distinct
        ''                               pack_code
        ,corp_cod               
        ,NULL                      YYYYMM                     
        ,ytd                         
        ,geo_key      
        ,'top1_brand_val'          kpi_name 	     
        ,null         			       kpi_val
        ,NULL                      kpi_val_ly   
        ,top1_brand_val            str_kpi_val		 
        ,NULL                   	 str_kpi_val_ly 
        ,null					             kpi_vol 
        ,null                      kpi_vol_ly
        ,null					             str_kpi_vol 
        ,null                      str_kpi_vol_ly  
		,TOTAL_FLAG
       ,ta                         
	from tmp.tmp_zk_retail_rx_ta_and_top_corp     
	
	 
	
  union all 
    SELECT distinct
        ''                               pack_code
        ,corp_cod               
        ,NULL                      YYYYMM                     
        ,ytd                         
        ,geo_key      
        ,'top1_brand_gr'           kpi_name 	     
        ,top1_brand_gr         			       kpi_val
        ,NULL                      kpi_val_ly   
        ,NULL                     str_kpi_val		 
        ,NULL                   	 str_kpi_val_ly 
        ,null					             kpi_vol 
        ,null                      kpi_vol_ly
        ,null					             str_kpi_vol 
        ,null                      str_kpi_vol_ly    
		,TOTAL_FLAG
       ,ta                         
	from tmp.tmp_zk_retail_rx_ta_and_top_corp  
  union all 
    SELECT distinct
        ''                               pack_code
        ,corp_cod               
        ,NULL                      YYYYMM                     
        ,ytd                         
        ,geo_key      
        ,'top2_brand_val'         kpi_name 	     
        ,null         			       kpi_val
        ,NULL                      kpi_val_ly   
        ,top2_brand_val            str_kpi_val		 
        ,NULL                   	 str_kpi_val_ly 
        ,null					             kpi_vol 
        ,null                      kpi_vol_ly
        ,null					             str_kpi_vol 
        ,null                      str_kpi_vol_ly     
		,TOTAL_FLAG
       ,ta                       
	from tmp.tmp_zk_retail_rx_ta_and_top_corp     
  union all 
    SELECT distinct
        ''                               pack_code
        ,corp_cod               
        ,NULL                      YYYYMM                     
        ,ytd                         
        ,geo_key      
        ,'top2_brand_gr'        		kpi_name
				,top2_brand_gr  	       kpi_val
        ,NULL                      kpi_val_ly   
        ,NULL                     str_kpi_val		 
        ,NULL                   	 str_kpi_val_ly 
        ,null					             kpi_vol 
        ,null                      kpi_vol_ly
        ,null					             str_kpi_vol 
        ,null                      str_kpi_vol_ly     
		,TOTAL_FLAG
       ,ta                        
	from tmp.tmp_zk_retail_rx_ta_and_top_corp  
  union all 
    SELECT distinct
        ''                               pack_code
        ,corp_cod               
        ,NULL                      YYYYMM                     
        ,ytd                         
        ,geo_key      
        ,'top1_incremental_brand'      kpi_name 	     
        ,null         			       kpi_val
        ,NULL                      kpi_val_ly   
        ,top1_incremental_brand            str_kpi_val		 
        ,NULL                   	 str_kpi_val_ly 
        ,null					             kpi_vol 
        ,null                      kpi_vol_ly
        ,null					             str_kpi_vol 
        ,null                      str_kpi_vol_ly     
		,TOTAL_FLAG
       ,ta                       
	from tmp.tmp_zk_retail_rx_ta_and_top_corp     
  union all 
    SELECT distinct
        ''                               pack_code
        ,corp_cod               
        ,NULL                      YYYYMM                     
        ,ytd                         
        ,geo_key      
        ,'top1_incremental_gr'         kpi_name 	     
        ,top1_incremental_gr         			       kpi_val
        ,NULL                      kpi_val_ly   
        ,NULL                     str_kpi_val		 
        ,NULL                   	 str_kpi_val_ly 
        ,null					             kpi_vol 
        ,null                      kpi_vol_ly
        ,null					             str_kpi_vol 
        ,null                      str_kpi_vol_ly     
		,TOTAL_FLAG
       ,ta                             
	from tmp.tmp_zk_retail_rx_ta_and_top_corp  
		
  union all 
    SELECT distinct
        ''                               pack_code
        ,corp_cod               
        ,NULL                      YYYYMM                     
        ,ytd                         
        ,geo_key      
        ,'top2_incremental_brand'           kpi_name 	     
        ,null         			       kpi_val
        ,NULL                      kpi_val_ly   
        ,top2_incremental_brand            str_kpi_val		 
        ,NULL                   	 str_kpi_val_ly 
        ,null					             kpi_vol 
        ,null                      kpi_vol_ly
        ,null					             str_kpi_vol 
        ,null                      str_kpi_vol_ly     
		,TOTAL_FLAG
       ,ta                         
	from tmp.tmp_zk_retail_rx_ta_and_top_corp     
  union all 
    SELECT distinct
        ''                               pack_code
        ,corp_cod               
        ,NULL                      YYYYMM                     
        ,ytd                         
        ,geo_key      
        ,'top2_incremental_gr'            kpi_name 	     
        ,top2_incremental_gr         			       kpi_val
        ,NULL                      kpi_val_ly   
        ,NULL                      str_kpi_val		 
        ,NULL                      str_kpi_val_ly 
        ,null			     kpi_vol 
        ,null                      kpi_vol_ly
        ,null			     str_kpi_vol 
        ,null                      str_kpi_vol_ly     
		,TOTAL_FLAG
       ,ta                          
	from tmp.tmp_zk_retail_rx_ta_and_top_corp  
---------------处理增量数据
union all 
select  distinct 
        ''                               pack_code
        ,bq.type_name                                corp_cod 
        ,cast(bq.sales_quarter as int) yyyymm
        ,null                                       ytd
        ,'ROC'                                      geo_key
        ,'corp_type_sales_amount'                             kpi_name
        ,bq.sales_amount * 1000000		          kpi_val 
        ,tq.sales_amount * 1000000		          kpi_val_ly
        ,NULL                                       str_kpi_val		 
        ,NULL                                       str_kpi_val_ly 
        ,null			                      kpi_vol 
        ,null                                       kpi_vol_ly
        ,null			                      str_kpi_vol 
        ,null                                       str_kpi_vol_ly     
		,'Y'									TOTAL_FLAG
       ,null ta         
  from dwd.dwd_gnd_ext_retail_rx_market bq
  left join dwd.dwd_gnd_ext_retail_rx_market tq on bq.type_name=tq.type_name 
  and  cast(bq.sales_quarter as int)= cast(tq.sales_quarter+100 as int)
  
  union all 
  ---- 处理top corp 及 rank 数据
  select  DISTINCT 
          ''                               pack_code
        ,corp_cod
        ,yyyymm
        ,null             ytd
        ,geo_key
        ,'top_corp_sales_amount'      kpi_name
        ,sales_amount     kpi_val
        ,sales_amount_ly  kpi_val_ly
        ,NULL             str_kpi_val		 
        ,NULL             str_kpi_val_ly 
        ,null			        kpi_vol 
        ,null             kpi_vol_ly
        ,null			        str_kpi_vol 
        ,null             str_kpi_vol_ly     
		,TOTAL_FLAG
       ,ta       
 from top_corp_total
 union all 
 select distinct
          ''                               pack_code
        ,corp_cod
        ,yyyymm
        ,null             ytd
        ,geo_key
        ,'top_corp_rank'            kpi_name
        ,rank1             kpi_val
        ,null             kpi_val_ly
        ,NULL             str_kpi_val		 
        ,NULL             str_kpi_val_ly 
        ,null			        kpi_vol 
        ,null             kpi_vol_ly
        ,null			        str_kpi_vol 
        ,null             str_kpi_vol_ly     
		,TOTAL_FLAG
       ,ta        
 from top_corp_total 
 where  lower(rank1) <>'total' 


 

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

---将dtp 数据转换成 kpi 格式---
----品牌数据 初始
---- 关联公司code 
-----处理pack code
-----处理格式
-----total flag 打标
----------------------------------------------------dtp 所有数据的ta  均为 ONCO-----------------------------------------------------------------------------
insert overwrite table dws.dws_zk_retail_dtp_sales_kpi
 with tmp as (
  -------------获取dtp top brand 的 本同期数据--------------------------
    select 
  cast(top_corp.sales_quarter as int ) yyyymm  
  ,top_corp.sales_quarter
  ,replace(top_corp.rank1,'-','')              rank1
  ,replace(top_corp.corp_name,'-','') crp_name 
  ,top_corp.brand_name 
  ,case when top_corp.brand_name  ='Others' then 'Others' else zk.IQVIA_PROD_CODE end iqvia_prod_code
  ,case when top_corp.brand_name  ='Others' then 'Others' else brand.iqvia_pack_code end iqvia_pack_code
  ,zk.CORP_COD  
  -- ,top_corp.source_name
  ,top_corp.sales_amount*1000000 sales_amount  
  ,tq.sales_amount*1000000 sales_amount_ly
  ,'Y' TOTAL_FLAG
from dwd.dwd_gnd_ext_retail_dtp_top_brand top_corp
left join dwd.dwd_gnd_ext_retail_dtp_top_brand tq 
on  cast(top_corp.sales_quarter as int ) = cast(tq.sales_quarter+100 as int ) and nvl(top_corp.corp_name,'') = nvl(tq.corp_name,'' ) 
and tq.rank1 =top_corp.rank1   and nvl(top_corp.brand_name,'' ) =nvl(tq.brand_name,'' )
left join (select distinct  ZK_Prod_C,IQVIA_PROD_CODE,ZK_Corp_C,CORP_COD from  dwd.dwd_inc_gnd_retail_b2c_label_total     )  zk 
on  top_corp.brand_name = zk.ZK_Prod_C  AND replace(top_corp.corp_name,'-','')=zk.ZK_Corp_C
left join (select PROD_COD IQVIA_PROD_CODE,max(PACK_COD) iqvia_pack_code,CORP_COD from dm.dm_ims_td_pack_property  group by PROD_COD,CORP_COD) brand on zk.IQVIA_PROD_CODE = brand.iqvia_prod_code  AND replace(zk.CORP_COD,'-','')=brand.corp_cod
UNION all
  -------获取 dtp top corp 得 本同期数据------------
    select 
  cast(top_corp.sales_quarter as int ) yyyymm  
  ,top_corp.sales_quarter
  ,replace(top_corp.rank1,'-','')              rank1
  ,replace(top_corp.corp_name,'-','') corp_name 
  ,'' brand_name
  ,''iqvia_prod_code
  ,'' iqvia_pack_code
  ,case when replace(top_corp.corp_name,'-','') ='Others' then '' else pack.corp_cod end  corp_cod
  -- ,top_corp.source_name
  ,top_corp.sales_amount*1000000 sales_amount  
  ,tq.sales_amount*1000000 sales_amount_ly
--   ,case when replace(top_corp.rank1,'-','')  ='' then 'Y' ELSE 'N' END TOTAL_FLAG
      ,'Y'   TOTAL_FLAG
from dwd.dwd_gnd_ext_retail_dtp_top_copd top_corp
left join dwd.dwd_gnd_ext_retail_dtp_top_copd tq 
on  cast(top_corp.sales_quarter as int ) = cast(tq.sales_quarter+100 as int ) and nvl(top_corp.corp_name,'') = nvl(tq.corp_name,'' ) 
and tq.rank1 =top_corp.rank1   
-- left join (select zk_corp_des,max(corp_cod) corp_cod from dwd.dwd_inc_gnd_retail_b2c_label_total group by zk_corp_des) pack  
left join (select distinct  ZK_Corp_C,CORP_COD from  dwd.dwd_inc_gnd_retail_b2c_label_total ) pack
on replace(top_corp.corp_name,'-','')=pack.ZK_Corp_C
)
select distinct 
      iqvia_prod_code            pack_code
      ,corp_cod
      ,'ONCO'    ta
      ,yyyymm
      ,null                      ytd
      ,'ROC'                     geo_key
      ,'top_corp_rank'       kpi_name
      ,cast(rank1 as double)     kpi_val 
      ,null                      kpi_val_ly
      ,null					             str_kpi_val
      ,null		            	     str_kpi_val_ly
      ,null					             kpi_vol 
      ,null                      kpi_vol_ly
      ,null					             str_kpi_vol 
      ,null                      str_kpi_vol_ly
      ,                           TOTAL_FLAG
      -- ,null  				             ta
from tmp where rank1 !=''
union all 
select distinct 
      iqvia_prod_code            pack_code
      ,corp_cod
      ,'ONCO'    ta
      ,yyyymm
      ,null                      ytd
      ,'ROC'                     geo_key
      ,'top_corp_sales_amount'             kpi_name
      ,sales_amount              kpi_val 
      ,sales_amount_ly           kpi_val_ly
      ,null					             str_kpi_val
      ,null		            	     str_kpi_val_ly
      ,null					             kpi_vol 
      ,null                      kpi_vol_ly
      ,null					             str_kpi_vol 
      ,null                      str_kpi_vol_ly
      ,TOTAL_FLAG
      -- ,null  				             ta
from tmp   


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

----------------------------------7月30日新增-------------------------------
-------------------------retail 自有部分数据处理成kpi 形式--------------------
insert overwrite table dws.dws_zk_retail_sales_own_kpi
--------在 retail 拆分好的结果表里获取pack 粒度的 price 分子
 select 
        a.iqvia_pack_code  			pack_code
        ,a.corp_cod
        ,YYYYMM
        ,null                                   ytd
        ,a.AUDIT_COD  geo_key
        ,'pack_price_fz'       		        KPI_name
        ,sum(sales_value)                       kpi_val
        ,sum(sales_value_ly)                    kpi_val_ly
        ,null					str_kpi_val
        ,null		            	        str_kpi_val_ly
        ,null				        kpi_vol 
        ,null                                   kpi_vol_ly
        ,null		      			 str_kpi_vol 
        ,null                                    str_kpi_vol_ly
	    ,'N'					 TOTAL_FLAG
        ,case when a.source[0] = '慢性阻塞性肺疾病' then 'RE'
              WHEN A.source[0]=	'慢性胃炎、胃溃疡' THEN 'GI'
              WHEN A.source[0] = 'NIAD'	THEN 'DM'
              WHEN A.source[0] IN ('Brilinta Market','他汀类+血脂康','高血压用药','酒石酸美托洛尔')	THEN 'CV'
              WHEN A.source[0] ='RD Market' THEN 'RD' 
        END    ta
        ,a.brand_flag
        ,a.pack_flag
	from    dm.dm_zk_retail_sales  a 
    GROUP BY   a.iqvia_pack_code  ,a.corp_cod,YYYYMM ,a.AUDIT_COD  
             ,case when a.source[0] = '慢性阻塞性肺疾病' then 'RE'
                    WHEN A.source[0]=	'慢性胃炎、胃溃疡' THEN 'GI'
                    WHEN A.source[0] = 'NIAD'	THEN 'DM'
                    WHEN A.source[0] IN ('Brilinta Market','他汀类+血脂康','高血压用药','酒石酸美托洛尔')	THEN 'CV'
                    WHEN A.source[0] ='RD Market' THEN 'RD' 
              END 
            ,a.brand_flag
            ,a.pack_flag
    having ta is not null and sum(sales_value)>0 
union all 
--------在 retail 拆分好的结果表里获取pack 粒度的 price 分母
 select 
        a.iqvia_pack_code  			pack_code
        ,a.corp_cod
        ,YYYYMM
        ,null                                   ytd
        ,a.AUDIT_COD  geo_key
        ,'pack_price_fm'       		        KPI_name
        ,sum(a.sales_unit)                       kpi_val
        ,sum(a.sales_unit_ly)                    kpi_val_ly
        ,null					str_kpi_val
        ,null		            	        str_kpi_val_ly
        ,null				        kpi_vol 
        ,null                                   kpi_vol_ly
        ,null		      			 str_kpi_vol 
        ,null                                    str_kpi_vol_ly
	    ,'N'					 TOTAL_FLAG
        ,case when a.source[0] = '慢性阻塞性肺疾病' then 'RE'
              WHEN A.source[0]=	'慢性胃炎、胃溃疡' THEN 'GI'
              WHEN A.source[0] = 'NIAD'	THEN 'DM'
              WHEN A.source[0] IN ('Brilinta Market','他汀类+血脂康','高血压用药','酒石酸美托洛尔')	THEN 'CV'
              WHEN A.source[0] ='RD Market' THEN 'RD' 
        END    ta
        ,a.brand_flag
        ,a.pack_flag
	from    dm.dm_zk_retail_sales  a 
    GROUP BY   a.iqvia_pack_code  ,a.corp_cod,YYYYMM ,a.AUDIT_COD  
             ,case when a.source[0] = '慢性阻塞性肺疾病' then 'RE'
                    WHEN A.source[0]=	'慢性胃炎、胃溃疡' THEN 'GI'
                    WHEN A.source[0] = 'NIAD'	THEN 'DM'
                    WHEN A.source[0] IN ('Brilinta Market','他汀类+血脂康','高血压用药','酒石酸美托洛尔')	THEN 'CV'
                    WHEN A.source[0] ='RD Market' THEN 'RD' 
              END 
            ,a.brand_flag
            ,a.pack_flag
    having ta is not null and sum(sales_unit)>0 

union all 
--------在 retail 拆分好的结果表里获取pack 粒度的 by counting unit 的 price 分母

 select 
        a.iqvia_pack_code  			pack_code
        ,a.corp_cod
        ,YYYYMM
        ,null                                   ytd
        ,a.AUDIT_COD  geo_key
        ,'pack_price_by_counting_unit_fm'       		        KPI_name
        ,sum(a.counting_unit)                       kpi_val
        ,sum(a.counting_unit_ly)                    kpi_val_ly
        ,null					str_kpi_val
        ,null		            	        str_kpi_val_ly
        ,null				        kpi_vol 
        ,null                                   kpi_vol_ly
        ,null		      			 str_kpi_vol 
        ,null                                    str_kpi_vol_ly
	    ,'N'					 TOTAL_FLAG
        ,case when a.source[0] = '慢性阻塞性肺疾病' then 'RE'
              WHEN A.source[0]=	'慢性胃炎、胃溃疡' THEN 'GI'
              WHEN A.source[0] = 'NIAD'	THEN 'DM'
              WHEN A.source[0] IN ('Brilinta Market','他汀类+血脂康','高血压用药','酒石酸美托洛尔')	THEN 'CV'
              WHEN A.source[0] ='RD Market' THEN 'RD' 
        END    ta
        ,a.brand_flag
        ,a.pack_flag
	from    dm.dm_zk_retail_sales  a 
    GROUP BY   a.iqvia_pack_code  ,a.corp_cod,YYYYMM ,a.AUDIT_COD  
             ,case when a.source[0] = '慢性阻塞性肺疾病' then 'RE'
                    WHEN A.source[0]=	'慢性胃炎、胃溃疡' THEN 'GI'
                    WHEN A.source[0] = 'NIAD'	THEN 'DM'
                    WHEN A.source[0] IN ('Brilinta Market','他汀类+血脂康','高血压用药','酒石酸美托洛尔')	THEN 'CV'
                    WHEN A.source[0] ='RD Market' THEN 'RD' 
              END 
            ,a.brand_flag
            ,a.pack_flag
    having ta is not null and sum(counting_unit)>0

 union all 
 --------在 retail pack 粒度的文件中获取加权铺货率

select  distinct 
         iqvia_pack_code   pack_code
        ,corp_cod
        ,YYYYMM
        ,null ytd
        ,geo_key
        ,'weighted_spread_rate'     KPI_name
        ,weighted_spread_rate       kpi_val
        ,weighted_spread_rate_ly    kpi_val_ly
        ,null					         str_kpi_val
        ,null		            	 str_kpi_val_ly
        ,null					         kpi_vol 
        ,null                  kpi_vol_ly
        ,null		      			   str_kpi_vol 
        ,null                  str_kpi_vol_ly
		    ,'N'					  			 TOTAL_FLAG
        ,ta				             ta
        ,case when prod.prod_des_c  is null  THEN 2 else  1 END  brand_flag
        ,case when geo_key ='CHT' THEN 2 else  1 END             pack_flag
	from tmp.tmp_zk_retail_price   a 
      left join (SELECT DISTINCT b.prod_des_c FROM TMP.tmp_zk_retail_nataional_brand_union A 
                LEFT JOIN (SELECT DISTINCT iqvia_pack_code,prod_des_c 
                           FROM dm.dm_zk_retail_pack_property) b  on a.iqvia_pack_code=b.iqvia_pack_code
                ) prod on a.prod_des_c=prod.prod_des_c 
union all 
 --------在 retail pack 粒度的文件中获取数字铺货率

select   distinct
         iqvia_pack_code
        ,corp_cod
        ,YYYYMM
        ,null ytd
        ,geo_key
        ,'digital_spread_rate'     KPI_name
        ,digital_spread_rate       kpi_val
        ,digital_spread_rate_ly    kpi_val_ly
        ,null					         str_kpi_val
        ,null		            	 str_kpi_val_ly
        ,null					         kpi_vol 
        ,null                  kpi_vol_ly
        ,null		      			   str_kpi_vol 
        ,null                  str_kpi_vol_ly
		    ,'N'					  			 TOTAL_FLAG
        ,ta				             ta
        ,case when prod.prod_des_c  is null  THEN 2 else  1 END  brand_flag
        ,case when geo_key ='CHT' THEN 2 else  1 END             pack_flag
	from tmp.tmp_zk_retail_price   a 
      left join (SELECT DISTINCT b.prod_des_c FROM TMP.tmp_zk_retail_nataional_brand_union A 
                LEFT JOIN (SELECT DISTINCT iqvia_pack_code,prod_des_c 
                           FROM dm.dm_zk_retail_pack_property) b  on a.iqvia_pack_code=b.iqvia_pack_code
                ) prod on a.prod_des_c=prod.prod_des_c 
union  all 
 --------在 retail pack 粒度的文件中获取计算pack 粒度的price 
select distinct 
        iqvia_pack_code
        ,corp_cod
        ,YYYYMM
        ,null ytd
        ,geo_key
        ,'pack_price'     KPI_name
        ,round(sum(sales_value)/sum(sales_unit),3)  kpi_val
        ,round(sum(sales_value_ly)/sum(sales_unit_ly),3)  kpi_val_ly
        ,null					         str_kpi_val
        ,null		            	 str_kpi_val_ly
        ,null					         kpi_vol 
        ,null                  kpi_vol_ly
        ,null		      			   str_kpi_vol 
        ,null                  str_kpi_vol_ly
		    ,'N'					  			 TOTAL_FLAG
        ,ta				             ta
        ,case when prod.prod_des_c  is null  THEN 2 else  1 END  brand_flag
        ,case when geo_key ='CHT' THEN 2 else  1 END             pack_flag
	from tmp.tmp_zk_retail_price   a 
      left join (SELECT DISTINCT b.prod_des_c FROM TMP.tmp_zk_retail_nataional_brand_union A 
                LEFT JOIN (SELECT DISTINCT iqvia_pack_code,prod_des_c 
                           FROM dm.dm_zk_retail_pack_property) b  on a.iqvia_pack_code=b.iqvia_pack_code
                ) prod on a.prod_des_c=prod.prod_des_c 
  group by corp_cod,YYYYMM,geo_key,ta,iqvia_pack_code
          ,case when prod.prod_des_c  is null  THEN 2 else  1 END  
          ,case when geo_key ='CHT' THEN 2 else  1 END              

union all 
 --------在 retail pack 粒度的文件中获取计算pack 粒度的price by counting unit  

select distinct
        a.iqvia_pack_code
        ,a.corp_cod
        ,YYYYMM
        ,null ytd
        ,geo_key
        ,'pack_price_by_counting_unit'     KPI_name
        ,round(sum(sales_value)/sum(sales_unit*(b.counting_unit/coalesce(b.unit,1 ))),3)  kpi_val
        ,round(sum(sales_value_ly)/sum(sales_unit_ly*(b.counting_unit/coalesce(b.unit,1 ))),3)  kpi_val_ly
        ,null					         str_kpi_val
        ,null		            	 str_kpi_val_ly
        ,null					         kpi_vol 
        ,null                  kpi_vol_ly
        ,null		      			   str_kpi_vol 
        ,null                  str_kpi_vol_ly
		    ,'N'					  			 TOTAL_FLAG
        ,ta				             ta
        ,case when prod.prod_des_c  is null  THEN 2 else  1 END  brand_flag
        ,case when geo_key ='CHT' THEN 2 else  1 END             pack_flag
    from tmp.tmp_zk_retail_price   a 
    left join dwd.dwd_gnd_ext_retail_pack_property b on a.zk_product_id =b.product_id  
    left join (SELECT DISTINCT b.prod_des_c FROM TMP.tmp_zk_retail_nataional_brand_union A 
                LEFT JOIN (SELECT DISTINCT iqvia_pack_code,prod_des_c 
                           FROM dm.dm_zk_retail_pack_property) b  on a.iqvia_pack_code=b.iqvia_pack_code
                ) prod on a.prod_des_c=prod.prod_des_c   
  group by a.corp_cod,YYYYMM,geo_key,ta,a.iqvia_pack_code
            ,case when prod.prod_des_c  is null  THEN 2 else  1 END  
            ,case when geo_key ='CHT' THEN 2 else  1 END       
union all 
 --------在 retail brand 品牌文件中获取数字铺货率 并对re 特殊处理，re相关品牌优先取inhaled market 对应 pack  

  SELECT distinct
        nvl(B.iqvia_pack_code,val.iqvia_pack_code )                  pack_code
        ,val.corp_cod 			         corp_cod    
        ,val.YYYYMM				   YYYYMM
        ,null                                ytd
        ,val.geo_key                  	   geo_key 
        ,'num_dist_rate'  		         KPI_name 
        ,val.num_dist_rate			   kpi_val 
        ,val.num_dist_rate_ly			   kpi_val_ly 
        ,null					   str_kpi_val
        ,null		            	   str_kpi_val_ly
        ,null					   kpi_vol 
        ,null                                kpi_vol_ly
        ,null					   str_kpi_vol 
        ,null                                str_kpi_vol_ly
		    ,'N'								 TOTAL_FLAG
        ,ta 				         ta
        ,case when prod.prod_des_c  is null  THEN 2 else  1 END  brand_flag
        ,case when geo_key ='CHT' THEN 2 else  1 END             pack_flag
   from tmp.tmp_zk_retail_nataional_brand_union val 
   left join (select distinct iqvia_pack_code ,prod_des_c from dm.dm_zk_retail_pack_property ) c on val.iqvia_pack_code=c.iqvia_pack_code
   left join (select prod_des_c,max(iqvia_pack_code) iqvia_pack_code from dm.dm_zk_retail_pack_property
            --   where market='Inhaled Extended Market by Brand' group by  1) b    on   c.prod_des_c =b.prod_des_c
            where market='Inhaled Extended Market' group by  1) b    on   c.prod_des_c =b.prod_des_c
    left join (SELECT DISTINCT b.prod_des_c FROM TMP.tmp_zk_retail_nataional_brand_union A 
                LEFT JOIN (SELECT DISTINCT iqvia_pack_code,prod_des_c 
                           FROM dm.dm_zk_retail_pack_property) b  on a.iqvia_pack_code=b.iqvia_pack_code
                ) prod on c.prod_des_c=prod.prod_des_c   
   where     val.ranked_by ='volume'
  union all 
 --------在 retail brand 品牌文件中获取加权铺货率 并对re 特殊处理，re相关品牌优先取inhaled market 对应 pack  

  SELECT distinct
        nvl(B.iqvia_pack_code,val.iqvia_pack_code )                  pack_code
        ,val.corp_cod 			         corp_cod    
        ,val.YYYYMM				   YYYYMM
        ,null                                ytd
        ,val.geo_key                  	   geo_key 
        ,'weig_dist_rate'  		       
        ,val.weig_dist_rate				 
        ,val.weig_dist_rate_ly	
        ,null					   str_kpi_val
        ,null		            	   str_kpi_val_ly
        ,null					   kpi_vol 
        ,null                                kpi_vol_ly
        ,null					   str_kpi_vol 
        ,null                                str_kpi_vol_ly
		    ,'N'								 TOTAL_FLAG
        ,ta 				         ta
        ,case when prod.prod_des_c  is null  THEN 2 else  1 END  brand_flag
        ,case when geo_key ='CHT' THEN 2 else  1 END             pack_flag
   from tmp.tmp_zk_retail_nataional_brand_union val 
   left join (select distinct iqvia_pack_code ,prod_des_c from dm.dm_zk_retail_pack_property ) c on val.iqvia_pack_code=c.iqvia_pack_code
--    left join (select prod_des_c,max(iqvia_pack_code) iqvia_pack_code from dm.dm_zk_retail_pack_property where market='Inhaled Extended Market by Brand' group by  1) b    on   c.prod_des_c =b.prod_des_c
    left join (select prod_des_c,max(iqvia_pack_code) iqvia_pack_code from dm.dm_zk_retail_pack_property where market='Inhaled Extended Market' group by  1) b
           on c.prod_des_c =b.prod_des_c
    left join (SELECT DISTINCT b.prod_des_c FROM TMP.tmp_zk_retail_nataional_brand_union A 
                LEFT JOIN (SELECT DISTINCT iqvia_pack_code,prod_des_c 
                           FROM dm.dm_zk_retail_pack_property) b  on a.iqvia_pack_code=b.iqvia_pack_code
                ) prod on c.prod_des_c=prod.prod_des_c   
   where     val.ranked_by ='volume'

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

---------------将所有渠道kpi 数据汇总到dm --------------
-----KPI 表汇总
-----ytd  为是否ytd flag 
-----geo 将cht(全国) 改成 ROC 
-----DTP FLAG   O  非dtp ， 1 dtp 数据
insert overwrite table  dm.dm_zk_retail_sales_kpi 
(pack_code
,corp_cod
,ta
,YYYYMM
,ytd
,geo_key
,KPI_name
,kpi_val
,kpi_val_ly
,str_kpi_val
,str_kpi_val_ly
,kpi_vol
,kpi_vol_ly
,str_kpi_vol
,str_kpi_vol_ly
,TOTAL_FLAG
,data_source
,dtp_flag
,brand_flag
,pack_flag
)
--------retail 渠道 kpi  
select 
    nvl(pack_code,'')  pack_code  
    ,nvl(corp_cod,'')  corp_cod
    ,nvl(ta,'')   ta
    ,nvl(YYYYMM,cast( if(length(ytd)=7 AND substr(ytd,6,1)<>'Q',concat('20',substr(ytd,4,2))*100 + right(ytd,2) ,concat('20',substr(ytd,4,2))*100  + right(ytd,1) * 3 ) as int ))   YYYYMM
    ,if(ytd IS NOT NULL ,'Y' ,'N')  YTD
    ,case when geo_key ='CHT' THEN 'ROC' ELSE nvl(geo_key,'') END geo_key
    ,nvl(kpi_name,'') as kpi_name
    ,nvl(cast(kpi_val as double),0)  kpi_val 
    ,nvl(cast(kpi_val_ly as double),0) kpi_val_ly  
    ,nvl(str_kpi_val,'')  
    ,nvl(str_kpi_val_ly,'')  
    ,nvl(cast(kpi_vol as double),0) kpi_vol  
    ,nvl(cast(kpi_vol_ly as double),0) kpi_vol_ly  
    ,nvl(str_kpi_vol,'')  
    ,nvl(str_kpi_vol_ly,'')  
    ,nvl(TOTAL_FLAG,'') 
    ,'Retail(Quarterly)'  data_source
    ,'0' dtp_flag
    ,0
    , 0
 from dws.dws_zk_retail_sales_kpi
 WHERE nvl(ta,'' ) NOT IN ('GI-Rx','GI-OTC')
 ---------------dtp 的kpi 
 union all 
select 
    nvl(pack_code,'')  pack_code  
    ,nvl(corp_cod,'')  corp_cod
    ,nvl(ta,'')   ta
    ,nvl(YYYYMM,cast( if(length(ytd)=7,concat('20',substr(ytd,4,2))*100 + right(ytd,1) * 3 ,substr(ytd,4,4) * 100 + right(ytd,1) * 3 ) as int ))   YYYYMM
    ,if(ytd IS NOT NULL ,'Y' ,'N')  YTD
    ,case when geo_key ='CHT' THEN 'ROC' ELSE nvl(geo_key,'') END 
    ,nvl(kpi_name,'')
    ,nvl(kpi_val,0)
    ,nvl(kpi_val_ly,0)  
    ,nvl(str_kpi_val,'')  
    ,nvl(str_kpi_val_ly,'')  
    ,nvl(kpi_vol,0)  
    ,nvl(kpi_vol_ly,0)  
    ,nvl(str_kpi_vol,'')  
    ,nvl(str_kpi_vol_ly,'')  
    ,nvl(TOTAL_FLAG,'') 
    ,'Retail(Quarterly)'  data_source
    ,'1' dtp_flag
    ,0
    ,0
from  dws.dws_zk_retail_dtp_sales_kpi 
 WHERE nvl(ta,'' ) NOT IN ('GI-Rx','GI-OTC')
--拼接retail 自有部分kpi 
 union all 
select 
    nvl(pack_code,'')  pack_code  
    ,nvl(corp_cod,'')  corp_cod
    ,nvl(ta,'')   ta
    ,nvl(YYYYMM,cast( if(length(ytd)=7,concat('20',substr(ytd,4,2))*100 + right(ytd,1) * 3 ,substr(ytd,4,4) * 100 + right(ytd,1) * 3 ) as int ))   YYYYMM
    ,if(ytd IS NOT NULL ,'Y' ,'N')  YTD
    ,case when geo_key ='CHT' THEN 'ROC' ELSE nvl(geo_key,'') END geo_key
    ,nvl(kpi_name,'')
    ,nvl(cast(kpi_val as double),0)  kpi_val 
    ,nvl(cast(kpi_val_ly as double),0) kpi_val_ly  
    ,nvl(str_kpi_val,'')  
    ,nvl(str_kpi_val_ly,'')  
    ,nvl(cast(kpi_vol as double),0) kpi_vol  
    ,nvl(cast(kpi_vol_ly as double),0) kpi_vol_ly  
    ,nvl(str_kpi_vol,'')  
    ,nvl(str_kpi_vol_ly,'')  
    ,nvl(TOTAL_FLAG,'') 
    ,'Retail(Quarterly)'  data_source
    ,'0' dtp_flag
    ,nvl(brand_flag,0)
    ,nvl(pack_flag,0)
 from dws.dws_zk_retail_sales_own_kpi
 WHERE nvl(ta,'' ) NOT IN ('GI-Rx','GI-OTC')
  
 


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

/*
修改人：chenwu
修改时间：20250319
修改内容：如果tblbrandratio 里面的没有对应的 ratio 则用market_ratio兜底而不是直接1
*/
------------------------生产retail 的ratio 表，该表系数用于计算pdot--------
insert overwrite table dm.dm_zk_retail_market_ratio 
select distinct
    a.market || a.iqvia_pack_code                     as market_pack_key
    ,a.iqvia_pack_code                                as pack_cod
    ,a.prod_des                                       as prod_des
    ,a.cmps_des                                       as cmps_des
    ,a.corp_cod                                       as corp_cod
    ,a.market                                         as market
    ,a.extend_market_ratio                            as value_market_ratio
    ,a.extend_market_ratio                            as unit_market_ratio
		,coalesce(case when t4.ratio is null then t5.ratio*coalesce(a.extend_market_ratio ,1 )
				 else t4.ratio*coalesce(a.extend_market_ratio ,1 )
				 end ,a.extend_market_ratio ) countingunit_market_ratio --20250319如果tblbrandratio 里面的没有对应的 ratio 则用market_ratio兜底而不是直接1
   ,from_utc_timestamp(current_timestamp(),'UTC+8') 	    AS etl_insert_dt
   ,from_utc_timestamp(current_timestamp(),'UTC+8') 	    AS etl_update_dt
 from tmp.tmp_dm_zk_retail_pack_property  a 
left join (--COUNTINGUNIT
	select distinct 
		market,
		cmps_cod,
		cmps_des,
		pack_cod,
		pack_des,
		level,
		type,
		cast(ratio as float) as ratio,
		cast(starttime as int) starttime,
		cast (endtime as int) endtime
	from dwd.dwd_gnd_ims_tblbrandratio 
    where UPPER(TYPE) = 'COUNTINGUNIT' and UPPER(Level) = 'PACK'   
)t4 on a.market=t4.market
    and if( a.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',a.iqvia_pack_code),12),a.iqvia_pack_code)
		= if( t4.PACK_COD REGEXP '^[0-9]',right(concat('000000000000',t4.PACK_COD),12),t4.PACK_COD)
		--right(concat('0000000',a.iqvia_pack_code ), 7) = right(concat('0000000',t4.PACK_COD ),7 )
left join (--COUNTINGUNIT
	select distinct 
		market,
		cmps_cod,
		cmps_des,
		pack_cod,
		pack_des,
		level,
		type,
		cast(ratio as float) as ratio,
		cast(starttime as int) starttime,
		cast (endtime as int) endtime
	from dwd.dwd_gnd_ims_tblbrandratio 
    where UPPER(TYPE) = 'COUNTINGUNIT' and UPPER(Level) = 'MOLECULE'
) t5 on a.market=t5.market
    and  right(concat('000000',a.CMPS_COD ),6 )= right(concat('000000',t5.CMPS_COD ),6 )
union all 
select distinct
    a.market || a.iqvia_pack_code                     as market_pack_key
    ,a.iqvia_pack_code                                as pack_cod
    ,a.prod_des                                       as prod_des
    ,a.cmps_des                                       as cmps_des
    ,a.corp_cod                                       as corp_cod
    ,a.market                                         as market
    ,a.Market_Ratio                            as value_market_ratio
    ,a.Market_Ratio                            as unit_market_ratio
		,coalesce(case when t4.ratio is null then t5.ratio*coalesce(a.Market_Ratio ,1 )
				 else t4.ratio*coalesce(a.Market_Ratio ,1 )
				 end ,a.Market_Ratio  ) countingunit_market_ratio --20250319如果tblbrandratio 里面的没有对应的 ratio 则用market_ratio兜底而不是直接1
   ,from_utc_timestamp(current_timestamp(),'UTC+8') 	    AS etl_insert_dt
   ,from_utc_timestamp(current_timestamp(),'UTC+8') 	    AS etl_update_dt
 from dws.dws_zk_retail_dtp_market  a 
left join (--COUNTINGUNIT
	select distinct 
		market,
		cmps_cod,
		cmps_des,
		pack_cod,
		pack_des,
		level,
		type,
		cast(ratio as float) as ratio,
		cast(starttime as int) starttime,
		cast (endtime as int) endtime
	from dwd.dwd_gnd_ims_tblbrandratio 
    where UPPER(TYPE) = 'COUNTINGUNIT' and UPPER(Level) = 'PACK'
)t4 on a.market=t4.market
    -- and right(concat('0000000',a.iqvia_pack_code ), 7)  = right(concat('0000000',t4.PACK_COD ),7 )
    and if( a.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',a.iqvia_pack_code),12),a.iqvia_pack_code)
		= if( t4.PACK_COD REGEXP '^[0-9]',right(concat('000000000000',t4.PACK_COD),12),t4.PACK_COD)
left join (--COUNTINGUNIT
	select distinct 
		market,
		cmps_cod,
		cmps_des,
		pack_cod,
		pack_des,
		level,
		type,
		cast(ratio as float) as ratio,
		cast(starttime as int) starttime,
		cast (endtime as int) endtime
	from dwd.dwd_gnd_ims_tblbrandratio 
    where UPPER(TYPE) = 'COUNTINGUNIT' and UPPER(Level) = 'MOLECULE'
) t5 on a.market=t5.market
    and  right(concat('000000',a.CMPS_COD ),6 ) = right(concat('000000',t5.CMPS_COD ),6 )
 