Add EXTERNAL SQL scripts
This commit is contained in:
228
EXTERNAL/Merged_Data/Merged_Data_Config_table_bkp.sql
Normal file
228
EXTERNAL/Merged_Data/Merged_Data_Config_table_bkp.sql
Normal file
@@ -0,0 +1,228 @@
|
||||
-- Databricks notebook source
|
||||
create or replace temporary view brand_province_config -- brand配置表
|
||||
as
|
||||
select upper(`brand`) as brand, `province` as province, datasource as data_source from dwd.dwd_gnd_merge_data_brand_province
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
create or replace temporary view market_province_config --market 配置表
|
||||
as
|
||||
select market, province, datasource as data_source from dwd.dwd_gnd_merge_data_brand_market
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
insert overwrite dws.dws_external_merge_data_config_special
|
||||
-- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DWS/dws_external_merge_data_config_special' --prd
|
||||
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DWS/dws_external_merge_data_config_special' --test
|
||||
with pack_and_brand ( --brand 转 pack
|
||||
select
|
||||
upper(brandnamee) brand_name
|
||||
,ims_pack_cod PACK_COD --取所有code带出market
|
||||
from dwd.dwd_td_imscode_azcode
|
||||
where ims_pack_cod is not null and startswith(ims_pack_cod,'0')
|
||||
and upper(brandnamee) in (select brand from brand_province_config group by 1 )--取出在配置表中的pack
|
||||
group by 1,2
|
||||
)
|
||||
,pack_to_market (
|
||||
select
|
||||
a.PACK_COD
|
||||
,MARKET
|
||||
from dm.dm_td_external_market_pack_mapping a
|
||||
inner join pack_and_brand b on a.PACK_COD = b.pack_cod --取出配置表中的brand对应的 market
|
||||
where DATA_SOURCE in ( 'AIA(Monthly)')--只取 AIA中的市场,chpa的市场太多
|
||||
and lower(a.MARKET) not like '%all market%'
|
||||
and market not in (
|
||||
'RE Extended Market'
|
||||
,'RE Market'
|
||||
,'RE Market (Global)'
|
||||
,'Pulmicort VBP Benchmark Market'
|
||||
,'Respules Market'
|
||||
)
|
||||
group by 1,2
|
||||
)
|
||||
,market_to_pack ( -- 获取上述market中所有的pack
|
||||
select
|
||||
market
|
||||
,pack_cod
|
||||
from dm.dm_td_external_market_pack_mapping a
|
||||
where DATA_SOURCE in ( 'AIA(Monthly)','IQVIA-CHPA(Monthly)')--AIA和chpa的市场
|
||||
and market in (select market from pack_to_market group by 1)
|
||||
group by 1,2
|
||||
)
|
||||
,province_to_audit (
|
||||
select
|
||||
PROVINCE_C
|
||||
,AUDIT_COD
|
||||
,DATA_SOURCE
|
||||
,CASE WHEN DATA_SOURCE = 'AIA(Monthly)' THEN 'AIA'
|
||||
WHEN DATA_SOURCE = 'IQVIA-CHPA(Monthly)' THEN 'CHPA'
|
||||
ELSE NULL
|
||||
END AS SOURCENAME
|
||||
from dm.dm_td_external_geo
|
||||
where DATA_SOURCE in ( 'AIA(Monthly)','IQVIA-CHPA(Monthly)')
|
||||
group by 1,2,3
|
||||
)
|
||||
,not_selected_datasource (--因为pack2market2pack 会导致部分大市场包含所有的pack,故用配置表再删除一些不需要配置数据源的
|
||||
select
|
||||
a.ims_pack_cod PACK_COD --取所有code
|
||||
,b.province
|
||||
from dwd.dwd_td_imscode_azcode a
|
||||
inner join (
|
||||
select
|
||||
UPPER(brand) as brand
|
||||
,province
|
||||
from brand_province_config
|
||||
where data_source = '/'
|
||||
group by 1,2
|
||||
) b
|
||||
on upper(a.brandnamee) = upper(b.brand )
|
||||
where a.ims_pack_cod is not null and startswith(a.ims_pack_cod,'0')
|
||||
group by 1,2
|
||||
union
|
||||
select
|
||||
pack_cod,
|
||||
'天津'
|
||||
from market_to_pack
|
||||
where market= 'EGFR TKI Market'
|
||||
group by 1
|
||||
)
|
||||
select DATA_SOURCE,PACK_COD,AUDIT_COD
|
||||
from not_selected_datasource a
|
||||
left join province_to_audit b on a.province = b.province_c
|
||||
group by 1,2,3
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
INSERT OVERWRITE dws.dws_external_merge_data_config
|
||||
-- create or replace table dws.dws_external_merge_data_config
|
||||
/*
|
||||
1通过brand找到pack,通过pack找到市场以市场下的所有pack
|
||||
2通过province找到对应的省份或者机构,从而获取到audit_code
|
||||
*/
|
||||
----pack
|
||||
with pack_and_brand ( --brand 转 pack
|
||||
select
|
||||
upper(brandnamee) brand_name
|
||||
,ims_pack_cod PACK_COD --取所有code带出market
|
||||
from dwd.dwd_td_imscode_azcode
|
||||
where ims_pack_cod is not null and startswith(ims_pack_cod,'0')
|
||||
and upper(brandnamee) in (select brand from brand_province_config group by 1 )--取出在配置表中的pack
|
||||
group by 1,2
|
||||
)
|
||||
|
||||
,pack_to_market (
|
||||
select
|
||||
a.PACK_COD
|
||||
,MARKET
|
||||
from dm.dm_td_external_market_pack_mapping a
|
||||
inner join pack_and_brand b on a.PACK_COD = b.pack_cod --取出配置表中的brand对应的 market
|
||||
where DATA_SOURCE in ( 'AIA(Monthly)')--只取 AIA中的市场,chpa的市场太多
|
||||
and lower(a.MARKET) not like '%all market%'
|
||||
and market not in (
|
||||
'RE Extended Market'
|
||||
,'RE Market'
|
||||
,'RE Market (Global)'
|
||||
,'Pulmicort VBP Benchmark Market'
|
||||
,'Respules Market'
|
||||
)
|
||||
group by 1,2
|
||||
)
|
||||
,market_to_pack ( -- 获取上述market中所有的pack
|
||||
select
|
||||
market
|
||||
,pack_cod
|
||||
from dm.dm_td_external_market_pack_mapping a
|
||||
where DATA_SOURCE in ( 'AIA(Monthly)','IQVIA-CHPA(Monthly)')--AIA和chpa的市场
|
||||
and market in (select market from pack_to_market group by 1)
|
||||
group by 1,2
|
||||
)
|
||||
,province_to_audit (
|
||||
select
|
||||
PROVINCE_C
|
||||
,AUDIT_COD
|
||||
,DATA_SOURCE
|
||||
,CASE WHEN DATA_SOURCE = 'AIA(Monthly)' THEN 'AIA'
|
||||
WHEN DATA_SOURCE = 'IQVIA-CHPA(Monthly)' THEN 'CHPA'
|
||||
ELSE NULL
|
||||
END AS SOURCENAME
|
||||
from dm.dm_td_external_geo
|
||||
where DATA_SOURCE in ( 'AIA(Monthly)','IQVIA-CHPA(Monthly)')
|
||||
group by 1,2,3
|
||||
)
|
||||
|
||||
,pack_result as (
|
||||
select
|
||||
D.pack_cod
|
||||
,E.audit_cod
|
||||
,A.province
|
||||
,A.data_source as SOURCENAME
|
||||
,E.data_source
|
||||
from brand_province_config A
|
||||
inner join pack_and_brand B on A.brand = B.brand_name
|
||||
inner join pack_to_market C on B.pack_cod = C.pack_cod
|
||||
inner join market_to_pack D on C.market = D.market
|
||||
inner join province_to_audit E on A.province = E.province_c AND A.data_source = E.SOURCENAME
|
||||
where A.data_source <> '/'
|
||||
group by 1,2,3,4,5
|
||||
)
|
||||
|
||||
|
||||
----market
|
||||
,config_market (
|
||||
select
|
||||
market
|
||||
from market_province_config
|
||||
group by 1
|
||||
)
|
||||
,market_to_pack_dtp (
|
||||
select
|
||||
PACK_COD,MARKET
|
||||
from dm.dm_td_external_market_pack_mapping
|
||||
where DATA_SOURCE = 'DTP(Quarterly)' and PACK_COD in (
|
||||
select
|
||||
PACK_COD
|
||||
from dm.dm_tf_external_sales
|
||||
where DTP_FLAG =1
|
||||
group by 1)
|
||||
)
|
||||
,final_market_to_pack (--获取需要转换market成pack的部分
|
||||
select PACK_COD,a.MARKET from market_to_pack a inner join config_market b on a.market = b.market
|
||||
union
|
||||
select PACK_COD,a.MARKET from market_to_pack_dtp a inner join config_market b on a.market = b.market
|
||||
)
|
||||
|
||||
----market
|
||||
,market_result as (
|
||||
select
|
||||
B.pack_cod
|
||||
,C.AUDIT_COD
|
||||
,A.province
|
||||
,A.data_source as SOURCENAME
|
||||
,C.DATA_SOURCE
|
||||
from market_province_config a
|
||||
left join final_market_to_pack B on A.market = B.market
|
||||
left join province_to_audit C on A.province = C.province_c AND A.data_source = C.SOURCENAME
|
||||
where a.data_source <> '/' AND C.AUDIT_COD IS not NULL
|
||||
group by 1,2,3,4,5)
|
||||
,need_to_be_changed( --算出不在pack 中的pack code,替换成 market中的pack code 的配置信息
|
||||
select
|
||||
pack_cod,AUDIT_COD,DATA_SOURCE,sourcename
|
||||
from market_result
|
||||
where pack_cod in (select pack_cod from pack_result group by 1)
|
||||
)
|
||||
|
||||
--最终写入表
|
||||
|
||||
select
|
||||
pack_cod,AUDIT_COD,DATA_SOURCE,sourcename, 'pack' CONFIG_DATASOURCE
|
||||
from pack_result
|
||||
union ALL
|
||||
|
||||
select
|
||||
pack_cod,AUDIT_COD,DATA_SOURCE,sourcename, 'market' CONFIG_DATASOURCE
|
||||
from pack_result
|
||||
where pack_cod not in (select pack_cod from need_to_be_changed group by 1)
|
||||
union all
|
||||
select
|
||||
pack_cod,AUDIT_COD,DATA_SOURCE,sourcename, 'market' CONFIG_DATASOURCE
|
||||
from need_to_be_changed
|
||||
Binary file not shown.
213
EXTERNAL/Merged_Data/Merged_Data_Config_table_bymonth.sql
Normal file
213
EXTERNAL/Merged_Data/Merged_Data_Config_table_bymonth.sql
Normal file
@@ -0,0 +1,213 @@
|
||||
-- Databricks notebook source
|
||||
-- create or replace table dws.dws_td_ext_merge_data_config
|
||||
-- (
|
||||
-- market string,
|
||||
-- audit_cod string,
|
||||
-- province string,
|
||||
-- sourcename string,
|
||||
-- data_source string
|
||||
-- )
|
||||
-- USING delta
|
||||
-- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dws_td_ext_merge_data_config';
|
||||
-- -- 上面是生产环境location,下面是测试环境location
|
||||
-- -- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dws_td_ext_merge_data_config';
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
create or replace temporary view brand_province_config -- brand配置表
|
||||
as
|
||||
select upper(`brand`) as brand, `province` as province, datasource as data_source from dwd.dwd_gnd_merge_data_brand_province
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
create or replace temporary view market_province_config --market 配置表
|
||||
as
|
||||
select market, province, datasource as data_source from dwd.dwd_gnd_merge_data_brand_market
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- -- insert overwrite dws.dws_td_ext_merge_data_config_special
|
||||
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DWS/dws_external_merge_data_config_special' --prd
|
||||
-- -- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DWS/dws_external_merge_data_config_special' --test
|
||||
-- with pack_and_brand ( --brand 转 pack
|
||||
-- select
|
||||
-- upper(brandnamee) brand_name
|
||||
-- ,ims_pack_cod PACK_COD --取所有code带出market
|
||||
-- from dwd.dwd_td_imscode_azcode
|
||||
-- where ims_pack_cod is not null and startswith(ims_pack_cod,'0')
|
||||
-- and upper(brandnamee) in (select brand from brand_province_config group by 1 )--取出在配置表中的pack
|
||||
-- group by 1,2
|
||||
-- )
|
||||
-- ,pack_to_market (
|
||||
-- select
|
||||
-- a.PACK_CODE
|
||||
-- ,MARKET
|
||||
-- from dm.dm_td_ext_unionall_market_pack_mapping a
|
||||
-- inner join pack_and_brand b on a.PACK_CODE = b.pack_cod --取出配置表中的brand对应的 market
|
||||
-- where a.DATASOURCE in ( 'AIA(Monthly)')--只取 AIA中的市场,chpa的市场太多
|
||||
-- and lower(a.MARKET) not like '%all market%'
|
||||
-- and market not in (
|
||||
-- 'RE Extended Market'
|
||||
-- ,'RE Market'
|
||||
-- ,'RE Market (Global)'
|
||||
-- ,'Pulmicort VBP Benchmark Market'
|
||||
-- ,'Respules Market'
|
||||
-- )
|
||||
-- group by 1,2
|
||||
-- )
|
||||
-- -- ,market_to_pack ( -- 获取上述market中所有的pack
|
||||
-- -- select
|
||||
-- -- market
|
||||
-- -- ,pack_cod
|
||||
-- -- from dm.dm_td_external_market_pack_mapping a
|
||||
-- -- where DATA_SOURCE in ( 'AIA(Monthly)','IQVIA-CHPA(Monthly)')--AIA和chpa的市场
|
||||
-- -- and market in (select market from pack_to_market group by 1)
|
||||
-- -- group by 1,2
|
||||
-- -- )
|
||||
-- ,province_to_audit (
|
||||
-- select
|
||||
-- PROVINCE_C
|
||||
-- ,AUDIT_COD
|
||||
-- ,DATA_SOURCE
|
||||
-- ,CASE WHEN DATA_SOURCE = 'AIA(Monthly)' THEN 'AIA'
|
||||
-- WHEN DATA_SOURCE = 'IQVIA-CHPA(Monthly)' THEN 'CHPA'
|
||||
-- ELSE NULL
|
||||
-- END AS SOURCENAME
|
||||
-- from dm.dm_td_external_geo
|
||||
-- where DATA_SOURCE in ( 'AIA(Monthly)','IQVIA-CHPA(Monthly)')
|
||||
-- group by 1,2,3
|
||||
-- )
|
||||
-- ,not_selected_datasource (--因为pack2market2pack 会导致部分大市场包含所有的pack,故用配置表再删除一些不需要配置数据源的
|
||||
-- select
|
||||
-- a.ims_pack_cod PACK_COD --取所有code
|
||||
-- ,b.province
|
||||
-- from dwd.dwd_td_imscode_azcode a
|
||||
-- inner join (
|
||||
-- select
|
||||
-- UPPER(brand) as brand
|
||||
-- ,province
|
||||
-- from brand_province_config
|
||||
-- where data_source = '/'
|
||||
-- group by 1,2
|
||||
-- ) b
|
||||
-- on upper(a.brandnamee) = upper(b.brand )
|
||||
-- where a.ims_pack_cod is not null and startswith(a.ims_pack_cod,'0')
|
||||
-- group by 1,2
|
||||
-- union
|
||||
-- select
|
||||
-- pack_cod,
|
||||
-- '天津'
|
||||
-- from market_to_pack
|
||||
-- where market= 'EGFR TKI Market'
|
||||
-- group by 1
|
||||
-- )
|
||||
-- select DATA_SOURCE,PACK_COD,AUDIT_COD
|
||||
-- from not_selected_datasource a
|
||||
-- left join province_to_audit b on a.province = b.province_c
|
||||
-- group by 1,2,3
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
INSERT OVERWRITE dws.dws_td_ext_merge_data_config
|
||||
-- create or replace table dws.dws_td_ext_merge_data_config
|
||||
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DWS/dws_td_ext_merge_data_config' --test
|
||||
/*
|
||||
1通过brand找到pack,通过pack找到市场以市场下的所有pack
|
||||
2通过province找到对应的省份或者机构,从而获取到audit_code
|
||||
*/
|
||||
----pack
|
||||
with pack_and_brand ( --brand 转 pack
|
||||
select
|
||||
upper(brandnamee) brand_name
|
||||
,ims_pack_cod PACK_COD --取所有code带出market
|
||||
from dwd.dwd_td_imscode_azcode
|
||||
where ims_pack_cod is not null and startswith(ims_pack_cod,'0')
|
||||
and upper(brandnamee) in (select brand from brand_province_config group by 1 )--取出在配置表中的pack
|
||||
group by 1,2
|
||||
)
|
||||
|
||||
,pack_to_market (
|
||||
select
|
||||
a.PACK_CODE,
|
||||
MARKET
|
||||
from dm.dm_td_ext_unionall_market_pack_mapping a
|
||||
inner join pack_and_brand b on a.PACK_CODE = b.pack_cod --取出配置表中的brand对应的 market
|
||||
where a.DATASOURCE in ( 'AIA(Monthly)')--只取 AIA中的市场,chpa的市场太多
|
||||
and lower(a.MARKET) not like '%all market%'
|
||||
and market not in (
|
||||
'RE Extended Market'
|
||||
,'RE Market'
|
||||
,'RE Market (Global)'
|
||||
,'Pulmicort VBP Benchmark Market'
|
||||
,'Respules Market'
|
||||
)
|
||||
group by 1,2
|
||||
)
|
||||
,province_to_audit (
|
||||
select
|
||||
PROVINCE_C
|
||||
,AUDIT_COD
|
||||
,DATA_SOURCE
|
||||
,CASE WHEN DATA_SOURCE = 'AIA(Monthly)' THEN 'AIA'
|
||||
WHEN DATA_SOURCE = 'IQVIA-CHPA(Monthly)' THEN 'CHPA'
|
||||
ELSE NULL
|
||||
END AS SOURCENAME
|
||||
from dm.dm_td_external_geo
|
||||
where DATA_SOURCE in ( 'AIA(Monthly)','IQVIA-CHPA(Monthly)')
|
||||
group by 1,2,3
|
||||
)
|
||||
|
||||
,pack_result as (
|
||||
select
|
||||
C.market
|
||||
,E.audit_cod
|
||||
,A.province
|
||||
,A.data_source as SOURCENAME
|
||||
,E.data_source
|
||||
from brand_province_config A
|
||||
inner join pack_and_brand B on A.brand = B.brand_name
|
||||
inner join pack_to_market C on B.pack_cod = C.pack_code
|
||||
-- inner join market_to_pack D on C.market = D.market
|
||||
inner join province_to_audit E on A.province = E.province_c AND A.data_source = E.SOURCENAME
|
||||
where A.data_source <> '/'
|
||||
group by 1,2,3,4,5
|
||||
)
|
||||
|
||||
---- MARKET PART
|
||||
|
||||
,market_result as (
|
||||
select
|
||||
A.MARKET
|
||||
,C.AUDIT_COD
|
||||
,A.province
|
||||
,A.data_source as SOURCENAME
|
||||
,C.DATA_SOURCE
|
||||
from market_province_config a
|
||||
-- left join final_market_to_pack B on A.market = B.market
|
||||
left join province_to_audit C on A.province = C.province_c AND A.data_source = C.SOURCENAME
|
||||
where a.data_source <> '/' AND C.AUDIT_COD IS not NULL
|
||||
group by 1,2,3,4,5)
|
||||
|
||||
|
||||
----最终结果
|
||||
select
|
||||
market,
|
||||
audit_cod,
|
||||
province,
|
||||
sourcename,
|
||||
data_source
|
||||
FROM pack_result
|
||||
where market NOT in (
|
||||
select
|
||||
market
|
||||
from market_province_config
|
||||
group by 1
|
||||
)
|
||||
union all
|
||||
select
|
||||
market,
|
||||
audit_cod,
|
||||
province,
|
||||
sourcename,
|
||||
data_source
|
||||
FROM market_result
|
||||
Binary file not shown.
@@ -0,0 +1,124 @@
|
||||
-- Databricks notebook source
|
||||
-- create or replace table dm.dm_tf_exteranl_sales_merged_data_dtp_others
|
||||
insert overwrite table dm.dm_tf_exteranl_sales_merged_data_dtp_others
|
||||
with all_egfr_pack as (
|
||||
select * from DM.dm_td_external_market_pack_mapping
|
||||
where DATA_SOURCE = 'DTP(Quarterly)' and MARKET = 'EGFR TKI Market'
|
||||
)
|
||||
,4brand as (
|
||||
select * from all_egfr_pack
|
||||
where PROD_DES_C in ('阿美乐','泰瑞沙','凯美纳','艾弗沙')
|
||||
)
|
||||
,otherbrand as (
|
||||
select PACK_COD from all_egfr_pack
|
||||
where PACK_COD not in ( select PACK_COD from `4brand` group by 1)
|
||||
group by 1
|
||||
)
|
||||
|
||||
|
||||
-- -- 直取表 cu - dtp4brand 的 cu = otherbrand 的cu
|
||||
,dirct_cu as (
|
||||
select
|
||||
yyyymm
|
||||
,replace(audit_key,'DTP(Quarterly)','') audit_cod
|
||||
,cast(pdot_counting_unit as decimal ) pdot_counting_unit
|
||||
,cast(pdot_counting_unit_ly as decimal ) pdot_counting_unit_ly
|
||||
from dm.dm_tf_external_retail_special a
|
||||
where yyyymm >=202401
|
||||
)
|
||||
--先 4个brand 的 cu 和 value
|
||||
,fact_sales_dtp_4brand(
|
||||
select
|
||||
YYYYMM,AUDIT_COD
|
||||
,sum(cast( CONUTING_UNIT*PDOT_MARKET_RATIO as decimal)) CONUTING_UNIT
|
||||
,sum(cast( CONUTING_UNIT_LY*PDOT_MARKET_RATIO as decimal)) CONUTING_UNIT_LY
|
||||
from dm.dm_tf_external_sales t1
|
||||
inner join 4brand t2 on t1.PACK_COD = t2.PACK_COD
|
||||
where t1.DATA_SOURCE = 'DTP(Quarterly)'
|
||||
and DTP_FLAG = 1
|
||||
and YYYYMM >=202401
|
||||
group by
|
||||
YYYYMM,AUDIT_COD )
|
||||
|
||||
-- 其他产品 的 cu 和 value
|
||||
,fact_sales_dtp_other(
|
||||
select
|
||||
YYYYMM
|
||||
,"DTPOTHERS" PACK_COD
|
||||
,"DTPOTHERS" CORP_COD
|
||||
,AUDIT_COD
|
||||
,'' as PLATFORM_TYPE
|
||||
,'' as STORE_NAME
|
||||
,'' as STORE_TYPE
|
||||
,'' as REGION_TYPE
|
||||
,1 as PACK_FLAG
|
||||
,2 as PROD_FLAG
|
||||
,1 as DTP_FLAG
|
||||
,sum(SALES_UNIT_CAL) SALES_UNIT_CAL
|
||||
,sum(SALES_UNIT_CAL_LY) SALES_UNIT_CAL_LY
|
||||
,sum(SALES_VALUE_CAL) SALES_VALUE_CAL
|
||||
,sum(SALES_VALUE_CAL_LY) SALES_VALUE_CAL_LY
|
||||
,sum(CONUTING_UNIT) CONUTING_UNIT
|
||||
,sum(CONUTING_UNIT_LY) CONUTING_UNIT_LY
|
||||
,'DTP(Quarterly)' as DATA_SOURCE
|
||||
,'' as inst_code
|
||||
,'' as cmps_flag
|
||||
,'' as dept_name
|
||||
,'' as prescription
|
||||
,'' as prescription_ly
|
||||
,'' as new_code
|
||||
,'' as area
|
||||
,'' as h_level
|
||||
,'' as reimburse
|
||||
,'' as reimburse_type
|
||||
,'' as prescription_source
|
||||
from dm.dm_tf_external_sales
|
||||
where DTP_FLAG = 1
|
||||
and PACK_COD in (select PACK_COD from otherbrand) --其他部分
|
||||
and DATA_SOURCE = 'DTP(Quarterly)'
|
||||
and YYYYMM >=202401
|
||||
group by
|
||||
YYYYMM,AUDIT_COD)
|
||||
|
||||
select
|
||||
T0.YYYYMM
|
||||
,t0.PACK_COD
|
||||
,t0.CORP_COD
|
||||
,t0.AUDIT_COD
|
||||
,t0.PLATFORM_TYPE
|
||||
,t0.STORE_NAME
|
||||
,t0.STORE_TYPE
|
||||
,t0.REGION_TYPE
|
||||
,t0.PACK_FLAG
|
||||
,t0.PROD_FLAG
|
||||
,t0.DTP_FLAG
|
||||
,t0.SALES_UNIT_CAL
|
||||
,t0.SALES_UNIT_CAL_LY
|
||||
,t0.SALES_VALUE_CAL
|
||||
,t0.SALES_VALUE_CAL_LY
|
||||
,t1.CONUTING_UNIT
|
||||
,t1.CONUTING_UNIT_LY
|
||||
,t0.DATA_SOURCE
|
||||
,t0.inst_code
|
||||
,t0.cmps_flag
|
||||
,t0.dept_name
|
||||
,t0.prescription
|
||||
,t0.prescription_ly
|
||||
,t0.new_code
|
||||
,t0.area
|
||||
,t0.h_level
|
||||
,t0.reimburse
|
||||
,t0.reimburse_type
|
||||
,t0.prescription_source
|
||||
from fact_sales_dtp_other t0
|
||||
left join (
|
||||
select
|
||||
t1.yyyymm
|
||||
,t1.audit_cod
|
||||
,t1.pdot_counting_unit - t2.conuting_unit as CONUTING_UNIT
|
||||
,t1.pdot_counting_unit_ly - t2.conuting_unit_ly as CONUTING_UNIT_LY
|
||||
from dirct_cu t1
|
||||
left join fact_sales_dtp_4brand t2 on t1.yyyymm = t2.yyyymm and t1.audit_cod = t2.audit_cod
|
||||
) t1
|
||||
on t0.audit_cod = t1.audit_cod and t0.yyyymm = t1.yyyymm
|
||||
-- select * from fact_sales_dtp_4brand
|
||||
Binary file not shown.
@@ -0,0 +1,289 @@
|
||||
-- Databricks notebook source
|
||||
-- CREATE OR REPLACE TABLE dm.DM_TF_EXTERANL_SALES_MERGED_DATA_DTP_OTHERS_bymonth (
|
||||
-- MARKET STRING,
|
||||
-- KEY_COMPETITOR STRING,
|
||||
-- CLASS STRING,
|
||||
-- YYYYMM STRING,
|
||||
-- PACK_COD STRING,
|
||||
-- CORP_COD STRING,
|
||||
-- AUDIT_COD STRING,
|
||||
-- PLATFORM_TYPE STRING,
|
||||
-- STORE_NAME STRING,
|
||||
-- STORE_TYPE STRING,
|
||||
-- REGION_TYPE STRING,
|
||||
-- PACK_FLAG INT,
|
||||
-- PROD_FLAG INT,
|
||||
-- DTP_FLAG INT,
|
||||
-- SALES_UNIT_CAL DECIMAL(38,10),
|
||||
-- SALES_UNIT_CAL_LY DECIMAL(38,10),
|
||||
-- SALES_VALUE_CAL DECIMAL(38,10),
|
||||
-- SALES_VALUE_CAL_LY DECIMAL(38,10),
|
||||
-- CONUTING_UNIT DECIMAL(21,0),
|
||||
-- CONUTING_UNIT_LY DECIMAL(21,0),
|
||||
-- PDOT DECIMAL(21,0),
|
||||
-- PDOT_LY DECIMAL(21,0),
|
||||
-- DATA_SOURCE STRING,
|
||||
-- inst_code STRING,
|
||||
-- cmps_flag STRING,
|
||||
-- dept_name STRING,
|
||||
-- prescription STRING,
|
||||
-- prescription_ly STRING,
|
||||
-- new_code STRING,
|
||||
-- area STRING,
|
||||
-- h_level STRING,
|
||||
-- reimburse STRING,
|
||||
-- reimburse_type STRING,
|
||||
-- prescription_source STRING)
|
||||
-- USING delta
|
||||
-- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/DM_TF_EXTERANL_SALES_MERGED_DATA_DTP_OTHERS_bymonth';
|
||||
-- -- 上面是生产环境location,下面是测试环境location
|
||||
-- -- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/DM_TF_EXTERANL_SALES_MERGED_DATA_DTP_OTHERS_bymonth';
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- 这个jupyter核心逻辑是
|
||||
-- 直接 cu - 4brand cu = other cu
|
||||
-- 其中 维度部分,通过DM.DM_TF_EXT_UNIONALL_SALES innerjoin other
|
||||
-- 其中 度量部分,通过 直接cu - 4brand cu
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
CREATE OR REPLACE TEMPORARY VIEW RETAIL_DTP
|
||||
AS
|
||||
SELECT
|
||||
nvl(t2.market,'ALL Market') MARKET
|
||||
,nvl(t2.KEY_COMPETITOR,'OTHERS') as KEY_COMPETITOR
|
||||
,nvl(t2.CLASS,'Others') as CLASS
|
||||
,t1.YYYYMM
|
||||
,t1.PACK_CODE
|
||||
,t1.CORP_CODE
|
||||
,t1.AUDIT_CODE
|
||||
,t1.PLATFORM_TYPE
|
||||
,t1.STORE_NAME
|
||||
,t1.STORE_TYPE
|
||||
,t1.REGION_TYPE
|
||||
,t1.DATA_SOURCE
|
||||
,t1.PACK_FLAG
|
||||
,t1.PROD_FLAG
|
||||
,t1.DTP_FLAG
|
||||
,t1.CMPS_FLAG
|
||||
,t1.NEW_CODE
|
||||
,t1.INST_CODE
|
||||
,NULL AS AIA_HP_FLAG
|
||||
,t1.DEPT_NAME
|
||||
,t1.H_LEVEL
|
||||
,t1.REIMBURSE
|
||||
,t1.REIMBURSE_TYPE
|
||||
,PRESCRIPTION_SOURCE AS PRESCRIPTION_TYPE
|
||||
,t1.PRESCRIPTION
|
||||
,t1.PRESCRIPTION_LY
|
||||
,nvl(nvl(t2.MARKET_RATIO,1)*t2.Value_brand_Ratio,1) as VA
|
||||
,nvl(nvl(t2.MARKET_RATIO,1)*t2.Unit_brand_Ratio,1) AS UT
|
||||
,nvl(nvl(t2.MARKET_RATIO,1)*t2.CountingUnit_brand_Ratio,1) AS CU
|
||||
,nvl(nvl(t2.MARKET_RATIO,1)*t2.PDOT_brand_Ratio,1) AS PT
|
||||
,cast(t1.SALES_UNIT_CAL * nvl(t2.Unit_brand_Ratio,1) *nvl(t2.MARKET_RATIO,1) as decimal(35,6)) AS SALES_UNIT_CAL
|
||||
,cast(t1.SALES_UNIT_CAL_LY * nvl(t3.Unit_brand_Ratio,1) *nvl(t3.MARKET_RATIO,1) as decimal(35,6)) AS SALES_UNIT_CAL_LY
|
||||
,cast(t1.SALES_VALUE_CAL * nvl(t2.Value_brand_Ratio,1) *nvl(t2.MARKET_RATIO,1) as decimal(35,6)) AS SALES_VALUE_CAL
|
||||
,cast(t1.SALES_VALUE_CAL_LY * nvl(t3.Value_brand_Ratio,1) *nvl(t3.MARKET_RATIO,1) as decimal(35,6)) AS SALES_VALUE_CAL_LY
|
||||
,cast(t1.CONUTING_UNIT * nvl(t2.CountingUnit_brand_Ratio,1) *nvl(t2.MARKET_RATIO,1) as decimal(35,6)) AS CONUTING_UNIT
|
||||
,cast(t1.CONUTING_UNIT_LY * nvl(t3.CountingUnit_brand_Ratio,1) *nvl(t3.MARKET_RATIO,1) as decimal(35,6)) AS CONUTING_UNIT_LY
|
||||
,cast(t1.CONUTING_UNIT * nvl(t2.PDOT_brand_Ratio,1) *nvl(t2.MARKET_RATIO,1) as decimal(35,6)) AS PDOT
|
||||
,cast(t1.CONUTING_UNIT_LY * nvl(t3.PDOT_brand_Ratio,1) *nvl(t3.MARKET_RATIO,1) as decimal(35,6)) AS PDOT_LY
|
||||
FROM DM.DM_TF_EXT_UNIONALL_SALES t1
|
||||
LEFT JOIN DM.dm_td_ext_unionall_market_pack_mapping t2
|
||||
on t1.PACK_CODE = t2.PACK_CODE and t1.DATA_SOURCE = t2.DATASOURCE
|
||||
and t1.YYYYMM >=t2.starttime and t1.YYYYMM <=t2.endtime
|
||||
and t1.YYYYMM >= t2.Value_brand_Ratio_start and t1.YYYYMM <=t2.Value_brand_Ratio_end
|
||||
and t1.YYYYMM >= t2.Unit_brand_start and t1.YYYYMM <=t2.Unit_brand_end
|
||||
and t1.YYYYMM >= t2.CountingUnit_brand_start and t1.YYYYMM <=t2.CountingUnit_brand_end
|
||||
and t1.YYYYMM >= t2.PDOT_brand_start and t1.YYYYMM <=t2.PDOT_brand_end
|
||||
LEFT JOIN DM.dm_td_ext_unionall_market_pack_mapping t3
|
||||
on t1.PACK_CODE = t3.PACK_CODE and t1.DATA_SOURCE = t3.DATASOURCE
|
||||
and (t1.YYYYMM-100) >=t3.starttime and (t1.YYYYMM-100) <=t3.endtime
|
||||
and (t1.YYYYMM-100) >= t3.Value_brand_Ratio_start and (t1.YYYYMM-100) <=t3.Value_brand_Ratio_end
|
||||
and (t1.YYYYMM-100) >= t3.Unit_brand_start and (t1.YYYYMM-100) <=t3.Unit_brand_end
|
||||
and (t1.YYYYMM-100) >= t3.CountingUnit_brand_start and (t1.YYYYMM-100) <=t3.CountingUnit_brand_end
|
||||
and (t1.YYYYMM-100) >= t3.PDOT_brand_start and (t1.YYYYMM-100) <=t3.PDOT_brand_end
|
||||
and t2.market = t3.MARKET
|
||||
where t1.DATA_SOURCE = 'DTP(Quarterly)' and t1.DTP_FLAG=1
|
||||
-- and YYYYMM >=202501
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- 所有的pack_code
|
||||
create or replace temporary view all_egfr_pack
|
||||
as
|
||||
select MARKET,PACK_CODE from DM.DM_TD_EXT_RETAIL_MARKET_PACK_MAPPING
|
||||
where MARKET = 'EGFR TKI Market'
|
||||
group by MARKET,PACK_CODE
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- 4brand pack_code
|
||||
create or replace temporary view 4brand
|
||||
as
|
||||
select
|
||||
MARKET,
|
||||
PACK_CODE
|
||||
from
|
||||
all_egfr_pack
|
||||
where
|
||||
pack_code IN
|
||||
(
|
||||
SELECT
|
||||
PACK_CODE
|
||||
FROM
|
||||
dm.dm_td_ext_retail_DTP_pack_property
|
||||
WHERE
|
||||
PROD_DESC_C in ('阿美乐','泰瑞沙','凯美纳','艾弗沙')
|
||||
group BY
|
||||
PACK_CODE
|
||||
)
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
create or replace temporary view otherbrand
|
||||
as
|
||||
select
|
||||
PACK_CODE
|
||||
from
|
||||
all_egfr_pack
|
||||
where
|
||||
PACK_CODE not in ( select PACK_CODE from `4brand` group by PACK_CODE)
|
||||
group by
|
||||
PACK_CODE
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- 这里是 直接 cu
|
||||
create or replace temporary view dirct_cu
|
||||
as
|
||||
select
|
||||
yyyymm,
|
||||
replace(audit_key,'DTP(Quarterly)','') audit_cod,
|
||||
cast(pdot_counting_unit as decimal ) pdot_counting_unit,
|
||||
cast(pdot_counting_unit_ly as decimal ) pdot_counting_unit_ly
|
||||
from dm.dm_tf_external_retail_special a
|
||||
where yyyymm >=202401
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- 这里是获取 4brand 的度量值部分
|
||||
create or replace temporary view fact_sales_dtp_4brand
|
||||
as
|
||||
select
|
||||
YYYYMM,
|
||||
AUDIT_CODE AS AUDIT_COD
|
||||
,sum(PDOT) CONUTING_UNIT
|
||||
,sum(PDOT_LY) CONUTING_UNIT_LY
|
||||
,MAX(t2.MARKET) as MARKET
|
||||
from RETAIL_DTP t1
|
||||
inner join 4brand t2 on t1.PACK_CODE = t2.PACK_CODE
|
||||
where t1.DATA_SOURCE = 'DTP(Quarterly)'
|
||||
and t1.MARKET = 'EGFR TKI Market'
|
||||
and DTP_FLAG = 1
|
||||
and YYYYMM >=202401
|
||||
group by
|
||||
YYYYMM,AUDIT_COD
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- 这里是获取 other 的维度值部分
|
||||
create or replace temporary view fact_sales_dtp_other
|
||||
as
|
||||
select
|
||||
YYYYMM
|
||||
,"DTPOTHERS" PACK_COD
|
||||
,"DTPOTHERS" CORP_COD
|
||||
,AUDIT_CODE AS AUDIT_COD
|
||||
,'' as PLATFORM_TYPE
|
||||
,'' as STORE_NAME
|
||||
,'' as STORE_TYPE
|
||||
,'' as REGION_TYPE
|
||||
,1 as PACK_FLAG
|
||||
,2 as PROD_FLAG
|
||||
,1 as DTP_FLAG
|
||||
,sum(SALES_UNIT_CAL) SALES_UNIT_CAL
|
||||
,sum(SALES_UNIT_CAL_LY) SALES_UNIT_CAL_LY
|
||||
,sum(SALES_VALUE_CAL) SALES_VALUE_CAL
|
||||
,sum(SALES_VALUE_CAL_LY) SALES_VALUE_CAL_LY
|
||||
,sum(CONUTING_UNIT) CONUTING_UNIT
|
||||
,sum(CONUTING_UNIT_LY) CONUTING_UNIT_LY
|
||||
,'DTP(Quarterly)' as DATA_SOURCE
|
||||
,'' as inst_code
|
||||
,'' as cmps_flag
|
||||
,'' as dept_name
|
||||
,'' as prescription
|
||||
,'' as prescription_ly
|
||||
,'' as new_code
|
||||
,'' as area
|
||||
,'' as h_level
|
||||
,'' as reimburse
|
||||
,'' as reimburse_type
|
||||
,'' as prescription_source
|
||||
from DM.DM_TF_EXT_UNIONALL_SALES
|
||||
where DTP_FLAG = 1
|
||||
and PACK_CODE in (select PACK_CODe from otherbrand) --其他部分
|
||||
and DATA_SOURCE = 'DTP(Quarterly)'
|
||||
and YYYYMM >=202401
|
||||
group by
|
||||
YYYYMM,AUDIT_CODE
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- create or replace table dm.DM_TF_EXTERANL_SALES_MERGED_DATA_DTP_OTHERS_bymonth
|
||||
insert overwrite table dm.DM_TF_EXTERANL_SALES_MERGED_DATA_DTP_OTHERS_bymonth
|
||||
select
|
||||
t1.MARKET
|
||||
,'OTHERS' as KEY_COMPETITOR
|
||||
,'Others' as CLASS
|
||||
,T0.YYYYMM
|
||||
,t0.PACK_COD
|
||||
,t0.CORP_COD
|
||||
,t0.AUDIT_COD
|
||||
,t0.PLATFORM_TYPE
|
||||
,t0.STORE_NAME
|
||||
,t0.STORE_TYPE
|
||||
,t0.REGION_TYPE
|
||||
,t0.PACK_FLAG
|
||||
,t0.PROD_FLAG
|
||||
,t0.DTP_FLAG
|
||||
,t0.SALES_UNIT_CAL
|
||||
,t0.SALES_UNIT_CAL_LY
|
||||
,t0.SALES_VALUE_CAL
|
||||
,t0.SALES_VALUE_CAL_LY
|
||||
,t1.CONUTING_UNIT
|
||||
,t1.CONUTING_UNIT_LY
|
||||
,t1.CONUTING_UNIT as pdot
|
||||
,t1.CONUTING_UNIT_LY as pdot_ly
|
||||
,t0.DATA_SOURCE
|
||||
,t0.inst_code
|
||||
,t0.cmps_flag
|
||||
,t0.dept_name
|
||||
,t0.prescription
|
||||
,t0.prescription_ly
|
||||
,t0.new_code
|
||||
,t0.area
|
||||
,t0.h_level
|
||||
,t0.reimburse
|
||||
,t0.reimburse_type
|
||||
,t0.prescription_source
|
||||
from fact_sales_dtp_other t0
|
||||
left join (
|
||||
select
|
||||
t1.yyyymm
|
||||
,t1.audit_cod
|
||||
,t1.pdot_counting_unit - t2.conuting_unit as CONUTING_UNIT
|
||||
,t1.pdot_counting_unit_ly - t2.conuting_unit_ly as CONUTING_UNIT_LY
|
||||
,t2.market
|
||||
from dirct_cu t1
|
||||
left join fact_sales_dtp_4brand t2 on t1.yyyymm = t2.yyyymm and t1.audit_cod = t2.audit_cod
|
||||
) t1
|
||||
on t0.audit_cod = t1.audit_cod and t0.yyyymm = t1.yyyymm
|
||||
-- select * from fact_sales_dtp_4brand
|
||||
Binary file not shown.
Reference in New Issue
Block a user