Files
MarketAnalysis-ETL/EC/03 ec_load_data_backup.sql
2026-04-29 10:15:15 +00:00

1545 lines
48 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Databricks notebook source
-- 有几张表发生了变化
-- tmp.tmp_ec_temp_market
-- tmp.tmp_ec_temp_market_del
-- dws.dws_zk_ec_pack_property
-- dm.dm_zk_ec_market_ratio
-- COMMAND ----------
insert overwrite table tmp.tmp_ec_temp_market
select distinct
b.iqvia_lineno ,
if( nvl(b.iqvia_pack_code,0) REGEXP '^[0-9]',right(concat('000000000000',nvl(b.iqvia_pack_code,0)),12),nvl(b.iqvia_pack_code,0)) as iqvia_pack_code,-- right(concat('0000000',nvl(b.iqvia_pack_code,0)),7)
right(concat('000000000',nvl(b.iqvia_prod_code,0)),9) as prod_cod ,-- right(concat('00000',nvl(b.iqvia_prod_code,0)),5)
b.iqvia_notes ,
b.new_pack_flag ,
b.is_exists_chpa ,
right(concat('0000000',nvl(b.product_id,0)),7) as product_id,
b.prescription_nature ,
b.medicine_type ,
b.zk_medicine_tier1 ,
b.zk_medicine_tier2 ,
b.zk_medicine_tier3 ,
b.zk_medicine_tier4 ,
b.common_name ,
b.dosage_form ,
b.user_type ,
b.category_name ,
b.product_name ,
b.brand_name ,
b.zk_manu_des ,
b.zk_corp_des ,
b.zk_pack_des ,
b.counting_unit_a ,
b.dosage_unit_a ,
b.unit_a ,
b.app1_cod ,
b.app1_des ,
b.app1_des_c ,
b.app2_cod ,
b.app2_des ,
b.app2_des_c ,
b.app3_cod ,
b.app3_des ,
b.app3_des_c ,
b.atc1_cod ,
b.atc1_des ,
b.atc1_des_c ,
b.atc2_cod ,
b.atc2_des ,
b.atc2_des_c ,
b.atc3_cod ,
b.atc3_des ,
b.atc3_des_c ,
b.atc4_cod ,
b.atc4_des ,
b.atc4_des_c ,
b.bio_desc ,
right(concat('000000',nvl(b.cmps_cod,0 )),6) cmps_cod,
b.cmps_des ,
b.cmps_des_c ,
b.corp_cod ,
b.corp_des ,
b.corp_des_c ,
b.edl_desc ,
b.eth_otc_desc ,
b.gene_orig_desc ,
b.gqce_desc ,
b.manu_cod ,
b.manu_des ,
b.manu_des_c ,
b.mnfl_cod ,
b.mnfl_des ,
b.nrdl_desc ,
b.pack_des ,
b.stgh_des ,
b.pack_lch ,
b.paed_desc ,
b.prod_des ,
b.prod_des_c ,
b.tcm_desc ,
b.vbp_desc ,
b.unit ,
b.counting_unit ,
b.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,
-- c.ta,
if(b.corp_des_c in ('阿斯利康制药集团'),'Y','N') as is_az
from dwd.dwd_gnd_ext_retail_pack_property as b
left join dwd.dwd_ims_td_pack_additional_attribute mole on b.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,pack_desc,strength,product_code,product_desc,molecule_code,molecule_desc,not_in_flag,extend_market,extend_market_ratio,corporation_code,corporation_desc,manufacturer_code,manufacturer_desc,zk_common_name,zk_product_id,zk_dosage_form
from dwd.dwd_gnd_ec_retail_tblmarket_rule
where extend_market is null and ( NOT_IN_FLAG IS NULL or NOT_IN_FLAG = '1' )
) tblmkt
on COALESCE(b.ATC1_COD,'') = case when tblmkt.ATC1_Code is null then COALESCE(b.ATC1_COD,'') else tblmkt.ATC1_Code end
and COALESCE(b.ATC2_COD,'') = case when tblmkt.ATC2_Code is null then COALESCE(b.ATC2_COD,'') else tblmkt.ATC2_Code end
and COALESCE(b.ATC3_COD,'') = case when tblmkt.ATC3_Code is null then COALESCE(b.ATC3_COD,'') else tblmkt.ATC3_Code end
and COALESCE(b.ATC4_COD,'') = case when tblmkt.ATC4_Code is null then COALESCE(b.ATC4_COD,'') else tblmkt.ATC4_Code end
and COALESCE(b.APP1_COD,'') = case when tblmkt.NFC1_Code is null then COALESCE(b.APP1_COD,'') else tblmkt.NFC1_Code end
and COALESCE(b.APP2_COD,'') = case when tblmkt.NFC2_Code is null then COALESCE(b.APP2_COD,'') else tblmkt.NFC2_Code end
and COALESCE(b.app3_cod,'') = case when tblmkt.NFC3_Code is null then COALESCE(b.app3_cod,'') else tblmkt.NFC3_Code end
and COALESCE(b.stgh_des,'') = case when tblmkt.Strength is null then COALESCE(b.stgh_des,'') else tblmkt.Strength end
and COALESCE(b.corp_cod,'') = case when tblmkt.Corporation_Code is null then COALESCE(b.corp_cod,'') else tblmkt.Corporation_Code end
and COALESCE(b.manu_cod,'') = case when tblmkt.Manufacturer_Code is null then COALESCE(b.manu_cod,'') else tblmkt.Manufacturer_Code end
and right(concat('0000000',nvl(b.product_id,0)),7 ) = case when tblmkt.zk_product_id is null then right(concat('0000000',nvl(b.product_id,0) ),7 ) else right(concat('0000000',tblmkt.zk_product_id),7) end
and right(concat('000000000',COALESCE(b.iqvia_prod_code,0)),9) = case when tblmkt.Product_Code is null then right(concat('000000000',COALESCE(b.iqvia_prod_code,0)),9) else right(concat('000000000', tblmkt.Product_Code),9) end
-- and right(concat('00000',COALESCE(b.iqvia_prod_code,0)),5) = case when tblmkt.Product_Code is null then right(concat('00000',COALESCE(b.iqvia_prod_code,0)),5) else right(concat('00000', tblmkt.Product_Code),5) end
and if(COALESCE(b.iqvia_pack_code,0) REGEXP '^[0-9]',right(concat('000000000000',COALESCE(b.iqvia_pack_code,0)),12),COALESCE(b.iqvia_pack_code,0)) = case when tblmkt.Pack_Code is null then if(COALESCE(b.iqvia_pack_code,0) REGEXP '^[0-9]' ,right(concat('000000000000',COALESCE(b.iqvia_pack_code,0)),12),COALESCE(b.iqvia_pack_code,0)) else if( tblmkt.Pack_Code REGEXP '^[0-9]', right(concat('000000000000',tblmkt.Pack_Code),12),tblmkt.Pack_Code) end
-- and right(concat('0000000',COALESCE(b.iqvia_pack_code,0)),7) = case when tblmkt.Pack_Code is null then right(concat('0000000',COALESCE(b.iqvia_pack_code,0)),7) else right(concat('0000000',tblmkt.Pack_Code),7) end
and right(concat('000000',COALESCE(b.CMPS_COD,0)),6) = case when tblmkt.Molecule_Code is null then right(concat('000000',COALESCE(b.CMPS_COD,0)),6) else right(concat('000000',tblmkt.Molecule_Code),6) end
-- left join dwd.dwd_gnd_ext_retail_dim_ta c
-- on c.market = tblmkt.Market
where tblmkt.market is not null ;
insert overwrite table tmp.tmp_ec_temp_market_del
select
b.iqvia_lineno ,
if( nvl(b.iqvia_pack_code,0) REGEXP '^[0-9]',right(concat('000000000000',nvl(b.iqvia_pack_code,0)),12),nvl(b.iqvia_pack_code,0)) as iqvia_pack_code,-- right(concat('0000000',nvl(b.iqvia_pack_code,0)),7)
right(concat('000000000',nvl(b.iqvia_prod_code,0)),9) as prod_cod ,-- right(concat('00000',nvl(b.iqvia_prod_code,0)),5)
b.iqvia_notes ,
b.new_pack_flag ,
b.is_exists_chpa ,
right(concat('0000000',nvl(b.product_id,0)),7) as product_id,
b.prescription_nature ,
b.medicine_type ,
b.zk_medicine_tier1 ,
b.zk_medicine_tier2 ,
b.zk_medicine_tier3 ,
b.zk_medicine_tier4 ,
b.common_name ,
b.dosage_form ,
b.user_type ,
b.category_name ,
b.product_name ,
b.brand_name ,
b.zk_manu_des ,
b.zk_corp_des ,
b.zk_pack_des ,
b.counting_unit_a ,
b.dosage_unit_a ,
b.unit_a ,
b.app1_cod ,
b.app1_des ,
b.app1_des_c ,
b.app2_cod ,
b.app2_des ,
b.app2_des_c ,
b.app3_cod ,
b.app3_des ,
b.app3_des_c ,
b.atc1_cod ,
b.atc1_des ,
b.atc1_des_c ,
b.atc2_cod ,
b.atc2_des ,
b.atc2_des_c ,
b.atc3_cod ,
b.atc3_des ,
b.atc3_des_c ,
b.atc4_cod ,
b.atc4_des ,
b.atc4_des_c ,
b.bio_desc ,
right(concat('000000',nvl(b.cmps_cod,0 )),6) cmps_cod,
b.cmps_des ,
b.cmps_des_c ,
b.corp_cod ,
b.corp_des ,
b.corp_des_c ,
b.edl_desc ,
b.eth_otc_desc ,
b.gene_orig_desc ,
b.gqce_desc ,
b.manu_cod ,
b.manu_des ,
b.manu_des_c ,
b.mnfl_cod ,
b.mnfl_des ,
b.nrdl_desc ,
b.pack_des ,
b.stgh_des ,
b.pack_lch ,
b.paed_desc ,
b.prod_des ,
b.prod_des_c ,
b.tcm_desc ,
b.vbp_desc ,
b.unit ,
b.counting_unit ,
b.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,
-- c.ta,
if(b.corp_des_c in ('阿斯利康制药集团'),'Y','N') as is_az
from dwd.dwd_gnd_ext_retail_pack_property as b
left join dwd.dwd_ims_td_pack_additional_attribute mole on b.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,pack_desc,strength,product_code,product_desc,molecule_code,molecule_desc,not_in_flag,extend_market,extend_market_ratio,corporation_code,corporation_desc,manufacturer_code,manufacturer_desc,zk_common_name,zk_product_id,zk_dosage_form
from dwd.dwd_gnd_ec_retail_tblmarket_rule
where Extend_Market is null and NOT_IN_FLAG = '0'
) tblmkt
on COALESCE(b.ATC1_COD,'') = case when tblmkt.ATC1_Code is null then COALESCE(b.ATC1_COD,'') else tblmkt.ATC1_Code end
and COALESCE(b.ATC2_COD,'') = case when tblmkt.ATC2_Code is null then COALESCE(b.ATC2_COD,'') else tblmkt.ATC2_Code end
and COALESCE(b.ATC3_COD,'') = case when tblmkt.ATC3_Code is null then COALESCE(b.ATC3_COD,'') else tblmkt.ATC3_Code end
and COALESCE(b.ATC4_COD,'') = case when tblmkt.ATC4_Code is null then COALESCE(b.ATC4_COD,'') else tblmkt.ATC4_Code end
and COALESCE(b.APP1_COD,'') = case when tblmkt.NFC1_Code is null then COALESCE(b.APP1_COD,'') else tblmkt.NFC1_Code end
and COALESCE(b.APP2_COD,'') = case when tblmkt.NFC2_Code is null then COALESCE(b.APP2_COD,'') else tblmkt.NFC2_Code end
and COALESCE(b.app3_cod,'') = case when tblmkt.NFC3_Code is null then COALESCE(b.app3_cod,'') else tblmkt.NFC3_Code end
and COALESCE(b.stgh_des,'') = case when tblmkt.Strength is null then COALESCE(b.stgh_des,'') else tblmkt.Strength end
and COALESCE(b.corp_cod,'') = case when tblmkt.Corporation_Code is null then COALESCE(b.corp_cod,'') else tblmkt.Corporation_Code end
and COALESCE(b.manu_cod,'') = case when tblmkt.Manufacturer_Code is null then COALESCE(b.manu_cod,'') else tblmkt.Manufacturer_Code end
and right(concat('0000000',nvl(b.product_id,0)),7 ) = case when tblmkt.zk_product_id is null then right(concat('0000000',nvl(b.product_id,0) ),7 ) else right(concat('0000000',tblmkt.zk_product_id),7) end
and right(concat('000000000',COALESCE(b.iqvia_prod_code,0)),9) = case when tblmkt.Product_Code is null then right(concat('000000000',COALESCE(b.iqvia_prod_code,0)),9) else right(concat('000000000', tblmkt.Product_Code),9) end
-- and right(concat('00000',COALESCE(b.iqvia_prod_code,0)),5) = case when tblmkt.Product_Code is null then right(concat('00000',COALESCE(b.iqvia_prod_code,0)),5) else right(concat('00000', tblmkt.Product_Code),5) end
and if(COALESCE(b.iqvia_pack_code,0) REGEXP '^[0-9]',right(concat('000000000000',COALESCE(b.iqvia_pack_code,0)),12),COALESCE(b.iqvia_pack_code,0)) = case when tblmkt.Pack_Code is null then if(COALESCE(b.iqvia_pack_code,0) REGEXP '^[0-9]' ,right(concat('000000000000',COALESCE(b.iqvia_pack_code,0)),12),COALESCE(b.iqvia_pack_code,0)) else if( tblmkt.Pack_Code REGEXP '^[0-9]', right(concat('000000000000',tblmkt.Pack_Code),12),tblmkt.Pack_Code) end
-- and right(concat('0000000',COALESCE(b.iqvia_pack_code,0)),7) = case when tblmkt.Pack_Code is null then right(concat('0000000',COALESCE(b.iqvia_pack_code,0)),7) else right(concat('0000000',tblmkt.Pack_Code),7) end
and right(concat('000000',COALESCE(b.CMPS_COD,0)),6) = case when tblmkt.Molecule_Code is null then right(concat('000000',COALESCE(b.CMPS_COD,0)),6) else right(concat('000000',tblmkt.Molecule_Code),6) end
-- left join dwd.dwd_gnd_ext_retail_dim_ta c
-- on c.market = tblmkt.Market
where tblmkt.market is not null ;
MERGE INTO tmp.tmp_ec_temp_market AS t1
USING tmp.tmp_ec_temp_market_del AS t2
on COALESCE(t1.ATC1_COD,'') = COALESCE(t2.ATC1_COD,'')
and COALESCE(t1.ATC2_COD,'') = COALESCE(t2.ATC2_COD,'')
and COALESCE(t1.ATC3_COD,'') = COALESCE(t2.ATC3_COD,'')
and COALESCE(t1.ATC4_COD,'') = COALESCE(t2.ATC4_COD,'')
and COALESCE(t1.APP1_COD,'') = COALESCE(t2.APP1_COD,'')
and COALESCE(t1.APP2_COD,'') = COALESCE(t2.APP2_COD,'')
and COALESCE(t1.APP3_COD,'') = COALESCE(t2.APP3_COD,'')
and COALESCE(t1.CORP_COD,'') = COALESCE(t2.CORP_COD,'')
and COALESCE(t1.MANU_COD,'') = COALESCE(t2.MANU_COD,'')
and COALESCE(t1.PROD_COD,'') = COALESCE(t2.PROD_COD,'')
and COALESCE(t1.iqvia_pack_code,'') = COALESCE(t2.iqvia_pack_code,'')
and COALESCE(t1.STGH_DES,'') = COALESCE(t2.STGH_DES,'')
and COALESCE(t1.CMPS_COD,'') = COALESCE(t2.CMPS_COD,'')
and COALESCE(t1.market,'') = COALESCE(t2.market,'')
WHEN MATCHED THEN DELETE ;
-- COMMAND ----------
--处理 Extend_Market 有值得数据,用已有市场,定义新的市场,带上系数
insert into tmp.tmp_ec_temp_market
select
iqvia_lineno,iqvia_pack_code,prod_cod,iqvia_notes,new_pack_flag,is_exists_chpa,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_name,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_des,corp_des_c,edl_desc,eth_otc_desc,gene_orig_desc,gqce_desc,manu_cod,manu_des,manu_des_c,mnfl_cod,mnfl_des,nrdl_desc,pack_des,stgh_des,pack_lch,paed_desc,prod_des,prod_des_c,tcm_desc,vbp_desc,unit,counting_unit,dosage_unit,NRDL_ENTRY_DATE,t2.Market,t2.bu,nvl(t2.extend_market_ratio,1) as extend_market_ratio,is_az
from tmp.tmp_ec_temp_market t1
left join (select Market,BU,Extend_Market,Extend_Market_Ratio from dwd.dwd_gnd_ec_retail_tblmarket_rule where Extend_Market is not null )t2
on t1.Market = t2.Extend_Market
where t2.Market is not null
-- COMMAND ----------
-----------事实表数据处理获取counting_unit字段给特殊字段补0并对数据去重
insert overwrite table tmp.tmp_ec_source
select
a.time as yyyymm,
right(concat('0000000',a.product_id),7) as pack_id,
if( b.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',b.iqvia_pack_code),12),b.iqvia_pack_code) as iqvia_pack_code, --right(concat('0000000',b.iqvia_pack_code),7)
a.platform,
a.store_name,
a.store_type,
sum(a.sales_amount) as sales_value,
sum(a.sales_qty) as sales_unit,
cast(sum(a.sales_qty*(b.counting_unit/coalesce(unit,1 ))) as decimal(30,10)) as counting_unit
from dwd.dwd_inc_gnd_ext_ec_nationnal_pack_union_all a
left join (
select DISTINCT product_id,iqvia_pack_code,counting_unit,unit,prod_des_c
from dwd.dwd_gnd_ext_retail_pack_property) b
on right(concat('0000000',a.product_id),7) = right(concat('0000000',b.product_id),7)
group by
a.`time`,right(concat('0000000',a.product_id),7)
,if( b.iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',b.iqvia_pack_code),12),b.iqvia_pack_code)
,a.platform,a.store_name
,a.store_type
-- COMMAND ----------
insert overwrite table dws.dws_zk_ec_sales
select
t1.yyyymm,t1.pack_id,t1.iqvia_pack_code,t1.platform,t1.store_name,t1.store_type,
sum(sales_value) as sales_value,
sum(sales_unit) as sales_unit,
sum(counting_unit) as counting_unit
from tmp.tmp_ec_source t1
group by
t1.yyyymm,t1.pack_id,t1.iqvia_pack_code,t1.platform,t1.store_name,t1.store_type
-- COMMAND ----------
-------------------------处理top_ta_corp数据
insert overwrite table dws.dws_zk_ec_by_ta_top_corp
select
t1.yyyymm,
t1.ta,
t1.rank,
t1.corp_des,
t1.CORP_COD,
t1.platform,
t1.store_type,
sum(t1.sales_value) sales_value
from (
select
a.ytd yyyymm,
a.ta,
a.rank,
a.corp_des,
b.CORP_COD,
a.platform,
a.store_type,
replace(a.sales_value,',','') as sales_value
from dwd.dwd_gnd_ec_ta_overview a
left join (select distinct ZK_Corp_C ,CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total) b
on b.ZK_Corp_C = a.corp_des
) t1
group by 1,2,3,4,5,6,7
-- COMMAND ----------
-----加工倒减的by_corp_by_ta_by_total表数据获取去年数据
insert overwrite table dws.dws_zk_ec_by_ta_top_corp
select
t1.yyyymm,
t1.ta,
t1.rank,
t1.corp_des,
t1.CORP_COD,
t1.platform,
t1.store_type,
sum(t1.sales_value) sales_value
from(
select
a.ytd yyyymm,
a.ta,
a.rank,
a.corp_des,
b.CORP_COD,
a.platform,
a.store_type,
replace(a.sales_value,',','') as sales_value
from dwd.dwd_gnd_ec_ta_overview a
left join (select distinct ZK_Corp_C ,CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total) b
on b.ZK_Corp_C = a.corp_des
)t1
group by 1,2,3,4,5,6,7
-- COMMAND ----------
----倒减需要用到公司信息以及ta信息加工配置表挂靠Corp、ta
insert overwrite table tmp.tmp_zk_ec_pack_property_corp
select distinct a.product_id,a.iqvia_pack_code,a.Market,ta.ta
,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 product_id,iqvia_pack_code,Market,corp_cod,corp_des,corp_des_c,manu_cod,manu_des,manu_des_c,prod_cod from tmp.tmp_ec_temp_market) a
left join dwd.dwd_gnd_ext_retail_dim_ta ta
on a.Market = ta.market
left join (select right(concat('000000000',prod_cod ),9) prod_cod,corp_cod,corp_des,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,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 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 ----------
-------------------------------------------------------ec倒减-------------------------------------------------------
--注意事项:
-- 只做了CV、CVRM、TA大数的倒减其余TA没有做
--cv倒减关联加工好的配置表获取ta信息进行倒减
insert overwrite table tmp.tmp_dm_zk_ec_sales_cv_tmp
with cv_data as (
select
t1.yyyymm,
t1.pack_id,
t1.iqvia_pack_code,
t1.platform,
t1.store_type,
t1.store_name,
t1.sales_value,
t1.sales_unit,
t1.counting_unit,
max(t2.ta) ta
from dws.dws_zk_ec_sales t1
left join (select distinct iqvia_pack_code,ta from tmp.tmp_zk_ec_pack_property_corp) t2
on t1.iqvia_pack_code = t2.iqvia_pack_code
where t2.ta = 'CV'
group by 1,2,3,4,5,6,7,8,9
),cv_corp_data as (
-----------------------cv下的az 数据
select
A.yyyymm,
A.pack_id,
A.iqvia_pack_code,
A.platform,
A.store_type,
A.store_name,
B.corp_cod,
A.sales_value,
A.sales_unit,
A.counting_unit,
A.ta
from cv_data A
inner join(select distinct iqvia_pack_code,corp_cod,product_id from tmp.tmp_zk_ec_pack_property_corp) B
on A.pack_id = B.product_id
where B.corp_cod = 'A5Z'
union all
-----------------------cv下的az others数据
select
cv_az.yyyymm,
'CV_AZ_OTHERS' as pack_id,
'CV_AZ_OTHERS' as iqvia_pack_code,
cv_az.platform,
cv_az.store_type,
case when cv_az.store_type = '自营旗舰' then 'CV_AZ_OTHERS_SO' when cv_az.store_type = '第三方' then 'CV_AZ_OTHERS_POP' end store_name,
cv_az.corp_cod,
cv_az_total.sales_value - cv_az.sales_value as sales_value,
0,0,
'CV_AZ_OTHERS' as ta
from (
select
A.yyyymm,
B.corp_cod,
A.platform,
case when A.store_type = '平台店' or A.store_type = '品牌店' then '自营旗舰' else A.store_type end store_type,
sum(A.sales_value) sales_value
from cv_data A
inner join(select distinct iqvia_pack_code,corp_cod,product_id from tmp.tmp_zk_ec_pack_property_corp) B
on A.pack_id = B.product_id
where B.corp_cod = 'A5Z'
group by 1,2,3,4
)cv_az
left join(
select
yyyymm,
CORP_COD,
platform,
case when store_type = '三方' then '第三方' else store_type end store_type,
sum(sales_value) sales_value
from dws.dws_zk_ec_by_ta_top_corp
where ta = 'CV' and CORP_COD = 'A5Z'
group by 1,2,3,4
)cv_az_total
on cv_az.yyyymm = cv_az_total.yyyymm
and cv_az.corp_cod = cv_az_total.CORP_COD
and cv_az.platform = cv_az_total.platform
and cv_az.store_type = cv_az_total.store_type
union all
---------------cv下的非az数据
select
A.yyyymm,
A.pack_id,
A.iqvia_pack_code,
A.platform,
A.store_type,
A.store_name,
B.corp_cod,
A.sales_value,
A.sales_unit,
A.counting_unit,
A.ta
from cv_data A
inner join(select distinct iqvia_pack_code,corp_cod,product_id from tmp.tmp_zk_ec_pack_property_corp) B
on A.pack_id = B.product_id
where nvl(corp_cod,'') <>'A5Z'
)
-----------------------所有cv 数据
,all_cv_data as (
select
yyyymm,
pack_id,
iqvia_pack_code,
platform,
store_type,
store_name,
corp_cod,
sales_value,
sales_unit,
counting_unit,
'CV' flag,
ta
from cv_corp_data
union all
-----------------------倒减出cv_others数据
select
t1.yyyymm,
'CV_OTHERS' as pack_id,
'CV_OTHERS' as iqvia_pack_code,
t1.platform,
t1.store_type,
case when t1.store_type = '自营旗舰' then 'CV_OTHERS_SO' when t1.store_type = '第三方' then 'CV_OTHERS_POP' end store_name,
'CV_OTHERS' as corp_cod,
t2.sales_value - t1.sales_value as sales_value,
0,0,
'CV' as flag,
'CV_OTHERS' as ta
from(
-----------------------所有的cv数据
select
yyyymm,
platform,
case when store_type = '平台店' or store_type = '品牌店' then '自营旗舰' else store_type end store_type,
sum(sales_value) sales_value
from cv_corp_data
group by 1,2,3
)t1
left join(
-----------------------cv大数
select
yyyymm,
platform,
case when store_type = '三方' then '第三方' else store_type end store_type,
sum(sales_value) sales_value
from dws.dws_zk_ec_by_ta_top_corp
where ta = 'CV' and lower(rank) = 'total'
group by 1,2,3
)t2
on t1.yyyymm = t2.yyyymm and t1.platform = t2.platform and t1.store_type = t2.store_type
),non_cv_all_data as (
select
t1.yyyymm,
t1.pack_id,
t1.iqvia_pack_code,
t1.platform,
t1.store_type,
t1.store_name,
t2.corp_cod,
t1.sales_value,
t1.sales_unit,
t1.counting_unit,
case when t2.ta in ('DM','RD') then 'CVRM' else '' end flag,
max(t2.ta) ta
from dws.dws_zk_ec_sales t1
left join (select distinct iqvia_pack_code,ta,corp_cod,product_id from tmp.tmp_zk_ec_pack_property_corp) t2
on t1.pack_id = t2.product_id
where nvl(t2.TA,'') <> 'CV'
group by 1,2,3,4,5,6,7,8,9,10,11
)
select * from all_cv_data
union
select * from non_cv_all_data
-- select * from non_cv_all_data order by sales_value desc
-- COMMAND ----------
--cvrm倒减关联加工好的配置表获取ta信息进行倒减
insert overwrite table tmp.tmp_dm_zk_ec_sales_cv_tmp2
with all_data as (
select
yyyymm,
pack_id,
iqvia_pack_code,
platform,
store_type,
store_name,
corp_cod,
sales_value,
sales_unit,
counting_unit,
flag
from tmp.tmp_dm_zk_ec_sales_cv_tmp
union all
select
t1.yyyymm,
'CVRM_AZ_OTHERS' as pack_id,
'CVRM_AZ_OTHERS' as iqvia_pack_code,
t1.platform,
t1.store_type,
case when t1.store_type = '自营旗舰' then 'CVRM_AZ_OTHERS_SO' when t1.store_type = '第三方' then 'CVRM_AZ_OTHERS_POP' end store_name,
t1.corp_cod,
t2.sales_value - t1.sales_value as sales_value,
0,0,
'CVRM' as flag
from(
select
yyyymm,
corp_cod,
platform,
case when store_type = '平台店' or store_type = '品牌店' then '自营旗舰' else store_type end store_type,
sum(sales_value) sales_value
from tmp.tmp_dm_zk_ec_sales_cv_tmp
where corp_cod = 'A5Z' and flag in ('CV','CVRM')
group by 1,2,3,4
)t1
left join (
select
yyyymm,
CORP_COD,
platform,
case when store_type = '三方' then '第三方' else store_type end store_type,
sum(sales_value) sales_value
from dws.dws_zk_ec_by_ta_top_corp
where CORP_COD = 'A5Z' and ta = 'CVRM'
group by 1,2,3,4
)t2
on t1.yyyymm = t2.yyyymm and t1.platform = t2.platform and t1.store_type = t2.store_type
)
select
yyyymm,
pack_id,
iqvia_pack_code,
platform,
store_type,
store_name,
corp_cod,
sales_value,
sales_unit,
counting_unit,
flag
from all_data
union all
select
t1.yyyymm,
'CVRM_OTHERS' as pack_id,
'CVRM_OTHERS' as iqvia_pack_code,
t1.platform,
t1.store_type,
case when t1.store_type = '自营旗舰' then 'CVRM_OTHERS_SO' when t1.store_type = '第三方' then 'CVRM_OTHERS_POP' end store_name,
'CVRM_OTHERS' as corp_cod,
t2.sales_value - t1.sales_value as sales_value,
0,0,
'CVRM' flag
from (
select
yyyymm,
platform,
case when store_type = '平台店' or store_type = '品牌店' then '自营旗舰' else store_type end store_type,
sum(sales_value) sales_value
from all_data
where flag in ('CV','CVRM')
group by 1,2,3
)t1
left join(
select
yyyymm,
platform,
case when store_type = '三方' then '第三方' else store_type end store_type,
sum(sales_value) sales_value
from dws.dws_zk_ec_by_ta_top_corp
where ta = 'CVRM' and lower(rank) = 'total'
group by 1,2,3
)t2
on t1.yyyymm = t2.yyyymm and t1.platform = t2.platform and t1.store_type = t2.store_type
-- COMMAND ----------
--ta倒减并写入事实表根据加工好的倒减底表中ta字段信息获取ta总数进行倒减
insert overwrite table tmp.tmp_dm_zk_ec_sales
with all_data as (
select distinct
yyyymm,
pack_id,
iqvia_pack_code,
platform,
store_type,
store_name,
corp_cod,
sales_value,
sales_unit,
counting_unit,
flag
from tmp.tmp_dm_zk_ec_sales_cv_tmp2
union all
select
t1.yyyymm,
'OTHERS_AZ_TA' as pack_id,
'OTHERS_AZ_TA' as iqvia_pack_code,
t1.platform,
t1.store_type,
case when t1.store_type = '自营旗舰' then 'OTHERS_AZ_TA_SO' when t1.store_type = '第三方' then 'OTHERS_AZ_TA_POP' end store_name,
t1.corp_cod,
t2.sales_value - t1.sales_value as sales_value,
0,0,
'OTHERS_AZ_TA' as flag
from(
select
yyyymm,
corp_cod,
platform,
case when store_type = '平台店' or store_type = '品牌店' then '自营旗舰' else store_type end store_type,
sum(sales_value) sales_value
from tmp.tmp_dm_zk_ec_sales_cv_tmp2
where corp_cod = 'A5Z'
group by 1,2,3,4
)t1
left join (
select
yyyymm,
platform,
store_type,
sum(sales_value) sales_value
from dws.dws_zk_ec_by_ta_top_corp
where CORP_COD = 'A5Z' and ta = 'RX Total'
group by 1,2,3
)t2
on t1.yyyymm = t2.yyyymm and t1.platform = t2.platform and t1.store_type = t2.store_type
),result as (
select
yyyymm,
pack_id,
iqvia_pack_code,
platform,
store_type,
store_name,
corp_cod,
sales_value,
sales_unit,
counting_unit,
flag
from all_data
union all
select
t1.yyyymm,
'OTHERS_TA' as pack_id,
'OTHERS_TA' as iqvia_pack_code,
t1.platform,
t1.store_type,
case when t1.store_type = '自营旗舰' then 'OTHERS_TA_SO' when t1.store_type = '第三方' then 'OTHERS_TA_POP' end store_name,
'OTHERS_TA' as corp_cod,
t2.sales_value - t1.sales_value as sales_value,
0,0,
'OTHERS_TA' as flag
from(
select
yyyymm,
platform,
case when store_type = '平台店' or store_type = '品牌店' then '自营旗舰' else store_type end store_type,
sum(sales_value) sales_value
from all_data
group by 1,2,3
)t1
left join(
select
yyyymm,
platform,
store_type,
sum(sales_value) sales_value
from dws.dws_zk_ec_by_ta_top_corp
where lower(rank) = 'total' and ta = 'RX Total'
group by 1,2,3
)t2
on t1.yyyymm = t2.yyyymm and t1.platform = t2.platform and t1.store_type = t2.store_type
)
select distinct
yyyymm,
pack_id,
iqvia_pack_code,
corp_cod,
'ROC' as audit_cod,
platform,
case when store_type = '第三方' then 'POP' when store_type in ('平台店','品牌店','自营旗舰') then 'SO' end store_type,
store_name,
case when iqvia_pack_code like '%OTHERS%' then 0 else 1 end PACK_FLAG,
case when iqvia_pack_code like '%OTHERS%' then 0 else 1 end PROD_FLAG,
flag,
'EC(Monthly)' DATA_SOURCE,
sum(sales_value) sales_value,
sum(sales_unit) sales_unit,
sum(counting_unit) counting_unit
from result
group by 1,2,3,4,5,6,7,8,9,10,11,12
-- COMMAND ----------
insert overwrite table dm.dm_zk_ec_sales
select
yyyymm,
max(pack_id) as pack_id,
iqvia_pack_code,
corp_cod,
audit_cod,
platform,
store_type,
store_name,
max(PACK_FLAG) as PACK_FLAG,
max(PROD_FLAG) as PROD_FLAG,
max(flag) as flag,
DATA_SOURCE,
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
from
(
select
yyyymm,
pack_id,
iqvia_pack_code,
corp_cod,
audit_cod,
platform,
store_type,
store_name,
PACK_FLAG,
PROD_FLAG,
flag,
DATA_SOURCE,
sales_value,
0 as sales_value_ly,
sales_unit,
0 as sales_unit_ly,
counting_unit,
0 as counting_unit_ly
from tmp.tmp_dm_zk_ec_sales
union all
select
cast(yyyymm + 100 as int) as yyyymm,
null as pack_id,
iqvia_pack_code,
corp_cod,
audit_cod,
platform,
store_type,
store_name,
case when upper(iqvia_pack_code) like '%OTHERS%' then 0 else 1 end as PACK_FLAG,
case when upper(iqvia_pack_code) like '%OTHERS%' then 0 else 1 end as PROD_FLAG,
'' as flag,
DATA_SOURCE,
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
from tmp.tmp_dm_zk_ec_sales
where cast(yyyymm + 100 as int) <= (select max(yyyymm) from tmp.tmp_dm_zk_ec_sales)
) t
group by yyyymm,
iqvia_pack_code,
corp_cod,
audit_cod,
platform,
store_type,
store_name,
DATA_SOURCE
-- COMMAND ----------
insert overwrite table dws.dws_zk_ec_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_ec_temp_market
union all
select
'' iqvia_lineno
,iqvia_pack_code as iqvia_pack_code
,'' prod_cod
,'' iqvia_notes
,'' new_pack_flag
,'' is_exists_chpa
,iqvia_pack_code as 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_name
,'' 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 as 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 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_ec_sales where iqvia_pack_code not in (select distinct iqvia_pack_code from tmp.tmp_ec_temp_market))
-- COMMAND ----------
insert overwrite table tmp.tmp_dm_zk_ec_pack_property
select
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
,a.Market
,a.bu
,a.extend_market_ratio
,a.is_az
from dws.dws_zk_ec_pack_property a
left join (select right(concat('000000000',prod_cod ),9) prod_cod,corp_cod,corp_des,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,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 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 ----------
------将事实表倒减之后的维度信息补充到维度表里
insert overwrite table dm.dm_zk_ec_pack_property
select
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_lineno
,a.iqvia_pack_code
,a.prod_cod
,a.iqvia_notes
,a.new_pack_flag
,a.is_exists_chpa
,'' Family_Code
,'' Family_Name
,'' BrandType
,'' AZ_MAIN
,'' AZ_Related
,t5.KEY_COMPETITOR
,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
,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'
WHEN a.corp_des like '%GROUP%' THEN replace(a.corp_des,' GROUP','')
else a.CORP_DES END corp_des
,CASE WHEN a.iqvia_pack_code LIKE '%AZ%' THEN '阿斯利康制药集团' else a.CORP_DES_C END
CORP_DES_C
,a.edl_desc
,a.eth_otc_desc
,a.gene_orig_desc
,a.gqce_desc
,a.manu_cod
,a.manu_des
,a.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.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
,a.bu
,a.extend_market_ratio
,a.is_az
,nvl(b.class, 'Others') class
,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 c.ta
END TA
from tmp.tmp_dm_zk_ec_pack_property a
left join dwd.dwd_gnd_ext_retail_dim_ta c
on c.market = a.Market
left join dwd.dwd_gnd_tblclass b
on a.Market = case when b.market is null then a.Market else b.market end
and nvl(a.iqvia_pack_code,'') = case when b.pack_code is null then nvl(a.iqvia_pack_code,'') else b.pack_code end
and nvl(a.prod_cod,'') = case when b.product_code is null then nvl(a.prod_cod,'') else b.product_code end
and nvl(a.cmps_cod,'') = case when b.molecule_code is null then nvl(a.cmps_cod,'') else right(concat('000000',b.molecule_code),6) end
and nvl(a.corp_cod,'') = case when b.corporation_code is null then nvl(a.corp_cod,'') else b.corporation_code end
and nvl(a.manu_cod,'') = case when b.manufacturer_code is null then nvl(a.manu_cod,'') else b.manufacturer_code end
and nvl(a.app1_cod,'') = case when b.nfc1_code is null then nvl(a.app1_cod,'') else b.nfc1_code end
and nvl(a.app2_cod,'') = case when b.nfc2_code is null then nvl(a.app2_cod,'') else b.nfc2_code end
and nvl(a.app3_cod,'') = case when b.nfc3_code is null then nvl(a.app3_cod,'') else b.nfc3_code end
and nvl(a.atc1_cod,'') = case when b.atc1_code is null then nvl(a.atc1_cod,'') else b.atc1_code end
and nvl(a.atc2_cod,'') = case when b.atc2_code is null then nvl(a.atc2_cod,'') else b.atc2_code end
and nvl(a.atc3_cod,'') = case when b.atc3_code is null then nvl(a.atc3_cod,'') else b.atc3_code end
and nvl(a.atc4_cod,'') = case when b.atc4_code is null then nvl(a.atc4_cod,'') else b.atc4_code end
and nvl(a.stgh_des,'') = case when b.strength is null then nvl(a.stgh_des,'') else b.strength end
left join (select distinct MARKET,PACK_CODE as PACK_COD,KEY_COMPETITOR from DM.DM_TD_EXT_CHPA_MARKET_PACK_MAPPING) t5
on a.market = t5.MARKET
and a.iqvia_pack_code = t5.PACK_COD
-- COMMAND ----------
insert overwrite table dm.dm_zk_ec_market_property
select distinct
iqvia_pack_code,market,ta
from dm.dm_zk_ec_pack_property
-- COMMAND ----------
insert overwrite table dm.dm_zk_ec_market_ratio
select distinct
concat(a.Market,'_',a.iqvia_pack_code) as market_pack_key,
a.iqvia_pack_code as pack_cod,
a.prod_des,
a.cmps_des,
a.corp_cod,
a.Market,
a.extend_market_ratio as value_market_ratio,
a.extend_market_ratio as unit_market_ratio,
coalesce(
case when c.ratio is null then a.extend_market_ratio*d.ratio
else a.extend_market_ratio*c.ratio end,1) as countingunit_market_ratio
from tmp.tmp_ec_temp_market a
left join (--pack
select
market,
cmps_cod,
cmps_des,
if( pack_cod REGEXP '^[0-9]',right(concat('000000000000',pack_cod),12),pack_cod) pack_cod,--right(concat('0000000',pack_cod ),7)
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'
)c on a.Market = c.market and a.iqvia_pack_code = if( c.pack_cod REGEXP '^[0-9]',right(concat('000000000000',c.pack_cod),12),c.pack_cod)--right(concat('0000000',c.pack_cod),7)
left join(--molecule
select
market,
cmps_cod,
cmps_des,
if( pack_cod REGEXP '^[0-9]',right(concat('000000000000',pack_cod),12),pack_cod) pack_cod,--right(concat('0000000',pack_cod ),7)
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'
)d on a.Market = d.market and a.cmps_cod = right(concat('000000',d.cmps_cod ),6)
-- COMMAND ----------
create or replace table dm.dm_zk_ec_geo
using delta as
select
'ROC' as audit_cod,
'全国' as city_c,
'national' as city_e,
'全国' as province_c,
'national' as province_e;
-- COMMAND ----------
---------------------------------------将直取部分数据处理成KPI形式-----------------------------------------
insert overwrite table tmp.tmp_zk_ec_sales_kpi_tmp
select
b.CORP_COD,
'' as yyyymm,
a.ytd ,
a.platform,
case when a.store_type = '三方' then '第三方' else a.store_type end store_type ,
'ROC' as geo_key,
'top_yoygr' as KPI_name,
a.yoy_gr as Kpi_val,
c.yoy_gr as Kpi_val_ly,
'' as str_kpi_val,
'' as str_kpi_val_ly,
'' as str_kpi_vol,
'' as str_kpi_vol_ly,
case when upper(a.rank) = 'TTL MKT' then 'Y' else 'N' end total_flag,
'' as ta
from dwd.dwd_gnd_ec_b2c_top10_drives_brand a
left join (select distinct ZK_Corp_C,CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total where (ZK_Corp_C = '东阳光' and ZK_Prod_C is null) or ZK_Corp_C != '东阳光') b
on a.corp = b.ZK_Corp_C
left join dwd.dwd_gnd_ec_b2c_top10_drives_brand c
on a.corp = c.corp
and a.ytd = c.ytd + 100
and a.platform = c.platform
and a.store_type = c.store_type
-- where upper(a.rank) <> 'TTL MKT'
union all
select
b.CORP_COD,
'' as yyyymm,
a.ytd ,
a.platform,
case when a.store_type = '三方' then '第三方' else a.store_type end store_type ,
'ROC' as geo_key,
'top1_brand_gr' as KPI_name,
a.top1_brand_gr as Kpi_val,
c.top1_brand_gr as Kpi_val_ly,
'' as str_kpi_val,
'' as str_kpi_val_ly,
'' as str_kpi_vol,
'' as str_kpi_vol_ly,
case when upper(a.rank) = 'TTL MKT' then 'Y' else 'N' end total_flag,
'' as ta
from dwd.dwd_gnd_ec_b2c_top10_drives_brand a
left join (select distinct ZK_Corp_C,CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total where (ZK_Corp_C = '东阳光' and ZK_Prod_C is null) or ZK_Corp_C != '东阳光') b
on a.corp = b.ZK_Corp_C
left join dwd.dwd_gnd_ec_b2c_top10_drives_brand c
on a.corp = c.corp
and a.ytd = c.ytd + 100
and a.platform = c.platform
and a.store_type = c.store_type
-- where upper(a.rank) <> 'TTL MKT'
union all
select
b.CORP_COD,
'' as yyyymm,
a.ytd ,
a.platform,
case when a.store_type = '三方' then '第三方' else a.store_type end store_type,
'ROC' as geo_key,
'top1_brand_val' as KPI_name,
'' as Kpi_val,
'' as Kpi_val_ly,
a.top1_brand_val as str_kpi_val,
'' as str_kpi_val_ly,
'' as str_kpi_vol,
'' as str_kpi_vol_ly,
case when upper(a.rank) = 'TTL MKT' then 'Y' else 'N' end total_flag,
'' as ta
from dwd.dwd_gnd_ec_b2c_top10_drives_brand a
left join (select distinct ZK_Corp_C,CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total where (ZK_Corp_C = '东阳光' and ZK_Prod_C is null) or ZK_Corp_C != '东阳光') b
on a.corp = b.ZK_Corp_C
-- where upper(a.rank) <> 'TTL MKT'
union all
select
b.CORP_COD,
'' as yyyymm,
a.ytd ,
a.platform,
case when a.store_type = '三方' then '第三方' else a.store_type end store_type,
'ROC' as geo_key,
'incremental_brand' as KPI_name,
'' as Kpi_val,
'' as Kpi_val_ly,
a.growth_driver_brand_val as str_kpi_val,
'' as str_kpi_val_ly,
'' as str_kpi_vol,
'' as str_kpi_vol_ly,
case when upper(a.rank) = 'TTL MKT' then 'Y' else 'N' end total_flag,
'' as ta
from dwd.dwd_gnd_ec_b2c_top10_drives_brand a
left join (select distinct ZK_Corp_C,CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total where (ZK_Corp_C = '东阳光' and ZK_Prod_C is null) or ZK_Corp_C != '东阳光') b
on a.corp = b.ZK_Corp_C
-- where upper(a.rank) <> 'TTL MKT'
union all
select
b.CORP_COD,
'' as yyyymm,
a.ytd ,
a.platform,
case when a.store_type = '三方' then '第三方' else a.store_type end store_type,
'ROC' as geo_key,
'incremental_brand_gr' as KPI_name,
a.growth_driver_brand_gr as Kpi_val,
c.growth_driver_brand_gr as Kpi_val_ly,
'' as str_kpi_val,
'' as str_kpi_val_ly,
'' as str_kpi_vol,
'' as str_kpi_vol_ly,
case when upper(a.rank) = 'TTL MKT' then 'Y' else 'N' end total_flag,
'' as ta
from dwd.dwd_gnd_ec_b2c_top10_drives_brand a
left join (select distinct ZK_Corp_C,CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total where (ZK_Corp_C = '东阳光' and ZK_Prod_C is null) or ZK_Corp_C != '东阳光') b
on a.corp = b.ZK_Corp_C
left join dwd.dwd_gnd_ec_b2c_top10_drives_brand c
on a.corp = c.corp
and a.ytd = c.ytd + 100
and a.platform = c.platform
and a.store_type = c.store_type
-- where upper(a.rank) <> 'TTL MKT'
union all
select
b.CORP_COD,
a.ytd yyyymm,
'' as ytd,
a.platform,
a.store_type,
'ROC' as geo_key,
'top_corp_sales' as KPI_name,
replace(a.sales_value,',','' ) as Kpi_val,
replace(c.sales_value,',','' ) as Kpi_val_ly,
'' as str_kpi_val,
'' as str_kpi_val_ly,
'' as str_kpi_vol,
'' as str_kpi_vol_ly,
case when lower(a.rank) = 'total' then 'Y' else 'N' end total_flag,
a.ta
from dwd.dwd_gnd_ec_ta_overview a
left join (select distinct ZK_Corp_C,CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total where (ZK_Corp_C = '东阳光' and ZK_Prod_C is null) or ZK_Corp_C != '东阳光') b
on a.corp_des = b.ZK_Corp_C
left join dwd.dwd_gnd_ec_ta_overview c
on a.ytd = c.ytd + 100
and a.corp_des = c.corp_des
and a.platform = c.platform
and a.store_type = c.store_type
and a.ta = c.ta
-- where lower(a.rank) <> 'total'
union all
select distinct
b.CORP_COD,
'' yyyymm,
a.ytd as ytd,
a.platform,
a.store_type,
'ROC' as geo_key,
'top_rank' as KPI_name,
a.rank as Kpi_val,
'' as Kpi_val_ly,
'' as str_kpi_val,
'' as str_kpi_val_ly,
'' as str_kpi_vol,
'' as str_kpi_vol_ly,
case when lower(a.rank) = 'total' then 'Y' else 'N' end total_flag,
a.ta
from dwd.dwd_gnd_ec_ta_overview a
left join (select distinct ZK_Corp_C,CORP_COD from dwd.dwd_inc_gnd_retail_b2c_label_total where (ZK_Corp_C = '东阳光' and ZK_Prod_C is null) or ZK_Corp_C != '东阳光') b
on a.corp_des = b.ZK_Corp_C
-- where lower(a.rank) <> 'total'
-- COMMAND ----------
insert overwrite table dm.dm_zk_ec_sales_kpi
select
case when total_flag = 'Y' then 'TTL MKT' else nvl(CORP_COD,'') end CORP_COD ,
if(yyyymm <> '', nvl(yyyymm,''),nvl(ytd,'')) yyyymm,
if(ytd <> '', 'Y', 'N') ytd,
nvl(platform,'') as platform,
case when store_type = '自营旗舰' then 'SO'
when store_type = '第三方' then 'POP' else nvl(store_type,'') end store_type,
nvl(geo_key,'') as geo_key,
nvl(KPI_name,'') as KPI_name,
nvl(cast(Kpi_val as double),'') Kpi_val,
nvl(cast(Kpi_val_ly as double),'') Kpi_val_ly,
nvl(str_kpi_val,'') as str_kpi_val,
nvl(str_kpi_val_ly,'') as str_kpi_val_ly,
nvl(str_kpi_vol,'') as str_kpi_vol,
nvl(str_kpi_vol_ly,'') as str_kpi_vol_ly,
nvl(total_flag,'') as total_flag,
case when ta = 'NIAD' then 'DM' when ta = 'RX Total' then '' else nvl(ta,'') end ta
from tmp.tmp_zk_ec_sales_kpi_tmp
-- COMMAND ----------
-- MAGIC %md
-- MAGIC ## NIAD Date
-- COMMAND ----------
-- 计算NIAD的时间是否比其他数据源先来数据 chenwu
CREATE OR REPLACE TABLE tmp.tmp_dm_td_ext_ec_niad_month as
WITH all_ec_data (
SELECT
MAX(TIME) month,
'NOTNIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_hm_jd_2025before
UNION ALL
SELECT
MAX(TIME) month,
'NOTNIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_hm_jd_2025after
UNION ALL
SELECT
MAX(TIME) month,
'NOTNIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_hm_tm_2206after
UNION ALL
SELECT
MAX(TIME) month,
'NOTNIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_hm_tm_2206before
UNION ALL
SELECT
MAX(TIME) month,
'NOTNIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_gastritis_23after
UNION ALL
SELECT
MAX(TIME) month,
'NOTNIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_gastritis_23before
UNION ALL
SELECT
MAX(TIME) month,
'NOTNIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_re
UNION ALL
SELECT
MAX(TIME) month,
'NOTNIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_dm_statins_xzk
UNION ALL
SELECT
MAX(TIME) month,
'NOTNIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_dm_23before
UNION ALL
SELECT
MAX(TIME) month,
'NIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_dm_23after
UNION ALL
SELECT
MAX(TIME) month,
'NOTNIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_hm_tm_2403after
UNION ALL
SELECT
MAX(TIME) month,
'NIAD' AS DATATYPE
FROM
dwd.dwd_gnd_ec_zkb2c_dm_26after
)
SELECT
MAX(t1.month) as NIAD_MONTH,
max(t2.month) OTHERS_MONTH
FROM
all_ec_data t1
LEFT JOIN (
SELECT
MAX(month) as month
FROM
all_ec_data
WHERE
datatype = 'NOTNIAD'
) t2
WHERE
t1.datatype = 'NIAD'
-- COMMAND ----------
select * from tmp.tmp_dm_td_ext_ec_niad_month