diff --git a/AIA/02 dm_ext_aia_sales_bakup_20230327.sql b/AIA/02 dm_ext_aia_sales_bakup_20230327.sql deleted file mode 100644 index 66a5305..0000000 --- a/AIA/02 dm_ext_aia_sales_bakup_20230327.sql +++ /dev/null @@ -1,195 +0,0 @@ --- Databricks notebook source ---字段值的初步处理 -create or replace temporary view aia_rawdata_1 as -select -year, -qtr, -cast(year * 100 + ym as int) ym, -org_prov, -org_city, -ins_level, -case when ins_cd is not null and length(ins_cd) < 7 then right(concat('0000000',ins_cd),7) else ins_cd end as org_ins_cd, -atc1, -atc2, -atc3, -atc4, -atc, -mole_nm, -prod_nm, -org_prd_nm_c, -org_pk, -org_prd_str, -org_pth, -min_unit, -value, -unit, -cast(pack_desc as int) as pack_desc, -org_pk_un, -manu -from dwd.dwd_gnd_aia_rawdata - --- COMMAND ---------- - - ---关联出AUDIT_COD、inst_code -create or replace temporary view aia_rawdata_2 as -with hospital_mapping as ( -select distinct -case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code, -ins_cd_nl as inst_code -from dwd.dwd_gnd_hospital_not_provided -) - -select upper(nvl(t2.inst_code,t1.org_ins_cd)) AUDIT_COD, -t1.*, -nvl(t2.inst_code,t1.org_ins_cd) as inst_code -from aia_rawdata_1 t1 -left join hospital_mapping t2 -on t1.org_ins_cd = t2.cpa_hospital_code --- left join dm.dm_td_institution t3 --- on t2.ins_code = t3.inst_code --- left join dm.dm_ims_td_geo t4 --- on t3.city_name_en = t4.city - - --- COMMAND ---------- - -/* -修改人 chenwu -修改时间 20250513 -修改内容 打包分子的数据,不论 打通表 和 事实表,都没有系数,需要用 事实表里的 最小制剂单位数量 作为counting_unit -*/ ---关联出pack_cod、corp_cod、counting_unit -create or replace temporary view aia_rawdata_3 as -select case when length(trim(t2.iqvia_pack_code)) < 12 and trim(t2.iqvia_pack_code) REGEXP '^[0-9]' then right(concat('000000000000',trim(t2.iqvia_pack_code)),12) - else trim(t2.iqvia_pack_code) - end as pack_cod, -t3.corp_cod, -case when left(t2.iqvia_pack_code,4) = 'AZP_' then cast(t1.min_unit/t1.org_pk_un as decimal(38,10)) else t3.counting_unit end counting_unit, --- t3.counting_unit,20250513 -t1.* -from aia_rawdata_2 t1 -left join (select distinct trim(iqvia_pack_code) as iqvia_pack_code, -ATC1,ATC2,ATC3,ATC4,org_prd_cd,org_mole_nm_c, -org_manu_prd_nm_c,org_prd_nm_c,org_pk,org_prd_str, -org_pth,min_pk_unit,org_pk_unit,org_manu_nm from dwd.dwd_gnd_ext_aia_cpt_data) t2 ---ATC1编码+ATC2编码+ATC3编码+ATC4编码+药品编码(ATC)+药品通用名+药品产品名+药品商品名+规格+剂型+给药途径+最小销售包装单位+包装规格+生产企业 -on nvl(t1.ATC1,'') = nvl(t2.ATC1,'') -and nvl(t1.ATC2,'') = nvl(t2.ATC2,'') -and nvl(t1.ATC3,'') = nvl(t2.ATC3,'') -and nvl(t1.ATC4,'') = nvl(t2.ATC4,'') -and nvl(t1.ATC,'') = nvl(t2.org_prd_cd,'') -and nvl(t1.MOLE_NM,'') = nvl(t2.org_mole_nm_c,'') -and nvl(t1.PROD_NM,'') = nvl(t2.org_manu_prd_nm_c,'') -and nvl(t1.ORG_PRD_NM_C,'') = nvl(t2.org_prd_nm_c,'') -and nvl(t1.ORG_PK,'') = nvl(t2.org_pk,'') -and nvl(t1.ORG_PRD_STR,'') = nvl(t2.org_prd_str,'') -and nvl(t1.ORG_PTH,'') = nvl(t2.org_pth,'') -and nvl(t1.UNIT,'') = nvl(t2.min_pk_unit,'') -and nvl(t1.PACK_DESC,'') = nvl(t2.org_pk_unit,'') -and nvl(t1.MANU,'') = nvl(t2.org_manu_nm,'') -left join (select distinct PACK_COD,corp_cod,counting_unit from dm.dm_aia_pack_property) t3 -on t2.iqvia_pack_code = t3.PACK_COD - - --- COMMAND ---------- - - ---计算出ly销量 -create or replace temporary view aia_rawdata_4 as -with max_ym as ( -select max(ym) as max_ym from aia_rawdata_3 -) - -select - YM, - org_ins_cd, - inst_code, - AUDIT_COD, - pack_cod, - corp_cod, - sum(value) as value, - sum(volume) as volume, - sum(counting_unit) as counting_unit, - sum(value_ly) as value_ly, - sum(volume_ly) as volume_ly, - sum(counting_unit_ly) as counting_unit_ly -from ( -select - ym, - org_ins_cd, - inst_code, - AUDIT_COD, - pack_cod, - corp_cod, - cast(value as decimal(38,10)) as value, - cast(ORG_PK_UN as decimal(38,10)) as volume, - cast(ORG_PK_UN as decimal(38,10)) * coalesce(cast(counting_unit as decimal(38,10)),1) as counting_unit, - 0 as value_ly, - 0 as volume_ly, - 0 as counting_unit_ly -from aia_rawdata_3 -union all -select - cast(cast(ym as int) + 100 as string) as ym, - org_ins_cd, - inst_code, - AUDIT_COD, - pack_cod, - corp_cod, - 0 as value, - 0 as volume, - 0 as counting_unit, - cast(value as decimal(38,10)) as value_ly, - cast(ORG_PK_UN as decimal(38,10)) as volume_ly, - cast(ORG_PK_UN as decimal(38,10)) * coalesce(cast(counting_unit as decimal(38,10)),1) as counting_unit_ly -from aia_rawdata_3 t1 -join max_ym t2 -on 1=1 -where cast(cast(ym as int) + 100 as string) <= t2.max_ym - ) -group by ym, - org_ins_cd, - inst_code, - AUDIT_COD, - pack_cod, - corp_cod - - --- COMMAND ---------- - -insert overwrite table dm.dm_ext_aia_sales( -ym, -org_ins_cd, -inst_code, -AUDIT_COD, -pack_cod, -corp_cod, -value, -volume, -counting_unit, -value_ly, -volume_ly, -counting_unit_ly -) -select -ym, -org_ins_cd, -inst_code, -AUDIT_COD, -pack_cod, -corp_cod, -sum(value) as value, -sum(volume) as volume, -sum(counting_unit) as counting_unit, -sum(value_ly) as value_ly, -sum(volume_ly) as volume_ly, -sum(counting_unit_ly) as counting_unit_ly -from aia_rawdata_4 -group by -ym, -org_ins_cd, -inst_code, -AUDIT_COD, -pack_cod, -corp_cod \ No newline at end of file diff --git a/AIA/z_03 dm_aia_flag_备份用.sql b/AIA/z_03 dm_aia_flag_备份用.sql deleted file mode 100644 index 0738500..0000000 --- a/AIA/z_03 dm_aia_flag_备份用.sql +++ /dev/null @@ -1,643 +0,0 @@ --- Databricks notebook source --- MAGIC %run ../../../Common/config - --- COMMAND ---------- - --- MAGIC %python --- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dim_product_wide`').createOrReplaceTempView('cdw_dwd_dim_product_wide') --- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dwd_hospital_classification`').createOrReplaceTempView('cdw_dwd_dwd_hospital_classification') - --- COMMAND ---------- - ------------------------------------------------------------------------------------ ---修改时间:20241021 ---修改人:FanXujia ---修改内容: ---只取最新年-1 ~ 最新年范围内的,称为“2年共有”。增加 ------------------------------------------------------------------------------------ ---共有机构 -insert overwrite table dm.dm_aia_hp_flag -with max_year as ( -select -max(cast(year as int)) as max_year, -max(cast(year as int) - 1) as max_year_ly -from dwd.dwd_gnd_hospital_not_provided -) -,hp_flag as ( - select - distinct concat(t1.year, '-Y') as hp_his_flag, - case when length(t1.cpa_hospital_code) < 7 then right(concat('0000000',t1.cpa_hospital_code),7) else t1.cpa_hospital_code end as cpa_hospital_code - from - dwd.dwd_gnd_hospital_not_provided t1 - cross join max_year t2 - where cast(t1.year as int) >= t2.max_year_ly -) -,hp_flag2 as ( -SELECT - cpa_hospital_code, - CONCAT_WS(' ∩ ', SORT_ARRAY(COLLECT_LIST(hp_his_flag))) AS FLAG -FROM - hp_flag -GROUP BY - cpa_hospital_code -) -,inst_mapping as ( - -- select - -- distinct right(concat('0000000', org_cd), 7) org_cd, - -- ins_cd - -- from - -- dwd.dwd_gnd_hospitalmapping -select distinct -case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code, -ins_cd_nl as inst_code -from dwd.dwd_gnd_hospital_not_provided -) - -,ALL_INS as ( -select distinct inst_code -from dm.dm_ext_aia_sales -) - -,flag as ( - select distinct - coalesce(t2.inst_code,t1.cpa_hospital_code) as inst_code, - t1.flag as aia_hp_flag, - 'Y' as FLAG, -'AIA(Monthly)' as DATA_SOURCE - from hp_flag2 t1 - left join inst_mapping t2 on t1.cpa_hospital_code = t2.cpa_hospital_code - where - length(t1.flag) > 6 -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-CHPA(Monthly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'XH Data(Quarterly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'EC(Monthly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'Retail(Quarterly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'CHC(Quarterly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'THC(Quarterly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-COUNTY(Quarterly)' as DATA_SOURCE -) - -select t1.inst_code, -NVL(t2.aia_hp_flag,'') as aia_hp_flag, -NVL(t2.FLAG,'N') as FLAG, -'AIA(Monthly)' as DATA_SOURCE -from ALL_INS t1 -left join flag t2 -on t1.inst_code = t2.inst_code - - --- COMMAND ---------- - ------------------------------------------------------------------------------------ ---修改时间:20241021 ---修改人:FanXujia ---修改内容: ---只取最新年-1 ~ 最新年范围内的,称为“2年共有”。增加 ------------------------------------------------------------------------------------ ---共有机构 -insert overwrite table dm.dm_aia_hp_flag -with max_year as ( -select -max(cast(year as int)) as max_year, -max(cast(year as int) - 1) as max_year_ly -from dwd.dwd_gnd_hospital_not_provided -) -,hp_flag as ( - select - distinct concat(t1.year, '-Y') as hp_his_flag, - case when length(t1.cpa_hospital_code) < 7 then right(concat('0000000',t1.cpa_hospital_code),7) else t1.cpa_hospital_code end as cpa_hospital_code - from - dwd.dwd_gnd_hospital_not_provided t1 - cross join max_year t2 - where cast(t1.year as int) >= t2.max_year_ly -) -,hp_flag2 as ( -SELECT - cpa_hospital_code, - CONCAT_WS(' ∩ ', SORT_ARRAY(COLLECT_LIST(hp_his_flag))) AS FLAG -FROM - hp_flag -GROUP BY - cpa_hospital_code -) -,inst_mapping as ( - -- select - -- distinct right(concat('0000000', org_cd), 7) org_cd, - -- ins_cd - -- from - -- dwd.dwd_gnd_hospitalmapping -select distinct -case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code, -ins_cd_nl as inst_code -from dwd.dwd_gnd_hospital_not_provided -) - -,ALL_INS as ( -select distinct inst_code -from dm.dm_ext_aia_sales -) - -,flag as ( - select distinct - coalesce(t2.inst_code,t1.cpa_hospital_code) as inst_code, - t1.flag as aia_hp_flag, - 'Y' as FLAG, -'AIA(Monthly)' as DATA_SOURCE - from hp_flag2 t1 - left join inst_mapping t2 on t1.cpa_hospital_code = t2.cpa_hospital_code - where - length(t1.flag) > 6 -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-CHPA(Monthly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'XH Data(Quarterly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'EC(Monthly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'Retail(Quarterly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'CHC(Quarterly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'THC(Quarterly)' as DATA_SOURCE -union all -select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-COUNTY(Quarterly)' as DATA_SOURCE -) - -select t1.inst_code, -NVL(t2.aia_hp_flag,'') as aia_hp_flag, -NVL(t2.FLAG,'N') as FLAG, -'AIA(Monthly)' as DATA_SOURCE -from ALL_INS t1 -left join flag t2 -on t1.inst_code = t2.inst_code - - --- COMMAND ---------- - ---连续取数机构 ----------------------------------------------------------------------------------------------------------------------- ---修改时间:20241029 ---修改人:FanXujia ---修改内容: ---根据事实表最新月,往前推12个月(含最新月),按机构 + 年月粒度汇总,如果每个月销量都不等于0,这家机构称为:滚动一年有数 ---根据事实表最新月,往前推24个月(含最新月),按机构 + 年月粒度汇总,如果每个月销量都不等于0,这家机构称为:滚动两年有数 ---2022年没有销量为正常情况,判断时仅考虑2023年01月起销量是否等于0 ----------------------------------------------------------------------------------------------------------------------- -insert overwrite table dm.dm_aia_provided_flag -with max_ym as ( -select max(ym) as max_ym -from dm.dm_ext_aia_sales -) -,ym_range as ( -select max_ym, -case when max_ym <= '202412' then cast(right(max_ym,2) as int) - else cast(12 as int) -end ym_12m, -case when max_ym <= '202312' then '202401' - when right(max_ym,2) = '12' then concat(left(max_ym,4),'01') - else concat(cast(left(max_ym, 4) - 1 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2)) -end as ym_12m_before, -case when max_ym <= '202312' then cast(right(max_ym,2) as int) - when max_ym <= '202512' then cast(right(max_ym,2) as int) + 12 - else cast(24 as int) -end as ym_24m, -case when max_ym <= '202512' then '202401' - when right(max_ym,2) = '12' then concat(cast(left(max_ym,4) - 1 as int),'01') - else concat(cast(left(max_ym, 4) -2 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2)) -end as ym_24m_before -from max_ym -) -,sales_1 as ( -select - inst_code, - ym -from - dm.dm_ext_aia_sales -group by - ym, - inst_code -having - sum(value) <> 0 -) -,sales_2 as ( -select - t1.inst_code, - t1.ym, - t2.max_ym, - t2.ym_12m, - t2.ym_12m_before, - t2.ym_24m, - t2.ym_24m_before -from sales_1 t1 -cross join ym_range t2 -) -,flag_12m as ( -select inst_code, -'滚动一年有数' as aia_provided_flag, -'AIA(Monthly)' as DATA_SOURCE -from sales_2 -where ym <= max_ym -and ym >= ym_12m_before -group by inst_code -having count(1) = (select ym_12m from ym_range) -) -,flag_24m as ( -select inst_code, -'滚动两年有数' as aia_provided_flag, -'AIA(Monthly)' as DATA_SOURCE -from sales_2 -where ym <= max_ym -and ym >= ym_24m_before -group by inst_code -having count(1) = (select ym_24m from ym_range) -) - -select inst_code, -aia_provided_flag, -DATA_SOURCE -from flag_12m -union all -select inst_code, -aia_provided_flag, -DATA_SOURCE -from flag_24m -union all -select '' as inst_code,'' as aia_provided_flag,'IQVIA-CHPA(Monthly)' as data_source -union all -select '' as inst_code,'' as aia_provided_flag,'XH Data(Quarterly)' as data_source -union all -select '' as inst_code,'' as aia_provided_flag,'EC(Monthly)' as data_source -union all -select '' as inst_code,'' as aia_provided_flag,'Retail(Quarterly)' as data_source -union all -select '' as inst_code,'' as aia_provided_flag,'CHC(Quarterly)' as data_source -union all -select '' as inst_code,'' as aia_provided_flag,'THC(Quarterly)' as data_source -union all -select '' as inst_code,'' as aia_provided_flag,'IQVIA-COUNTY(Quarterly)' as data_source - - --- COMMAND ---------- - ---------------------------------------------------------------------------- ---修改时间:20241108 ---修改人:Fanxujia ---修改内容: ---for AIA Dashboard ---增加sub_channel,dragon_flag,ka_flag,target_ins_level字段 ---------------------------------------------------------------------------- ---------------------------------------------------------------------------- ---修改时间:20241209 ---修改人:Fanxujia ---修改内容: ---增加sub channel的排序 ---------------------------------------------------------------------------- ---目标医院 -insert overwrite table dm.dm_aia_targethp_flag --获取全部的机构 + 市场 -with sales as ( - select distinct t1.inst_code,t2.MARKET - from dm.dm_ext_aia_sales t1 - left join dm.dm_aia_pack_property t2 - on t1.pack_cod = t2.PACK_COD -) ---获取产品与市场的映射关系 -,pack_market as ( -select distinct -PACK_COD, -MARKET -from dm.dm_aia_pack_property -) ---辖区表,获取到KA_Flag、target_ins_level以及目标机构Flag(在辖区表里存在的就是目标机构) -,td_territory_inst as ( -select distinct -inst_code, -trty_code, -family_code, -ka_flag, -TARGET_LEVEL as target_ins_level -from dm.dm_td_sd_territory_indication --eagle的非重点医院同样加为目标机构 -where yyyymm in ( - select - key_value - from - dm.dm_td_date_config - where - key_code = 'pfmc_cvh' - ) -and bu not in ('CUBE','KA') -and key_hp<>3 --chenwu 20250904 排除双考影响 -) ---汇报线表,获取到Channel,后续用于判断出sub_channel -,td_org as ( -select distinct -t1.mr_trty_code, -case when t1.bu_code in ('PC14','PC16','PC18','PC19') then 'BU' - when t1.bu_code = 'SC01' and t1.sub_bu_code = 'NA29' then sub_bu_name - when t1.bu_code = 'SC01' then 'BU' - when t1.bu_code <> 'SC01' and t2.bm_name = 'BCBH' then 'BU' - when t1.bu_code <> 'SC01' then t1.bu_name -end as channel -from dm.dm_td_org t1 -left join dws.dws_td_bu_active t2 -on t1.bu_name = t2.bu_name -where t1.yyyymm in ( - select - key_value - from - dm.dm_td_date_config - where - key_code = 'pfmc_cvh' - ) -) ---Family粒度和Brand粒度的映射表 -,product_wide as ( -select distinct -prd_family_cd, -prd_brand_cd -from cdw_dwd_dim_product_wide -) ---内部Brand粒度与外部pack_cod的映射表 -,imscode_azcode as ( -select distinct -brandcode, -case when length(ims_pack_cod) < 12 and ims_pack_cod REGEXP '^[0-9]' then right(concat('00000000000',ims_pack_cod),12) else ims_pack_cod end as ims_pack_cod -from dwd.dwd_td_imscode_azcode -) ---综合上面的几个表,得到KA_flag、target_ins_level、channel、目标机构Flag -,target_hp_all as ( -select - distinct - t1.inst_code, - t1.ka_flag, - t1.target_ins_level, - t2.channel, - t5.MARKET -from - td_territory_inst t1 - inner join td_org t2 on t1.trty_code = t2.mr_trty_code - left join product_wide t3 on t3.prd_family_cd = t1.family_code - left join imscode_azcode t4 on t3.prd_brand_cd = t4.brandcode - left join pack_market t5 on t4.ims_pack_cod = t5.pack_cod -where t5.MARKET is not null -) ---target_hp_all里一个机构一个市场对应了多个属性,这里按优先级依次取唯一值 ---ka_flag -,ka_flag1 as ( -select distinct -inst_code, -MARKET, -ka_flag -from target_hp_all -) -,ka_flag2 as ( -select -inst_code, -MARKET, -ka_flag, -row_number() over(partition by inst_code,market - order by case when ka_flag = 'KA' then 1 - ELSE 2 end asc) as RN -from ka_flag1 -) -,ka_flag_final as ( -select -inst_code, -MARKET, -ka_flag -from ka_flag2 -where RN = 1 -) ---target_ins_level不取唯一值 ---加个hardcoding,ONC Market和ALL Market设置成NA -,target_ins_level1 as ( -select distinct -inst_code, -MARKET, -case when MARKET in ('AIA ALL Market','ONC Market') then 'NA' - else target_ins_level -end as target_ins_level -from target_hp_all -) -,target_ins_level_final as ( -select distinct -inst_code, -MARKET, -target_ins_level -from target_ins_level1 -) ---target_hp_all里一个机构一个市场对应了多个属性,这里按优先级依次取唯一值 ---Channel -,Channel1 as ( -select distinct -inst_code, -MARKET, -channel -from target_hp_all -) -,channel2 as ( -select -inst_code, -MARKET, -channel, -row_number() over(partition by inst_code,market - order by case when channel = 'BU' then 1 - when channel = 'BBU_County' then 2 - when channel = 'CHC' then 3 - when channel = 'Eagle' then 4 - ELSE 5 end asc) as RN -from channel1 -) -,channel_final as ( -select -inst_code, -MARKET, -channel -from channel2 -where RN = 1 -) ---确定唯一值后重新放一起 -,target_hp1 as ( -select distinct -inst_code, -MARKET -from target_hp_all -) -,target_hp as ( -select t1.inst_code, -t1.market, -t2.ka_flag, -t3.target_ins_level, -t4.channel -from target_hp1 t1 -left join ka_flag_final t2 -on t1.inst_code = t2.inst_code -and t1.market = t2.market -left join target_ins_level_final t3 -on t1.inst_code = t3.inst_code -and t1.market = t3.market -left join channel_final t4 -on t1.inst_code = t4.inst_code -and t1.market = t4.market -) - ---获取Dragon_type ---判断Dragon_type是否为County -,county as ( -select distinct inst_code,county_tier_desc -from dm.dm_td_institution -) ---判断另外2种 --- 20250825修改 -,dragon_type as ( -select src_hcc_code as inst_code, -src_prod_code as family_code, -max(is_core) is_core -from cdw_dwd_dwd_hospital_classification -where active_status = 1 -and src_sales_cycle = (select key_value from dm.dm_td_date_config where key_code = 'pfmc_cvh') -group by src_hcc_code, -src_prod_code -) ---将Family_code转为pack_cod -,dragon_type_market1 as ( -select distinct -t1.inst_code, -t1.is_core, -t4.market -from dragon_type t1 -left join cdw_dwd_dim_product_wide t2 -on t1.family_code = t2.prd_family_cd -left join dwd.dwd_td_imscode_azcode t3 -on t2.prd_brand_cd = t3.brandcode -left join pack_market t4 -on t3.ims_pack_cod = t4.pack_cod -where t4.market is not null -) ---按优先级取唯一值 -,dragon_type_market2 as ( -select distinct -inst_code, -is_core, -market, -row_number() over(partition by inst_code,market - order by case when is_core = 'CORE' then 1 - when is_core = 'EMERGING' then 2 - else 3 end asc) as RN -from dragon_type_market1 -) -,dragon_type_market as ( -select distinct -inst_code, -is_core, -market -from dragon_type_market2 -where RN = 1 -) ---综合前面几个表,得出dragon_flag -,dragon_flag1 as ( -select distinct -t0.inst_code, -t0.market, -case when t1.county_tier_desc in ('County','County level city') then 'County' - when t2.is_core is null or length(t2.is_core) = 0 then 'Others' - else t2.is_core -end as dragon_flag -from target_hp t0 -left join county t1 -on t0.inst_code = t1.inst_code -left join dragon_type_market t2 -on t0.inst_code = t2.inst_code -and t0.MARKET = t2.MARKET -) ---按优先级取唯一值 -,dragon_flag2 as ( -select inst_code, -market, -dragon_flag, -row_number() over(partition by inst_code,market - order by case when dragon_flag = 'CORE' then 1 - when dragon_flag = 'EMERGING' then 2 - when dragon_flag = 'County' then 3 - else 4 end asc) as RN -from dragon_flag1 -) -,dragon_flag as ( -select inst_code, -market, -dragon_flag -from dragon_flag2 -where RN = 1 -) - ---下面是为了把其他渠道并进来 -,other_data_source as ( -select explode(array('IQVIA-CHPA(Monthly)','XH Data(Quarterly)','EC(Monthly)','Retail(Quarterly)','CHC(Quarterly)','THC(Quarterly)','IQVIA-COUNTY(Quarterly)')) -) -,col as ( -select '' as inst_code, -'' as MARKET, -'' as is_target_hp, -'' as sub_channel, -'' as dragon_flag, -'' as ka_flag, -'' as target_ins_level -) - -,temp_1 as ( -select distinct -case when t1.inst_code is null or t1.inst_code = '' then 'ROC' - else t1.inst_code -end as inst_code, -t1.MARKET, -case when t2.inst_code is null then 'N' else 'Y' end as is_target_hp, -case when t2.channel = 'BU' and geo.county_tier_desc in ('County','County level city') then 'BU_County' - when t2.channel = 'BU' and geo.county_tier_desc in ('City-3b','City-4a','City-4b') then 'BU_Emerging' - when t2.channel = 'BU' and t3.dragon_flag = 'CORE' then 'BU_Core' - when t2.channel = 'BU' then 'BU_Emerging' - when t2.channel in ('Eagle','CHC','BBU_County') then t2.channel - when t2.inst_code is null then 'NON_TARGET' - else 'NA' -end as sub_channel, -case when t3.dragon_flag is null then 'NON_TARGET' - else t3.dragon_flag -end as dragon_flag, -case when t2.ka_flag is null then 'NON_TARGET' - else t2.ka_flag -end as ka_flag, -case when t2.target_ins_level is null then 'NON_TARGET' - else t2.target_ins_level -end as target_ins_level, -'AIA(Monthly)' as DATA_SOURCE -from sales t1 -left join target_hp t2 -on t1.inst_code = t2.inst_code -and t1.MARKET = t2.MARKET -left join dragon_flag t3 -on t1.inst_code = t3.inst_code -and t1.MARKET = t3.MARKET -left join dm.dm_td_institution inst -on t1.inst_code =inst.inst_code -left join dm.dm_td_geography geo -on inst.county_code = geo.geo_key -union all -select * -from col -cross join other_data_source -) -,sub_channel_rank as ( -select distinct SUB_CHANNELSTD,SUB_CHANNEL_ORDER -from dm.dm_td_sd_channel -) - - -select t1.*,NVL(t2.SUB_CHANNEL_ORDER,100) as SUB_CHANNEL_ORDER -from temp_1 t1 -left join sub_channel_rank t2 -on t1.sub_channel = t2.SUB_CHANNELSTD diff --git a/EC/03 ec_load_data_backup.sql b/EC/03 ec_load_data_backup.sql deleted file mode 100644 index 0f3c432..0000000 --- a/EC/03 ec_load_data_backup.sql +++ /dev/null @@ -1,1544 +0,0 @@ --- 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 diff --git a/Merged_Data/Merged_Data_Config_table_bkp.sql b/Merged_Data/Merged_Data_Config_table_bkp.sql deleted file mode 100644 index 8c2c1af..0000000 --- a/Merged_Data/Merged_Data_Config_table_bkp.sql +++ /dev/null @@ -1,228 +0,0 @@ --- 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 diff --git a/Merged_Data/dm_tf_exteranl_sales_merged_data_dtp_others_bkp.sql b/Merged_Data/dm_tf_exteranl_sales_merged_data_dtp_others_bkp.sql deleted file mode 100644 index 7bc0611..0000000 --- a/Merged_Data/dm_tf_exteranl_sales_merged_data_dtp_others_bkp.sql +++ /dev/null @@ -1,124 +0,0 @@ --- 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 \ No newline at end of file diff --git a/Retail/04 map_to_dws_table_backup.py b/Retail/04 map_to_dws_table_backup.py deleted file mode 100644 index c6aaea1..0000000 --- a/Retail/04 map_to_dws_table_backup.py +++ /dev/null @@ -1,209 +0,0 @@ -# Databricks notebook source -############################################################START############################################################## -### STEP-1: insert splited pack data into tmp final table: tmp_retail_final_sales - -# COMMAND ---------- - -# MAGIC %sql -# MAGIC ------------------------------------------------------------------------------------- -# MAGIC -- STEP-1: insert splited pack data into tmp final table -# MAGIC -- insert into tmp_retail_final_sales -# MAGIC ------------------------------------------------------------------------------------- -# MAGIC -# MAGIC with tmp_pack as ( -# MAGIC select -# MAGIC ------------------------------------------------------ -# MAGIC -- 有月度数据使用月度数据,无月度数据用季度数据去转 -# MAGIC nvl( -# MAGIC a.month, -# MAGIC CONCAT( -# MAGIC SUBSTRING(a.quarter, 1, 4), -- 提取年份(前4位) -# MAGIC CASE -# MAGIC WHEN SUBSTRING(a.quarter, 6, 1) = '1' THEN '03' -- Q1 → 03月 -# MAGIC WHEN SUBSTRING(a.quarter, 6, 1) = '2' THEN '06' -- Q2 → 06月 -# MAGIC WHEN SUBSTRING(a.quarter, 6, 1) = '3' THEN '09' -- Q3 → 09月 -# MAGIC WHEN SUBSTRING(a.quarter, 6, 1) = '4' THEN '12' -- Q4 → 12月 -# MAGIC END -# MAGIC ) -# MAGIC ) as YYYYMM, -# MAGIC ------------------------------------------------------ -# MAGIC a.pack_code as iqvia_pack_code, -# MAGIC a.product_id as zk_product_id, -# MAGIC case when a.product_desc <> 'others' then a.product_desc else null end as prod_des_c, -# MAGIC case when a.product_desc <> 'others' then a.product_desc else concat('Others_', a.molecule_desc) end as PROD_MAPPING, -# MAGIC a.zk_regin as province_city, -# MAGIC a.level_market as market, -# MAGIC a.sales_value, -# MAGIC a.sales_unit, -# MAGIC ------------------------------------------------------ -# MAGIC -- counting_unit取值逻辑: -# MAGIC -- 不能直接取原始pack文件表中的值,改为取pack_property表中counting_unit / unit的值 -# MAGIC a.sales_unit * (b.counting_unit/ coalesce(b.unit,1)) as counting_unit, -# MAGIC ------------------------------------------------------ -# MAGIC case when data_flag = 0 then 1 else 2 end as pack_flag, -# MAGIC case when brand_flag = 1 then 1 else 2 end as brand_flag -# MAGIC from tmp.tmp_retail_pack_rawdata a -# MAGIC left join dwd.dwd_gnd_ext_retail_pack_property b -# MAGIC on a.product_id = b.product_id -# MAGIC ), tmp_has_roc as ( -# MAGIC select -# MAGIC product_id, -# MAGIC quarter, -# MAGIC ------------------------------------------------------ -# MAGIC -- 有月度数据使用月度数据,无月度数据用季度数据去转 -# MAGIC nvl( -# MAGIC month, -# MAGIC CONCAT( -# MAGIC SUBSTRING(quarter, 1, 4), -- 提取年份(前4位) -# MAGIC CASE -# MAGIC WHEN SUBSTRING(quarter, 6, 1) = '1' THEN '03' -- Q1 → 03月 -# MAGIC WHEN SUBSTRING(quarter, 6, 1) = '2' THEN '06' -- Q2 → 06月 -# MAGIC WHEN SUBSTRING(quarter, 6, 1) = '3' THEN '09' -- Q3 → 09月 -# MAGIC WHEN SUBSTRING(quarter, 6, 1) = '4' THEN '12' -- Q4 → 12月 -# MAGIC END -# MAGIC ) -# MAGIC ) as month, -# MAGIC ------------------------------------------------------ -# MAGIC pack_code -# MAGIC from tmp.tmp_retail_pack_rawdata -# MAGIC where zk_regin = 'ROC' -# MAGIC ), tmp_pack_this_year_with_roc as ( -# MAGIC select -# MAGIC * -# MAGIC from tmp_pack a -# MAGIC where exists( -# MAGIC select * from tmp_has_roc b -# MAGIC where a.YYYYMM = b.month -# MAGIC and a.iqvia_pack_code = b.pack_code -# MAGIC and a.zk_product_id = b.product_id -# MAGIC ) and a.province_city <> '全国' -# MAGIC ), tmp_pack_next_year_with_roc as ( -# MAGIC select -# MAGIC cast(YYYYMM + 100 as int) as YYYYMM, -# MAGIC iqvia_pack_code, -# MAGIC zk_product_id, -# MAGIC prod_des_c, -# MAGIC PROD_MAPPING, -# MAGIC province_city, -# MAGIC market, -# MAGIC sales_value as sales_value_ly, -# MAGIC sales_unit as sales_unit_ly, -# MAGIC counting_unit as counting_unit_ly, -# MAGIC pack_flag, -# MAGIC brand_flag -# MAGIC from tmp_pack a -# MAGIC where YYYYMM + 100 <= (select max(YYYYMM) from tmp_pack) -# MAGIC and exists( -# MAGIC select * from tmp_has_roc b -# MAGIC where a.YYYYMM = b.month -# MAGIC and a.iqvia_pack_code = b.pack_code -# MAGIC and a.zk_product_id = b.product_id -# MAGIC ) and a.province_city <> '全国' -# MAGIC -# MAGIC ), tmp_pack_this_year_without_roc as ( -# MAGIC select -# MAGIC * -# MAGIC from tmp_pack a -# MAGIC where not exists( -# MAGIC select * from tmp_has_roc b -# MAGIC where a.YYYYMM = b.month -# MAGIC and a.iqvia_pack_code = b.pack_code -# MAGIC and a.zk_product_id = b.product_id -# MAGIC ) -# MAGIC ), tmp_pack_next_year_without_roc as ( -# MAGIC select -# MAGIC cast(YYYYMM + 100 as int) as YYYYMM, -# MAGIC iqvia_pack_code, -# MAGIC zk_product_id, -# MAGIC prod_des_c, -# MAGIC PROD_MAPPING, -# MAGIC province_city, -# MAGIC market, -# MAGIC sales_value as sales_value_ly, -# MAGIC sales_unit as sales_unit_ly, -# MAGIC counting_unit as counting_unit_ly, -# MAGIC pack_flag, -# MAGIC brand_flag -# MAGIC from tmp_pack a -# MAGIC where YYYYMM + 100 <= (select max(YYYYMM) from tmp_pack) -# MAGIC and not exists( -# MAGIC select * from tmp_has_roc b -# MAGIC where a.YYYYMM = b.month -# MAGIC and a.iqvia_pack_code = b.pack_code -# MAGIC and a.zk_product_id = b.product_id -# MAGIC ) -# MAGIC ), tmp_final_sales as ( -# MAGIC select -# MAGIC ifnull(a.yyyymm, b.yyyymm) as yyyymm, -# MAGIC ifnull(a.iqvia_pack_code, b.iqvia_pack_code) as iqvia_pack_code, -# MAGIC ifnull(a.zk_product_id, b.zk_product_id) as zk_product_id, -# MAGIC ifnull(a.prod_des_c, b.prod_des_c) as prod_des_c, -# MAGIC ifnull(a.PROD_MAPPING, b.PROD_MAPPING) as PROD_MAPPING, -# MAGIC ifnull(a.province_city, b.province_city) as province_city, -# MAGIC ifnull(a.market, b.market) as market, -# MAGIC ifnull(a.sales_value, 0) as sales_value, -# MAGIC ifnull(a.sales_unit, 0) as sales_unit, -# MAGIC ifnull(a.counting_unit, 0) as counting_unit, -# MAGIC ifnull(a.pack_flag, b.pack_flag) as pack_flag, -# MAGIC ifnull(a.brand_flag,b.brand_flag ) as brand_flag, -# MAGIC ifnull(b.sales_value_ly, 0) as sales_value_ly, -# MAGIC ifnull(b.sales_unit_ly, 0) as sales_unit_ly, -# MAGIC ifnull(b.counting_unit_ly, 0) as counting_unit_ly -# MAGIC from tmp_pack_this_year_with_roc a -# MAGIC full outer join tmp_pack_next_year_with_roc b -# MAGIC on a.YYYYMM = b.YYYYMM -# MAGIC and a.iqvia_pack_code = b.iqvia_pack_code -# MAGIC and a.zk_product_id = b.zk_product_id -# MAGIC and a.province_city = b.province_city -# MAGIC -# MAGIC union all -# MAGIC -# MAGIC select -# MAGIC ifnull(c.yyyymm, d.yyyymm) as yyyymm, -# MAGIC ifnull(c.iqvia_pack_code, d.iqvia_pack_code) as iqvia_pack_code, -# MAGIC ifnull(c.zk_product_id, d.zk_product_id) as zk_product_id, -# MAGIC ifnull(c.prod_des_c, d.prod_des_c) as prod_des_c, -# MAGIC ifnull(c.PROD_MAPPING, d.PROD_MAPPING) as PROD_MAPPING, -# MAGIC 'ROC' as province_city, -# MAGIC ifnull(c.market, d.market) as market, -# MAGIC ifnull(c.sales_value, 0) as sales_value, -# MAGIC ifnull(c.sales_unit, 0) as sales_unit, -# MAGIC ifnull(c.counting_unit, 0) as counting_unit, -# MAGIC --ifnull(c.pack_flag, d.pack_flag) as pack_flag, -# MAGIC 2 as pack_flag, -- 此类没有拆分比例,且pack只有全国的数,pack_flag固定为2 -# MAGIC ifnull(c.brand_flag,d.brand_flag ) as brand_flag, -# MAGIC ifnull(d.sales_value_ly, 0) as sales_value_ly, -# MAGIC ifnull(d.sales_unit_ly, 0) as sales_unit_ly, -# MAGIC ifnull(d.counting_unit_ly, 0) as counting_unit_ly -# MAGIC from tmp_pack_this_year_without_roc c -# MAGIC full outer join tmp_pack_next_year_without_roc d -# MAGIC on c.YYYYMM = d.YYYYMM -# MAGIC and c.iqvia_pack_code = d.iqvia_pack_code -# MAGIC and c.zk_product_id = d.zk_product_id -# MAGIC and c.province_city = d.province_city -# MAGIC ) -# MAGIC -# MAGIC insert overwrite table tmp.tmp_retail_final_sales -# MAGIC -# MAGIC select -# MAGIC yyyymm, -# MAGIC iqvia_pack_code, -# MAGIC zk_product_id, -# MAGIC prod_des_c, -# MAGIC PROD_MAPPING, -# MAGIC province_city, -# MAGIC market, -# MAGIC sales_value, -# MAGIC sales_value_ly, -# MAGIC sales_unit, -# MAGIC sales_unit_ly, -# MAGIC counting_unit, -# MAGIC counting_unit_ly, -# MAGIC pack_flag, -# MAGIC brand_flag -# MAGIC from tmp_final_sales -# MAGIC order by yyyymm - -# COMMAND ---------- - -############################################################END################################################################ diff --git a/Retail/z1 dwd_inc_gnd_ext_retail_nataional_作废.py b/Retail/z1 dwd_inc_gnd_ext_retail_nataional_作废.py deleted file mode 100644 index d827c18..0000000 --- a/Retail/z1 dwd_inc_gnd_ext_retail_nataional_作废.py +++ /dev/null @@ -1,367 +0,0 @@ -# Databricks notebook source -# MAGIC %md -# MAGIC ### 原本逻辑 - -# COMMAND ---------- - -#当更新pack 或品牌 事实数据时需要运行此代码,否则无需运行。 - -# COMMAND ---------- - -# MAGIC %sql -# MAGIC update dwd.dwd_gnd_ext_retail_corresponding_relationship set table_name ='dwd.dwd_gnd_ext_retail_nataional_oap' where file_name ='pack-CV-抗血栓2通用名-全国.xlsx'; -# MAGIC update dwd.dwd_gnd_ext_retail_corresponding_relationship set table_name ='dwd.dwd_gnd_ext_retail_htn' where file_name ='pack-CV-高血压-化学药-全国.xlsx'; -# MAGIC update dwd.dwd_gnd_ext_retail_corresponding_relationship set table_name ='dwd.dwd_gnd_ext_retail_atomizer' where file_name ='pack-雾化器-全国&县域数据.xlsx'; -# MAGIC update dwd.dwd_gnd_ext_retail_corresponding_relationship set table_name ='dwd.dwd_gnd_ext_retail_anti_asthma_copd' where file_name ='pack-RE-慢阻肺-全国.xlsx'; -# MAGIC update dwd.dwd_gnd_ext_retail_corresponding_relationship set table_name ='dwd.dwd_gnd_ext_zk_brand' where file_name ='Brand-品牌数据报表.xlsx'; -# MAGIC update dwd.dwd_gnd_ext_retail_corresponding_relationship set table_name ='dwd.dwd_gnd_ext_retail_statin_xzk' where file_name ='pack-CV-他汀类+血脂康-全国.xlsx'; -# MAGIC update dwd.dwd_gnd_ext_retail_corresponding_relationship set table_name ='dwd.dwd_gnd_ext_retail_nataional_rd' where file_name ='pack-RD-肾科-全国.xlsx'; -# MAGIC update dwd.dwd_gnd_ext_retail_corresponding_relationship set table_name ='dwd.dwd_gnd_ext_retail_aagsa_ppi_oral' where file_name ='pack-GI-慢性胃炎胃溃疡-全国.xlsx'; -# MAGIC update dwd.dwd_gnd_ext_retail_corresponding_relationship set table_name ='dwd.dwd_gnd_ext_retail_nataional_niad' where file_name ='pack-DM-口服降糖化学药.xlsx'; -# MAGIC update dwd.dwd_gnd_ext_retail_corresponding_relationship set table_name ='dwd.dwd_gnd_ext_retail_metoprolol_tartrat' where file_name ='pack-CV-酒石酸美托洛尔.xlsx'; -# MAGIC - -# COMMAND ---------- - - # pack数据自动接入 整合 -#获取配置表信息(表名、brand_flag -df = spark.sql(""" -SELECT DISTINCT table_name tab ,file_name brand_flag FROM dwd.dwd_gnd_ext_retail_corresponding_relationship -where type_name ='PACK' -""").collect() - -def get_union_pack_data(df): - #初始化结果集 - union_query = None - # niad_pdot_unit需特殊赋值对应表名:tmp.tmp_inc_gnd_ext_retail_nataional_niad - # niad_pdot_unit_flag = 'tmp.tmp_inc_gnd_ext_retail_nataional_niad' - for table in df: - # 选择当前表名 - T = str(table.tab) - # 获取对应brand表维度对应得 market 名称 - brand_flag = str(table.brand_flag) - sql = f""" - select - cast(t1.month as int) AS YYYYMM - ,cast(left(t1.quarter, 4) as int) AS year - ,right(t1.quarter, 2) AS quarter - ,t1.quarter AS yq - ,t1.zk_product_id - ,t1.zk_region - ,t1.zk_rx_otc - ,t1.zk_medicine_type - ,t1.zk_medicine_tier1 - ,t1.zk_medicine_tier2 - ,t1.zk_medicine_tier3 - ,t1.zk_medicine_tier4 - ,t1.zk_common_name - ,t1.zk_dosage_form - ,t1.zk_user_type - ,t1.zk_category_name - ,t1.zk_product_name - ,t1.zk_brand_name - ,t1.zk_manu_des - ,t1.zk_corp_des - ,t1.zk_pack_des - ,t1.price - ,CAST(replace(t1.sales_unit,',','') AS decimal(30,10)) as sales_unit - ,CAST(replace(t1.sales_value,',','') AS decimal(30,10)) as sales_value - ,CAST(replace(t1.digital_spread_rate,',','') AS decimal(30,10)) as digital_spread_rate - ,CAST(replace(t1.weighted_spread_rate,',','') AS decimal(30,10)) as weighted_spread_rate - ,CAST(replace(t1.counting_unit,',','') AS decimal(30,10)) as counting_unit - ,'{brand_flag}' as 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 {T} t1 - left join dws.dws_ext_retail_td_prod t2 - on t1.zk_product_id = t2.zk_product_id - where month is not null - """ - # 读取数据 - current_query = spark.sql(sql) - #union 数据 - if union_query ==None: - union_query=current_query - else: - union_query = union_query.union(current_query) - #返回数据集 / 写入表也行??? - return union_query -pack_result = get_union_pack_data(df) -pack_result.write.mode("overwrite").saveAsTable("dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all") - -# COMMAND ---------- - - -# brand+ 省份数据自动接入 -#获取配置表信息(表名、brand_flag -dfband = spark.sql(""" -SELECT DISTINCT table_name tab ,file_name brand_flag FROM dwd.dwd_gnd_ext_retail_corresponding_relationship -where type_name ='BRAND' -""").collect() - -def get_union_brand_data(df): - #数据为空 - if df == None: - return None - #初始化结果集 - union_query = None - for table in df: - # 选择当前表名 - T = str(table.tab) - # 获取对应brand表维度对应得 market 名称 - pack_flag = str(table.brand_flag) - sql = f""" - select - cast(left(quarter, 4)*100 + right(quarter,1)*3 as int ) AS YYYYMM - ,cast(left(quarter, 4) as int ) AS year - ,right(quarter, 2) AS quarter - ,quarter AS yq - ,type AS brand_cat_type - ,case when ta = 'NIAD' then 'DM' else ta end AS TA - ,market AS market - ,zk_brand_category AS zk_brand_category - ,zk_common_name AS zk_common_name - ,zk_manu_des AS zk_manu_des - ,rc_name_en AS rc_name_en - ,province_city AS province_city - ,ytd AS ytd - ,cast(sales_value * 1000000 as decimal(30,10)) AS sales_val - ,cast(sales_volume * 1000000 as decimal(30,10)) AS sales_vol - ,cast(price as decimal(30,10)) as price - ,cast(num_dist_rate as decimal(30,10)) as num_dist_rate - ,cast(weig_dist_rate as decimal(30,10)) as weig_dist_rate - ,cast(value_share as decimal(30,10)) as val_share - ,cast(volume_share as decimal(30,10)) as vol_share - ,replace(key_brand_ytd,'-','') as key_brand_ytd - ,cast(replace(key_brand_rank_ytd,'-','0') as int) as key_brand_rank_ytd - ,replace(top_brand_ytd,'-','') as top_brand_ytd - ,cast(replace(top_brand_ms_ytd,'-','0') as decimal(30,10)) as top_brand_ms_ytd - ,cast(replace(top_brand_inc_ms_ytd,'-','0') as decimal(30,10)) as top_brand_inc_ms_ytd - ,cast(replace(top_brand_gr_ytd,'-','0') as decimal(30,10)) as top_brand_gr_ytd - ,replace(key_brand_qtd,'-','') as key_brand_qtd - ,cast(replace(key_brand_rank_qtd,'-','0') as int) as key_brand_rank_qtd - ,replace(top_brand_qtd,'-','') as top_brand_qtd - ,cast(replace(top_brand_ms_qtd,'-','0') as decimal(30,10)) as top_brand_ms_qtd - ,cast(replace(top_brand_inc_ms_qtd,'-','0') as decimal(30,10)) as top_brand_inc_ms_qtd - ,cast(replace(top_brand_gr_qtd,'-','0') as decimal(30,10)) as top_brand_gr_qtd - ,ranked_by as ranked_by - ,'{pack_flag}' as pack_flag - ,from_utc_timestamp(current_timestamp(),'UTC+8') as etl_insert_dt - ,from_utc_timestamp(current_timestamp(),'UTC+8') as etl_update_dt - from {T} - """ - # 读取数据 - current_query = spark.sql(sql) - #union 数据 - if union_query == None: - union_query = current_query - else: - union_query = union_query.union(current_query) - #返回数据集 / 写入表也行??? - return union_query -brand_result = get_union_brand_data(dfband) -brand_result.write.mode("overwrite").saveAsTable("dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all") - -# COMMAND ---------- - -# MAGIC %sql -# MAGIC ----------------------多达一、天一宁、others、氨氯地平阿托伐他汀钙 数据在 高血压和他汀血脂康里面重复,但该数据在品牌报表里面没有对应的值,会导致后续拆分到pack + 省份时得到的结果不一致,因此需要将高血压或者他汀血脂康数据 根据pack+ 全国数据按省份数平均分配,并汇总到品牌维度,写入品牌数据报表,为后续pack +全国拆分到pack + 省份 提供数据基础 -# MAGIC insert overwrite table dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all -# MAGIC with data_pack as ( -# MAGIC ----------底表获取基础数据----pack 汇总到品牌 --并按省份数量平均 -# MAGIC select brand_flag,a.YYYYMM,nvl(b.prod_des_c,'OTHERS' ) prod_des_c ,sum(a.sales_value)/sum(num) sales_value,sum(a.sales_unit)/sum(num) sales_unit ,sum(a.sales_value) sales ,sum(a.sales_unit) saleu -# MAGIC from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all a -# MAGIC left join dwd.dwd_gnd_ext_retail_pack_property b on a.zk_product_id = b.product_id -# MAGIC cross join (select count(distinct zk_region) num from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all where zk_region<>'全国') -# MAGIC where a.zk_common_name ='氨氯地平阿托伐他汀钙' and a.brand_flag in ('pack-CV-他汀类+血脂康-全国.xlsx' ,'pack-CV-高血压-化学药-全国.xlsx','pack-CV-抗血栓2通用名-全国.xlsx') -# MAGIC group by 1,2,3 -# MAGIC ),city as ( -# MAGIC ----获取省份及年月头表 -# MAGIC select distinct -# MAGIC YYYYMM -# MAGIC ,year -# MAGIC ,quarter -# MAGIC ,yq -# MAGIC ,province_city -# MAGIC from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all -# MAGIC where province_city !='全国' and ranked_by ='volume' -# MAGIC ) -# MAGIC , split as ( -# MAGIC ------分别补充全国数据、品牌数据------ -# MAGIC select -# MAGIC city.YYYYMM -# MAGIC ,year -# MAGIC ,quarter -# MAGIC ,yq -# MAGIC ,'品牌' brand_cat_type -# MAGIC ,'CV' TA -# MAGIC ,brand_flag market -# MAGIC ,prod_des_c -# MAGIC ,'' -# MAGIC ,'' -# MAGIC ,'' -# MAGIC ,province_city -# MAGIC ,'' -# MAGIC ,sales_value -# MAGIC ,sales_unit -# MAGIC ,0,0,0,0,0,'' ,0 ,'' ,0,0,0,'',0,'',0,0,0,'volume' ,'' -# MAGIC from data_pack left join city on city.YYYYMM=data_pack.YYYYMM -# MAGIC union all -# MAGIC select -# MAGIC city.YYYYMM -# MAGIC ,year -# MAGIC ,quarter -# MAGIC ,yq -# MAGIC ,'品牌' brand_cat_type -# MAGIC ,'CV' TA -# MAGIC ,brand_flag market -# MAGIC ,prod_des_c -# MAGIC ,'' -# MAGIC ,'' -# MAGIC ,'' -# MAGIC ,'全国' province_city -# MAGIC ,'' -# MAGIC ,sales sales_value -# MAGIC ,saleu sales_unit -# MAGIC ,0,0,0,0,0,'' ,0 ,'' ,0,0,0,'',0,'',0,0,0,'volume' ,'' -# MAGIC from data_pack left join (select distinct year,quarter,yyyymm,yq from city ) city on city.YYYYMM=data_pack.YYYYMM -# MAGIC -# MAGIC ) -# MAGIC select * from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all -# MAGIC union all -# MAGIC select * -# MAGIC ,from_utc_timestamp(current_timestamp(),'UTC+8') AS etl_insert_dt -# MAGIC ,from_utc_timestamp(current_timestamp(),'UTC+8') AS etl_update_dt -# MAGIC from split - -# COMMAND ---------- - -# MAGIC %sql -# MAGIC --补位 -# MAGIC with tmp as ( -# MAGIC select -# MAGIC YYYYMM, -# MAGIC year, -# MAGIC quarter, -# MAGIC yq, -# MAGIC case when length(zk_product_id) < 7 then right(concat('0000000',zk_product_id),7) else zk_product_id end as zk_product_id, -# MAGIC zk_region, -# MAGIC zk_rx_otc, -# MAGIC zk_medicine_type, -# MAGIC zk_medicine_tier1, -# MAGIC zk_medicine_tier2, -# MAGIC zk_medicine_tier3, -# MAGIC zk_medicine_tier4, -# MAGIC zk_common_name, -# MAGIC zk_dosage_form, -# MAGIC zk_user_type, -# MAGIC zk_category_name, -# MAGIC zk_product_name, -# MAGIC zk_brand_name, -# MAGIC zk_manu_des, -# MAGIC zk_corp_des, -# MAGIC zk_pack_des, -# MAGIC price, -# MAGIC sales_unit, -# MAGIC sales_value, -# MAGIC digital_spread_rate, -# MAGIC weighted_spread_rate, -# MAGIC counting_unit, -# MAGIC brand_flag, -# MAGIC etl_insert_dt, -# MAGIC etl_update_dt -# MAGIC from dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all -# MAGIC -- pack-CV-酒石酸美托洛尔.xlsx的数据没有了,所以排除 酒石酸美托洛尔 -# MAGIC where brand_flag<>'pack-CV-酒石酸美托洛尔.xlsx' -# MAGIC ) -# MAGIC -# MAGIC insert overwrite dwd.dwd_inc_gnd_ext_retail_nataional_pack_union_all -# MAGIC select * -# MAGIC from tmp; - -# COMMAND ---------- - -# MAGIC %md -# MAGIC ### 新逻辑 -# MAGIC - 修改brand数据,先拆分成月维度的数据 - -# COMMAND ---------- - -# MAGIC %sql -# MAGIC /* -# MAGIC 修改时间:20250311 -# MAGIC 修改人:chenwu -# MAGIC 修改内容:brand来数频率为 季度来数, 但是 pack 为 月度来数据,需要用季度的数据/3得到月度的 -# MAGIC */ -# MAGIC insert overwrite table dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all -# MAGIC with quarterly_table as ( -# MAGIC select -# MAGIC * -# MAGIC from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all -# MAGIC where market not in ('NIAD','Inhaled Extended Market','布地奈德雾化溶液') -# MAGIC -- 范围内只能是 季度来数据的,如果有月度来数据的需要排除掉 -# MAGIC ) -# MAGIC -# MAGIC ,month_table as (--转化成月度数据 -# MAGIC SELECT -# MAGIC SUBSTR(q.yq, 1, 4)*100 + -- 提取年份 -# MAGIC LPAD(m.month_num, 2, '0') -- 补零月份 -# MAGIC AS YYYYMM -- 月份首日 -# MAGIC ,`year` -# MAGIC ,`quarter` -# MAGIC ,yq -# MAGIC ,brand_cat_type -# MAGIC ,TA -# MAGIC ,market -# MAGIC ,zk_brand_category -# MAGIC ,zk_common_name -# MAGIC ,zk_manu_des -# MAGIC ,rc_name_en -# MAGIC ,province_city -# MAGIC ,ytd -# MAGIC ,sales_val /3 --除3 -# MAGIC ,sales_vol /3 --除3 -# MAGIC ,price -# MAGIC ,num_dist_rate -# MAGIC ,weig_dist_rate -# MAGIC ,val_share -# MAGIC ,vol_share -# MAGIC ,key_brand_ytd -# MAGIC ,key_brand_rank_ytd -# MAGIC ,top_brand_ytd -# MAGIC ,top_brand_ms_ytd -# MAGIC ,top_brand_inc_ms_ytd -# MAGIC ,top_brand_gr_ytd -# MAGIC ,key_brand_qtd -# MAGIC ,key_brand_rank_qtd -# MAGIC ,top_brand_qtd -# MAGIC ,top_brand_ms_qtd -# MAGIC ,top_brand_inc_ms_qtd -# MAGIC ,top_brand_gr_qtd -# MAGIC ,ranked_by -# MAGIC ,pack_flag -# MAGIC ,etl_insert_dt -# MAGIC ,etl_update_dt -# MAGIC FROM -# MAGIC quarterly_table q -# MAGIC LATERAL VIEW EXPLODE( -- 为每季度生成三个月 -# MAGIC CASE -# MAGIC WHEN RIGHT(q.yq, 2) = 'Q1' THEN ARRAY(1, 2, 3) -# MAGIC WHEN RIGHT(q.yq, 2) = 'Q2' THEN ARRAY(4, 5, 6) -# MAGIC WHEN RIGHT(q.yq, 2) = 'Q3' THEN ARRAY(7, 8, 9) -# MAGIC WHEN RIGHT(q.yq, 2) = 'Q4' THEN ARRAY(10, 11, 12) -# MAGIC END -# MAGIC ) m AS month_num -# MAGIC ) -# MAGIC -# MAGIC ,other_not_quarterly_table ( -# MAGIC select -# MAGIC * -# MAGIC from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all -# MAGIC where market in ('NIAD','Inhaled Extended Market','布地奈德雾化溶液') -# MAGIC -- 范围内只能是 月度来数据的 -# MAGIC ) -# MAGIC -# MAGIC select * from month_table -# MAGIC union all -# MAGIC select * from other_not_quarterly_table \ No newline at end of file diff --git a/Retail/z2 retail_load_data_作废.sql b/Retail/z2 retail_load_data_作废.sql deleted file mode 100644 index f5142ab..0000000 --- a/Retail/z2 retail_load_data_作废.sql +++ /dev/null @@ -1,6276 +0,0 @@ --- 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 ) - \ No newline at end of file diff --git a/Retail/z3 retail_overview_data_作废.sql b/Retail/z3 retail_overview_data_作废.sql deleted file mode 100644 index f378188..0000000 --- a/Retail/z3 retail_overview_data_作废.sql +++ /dev/null @@ -1,1201 +0,0 @@ --- Databricks notebook source --- MAGIC %md --- MAGIC 此代码主要加工 free report 中retail 自有部分的 overview 文件 和 b2c配置表维度 及品牌数据报表指标导出 --- MAGIC * 1.dm_zk_retail_overview_data overview 文件union 表格 --- MAGIC * 2.dm_zk_retail_product_info B2C 配置表的维度(底表直接抽) --- MAGIC * 3.dm_zk_retail_brand 品牌数据报表中的指标 --- MAGIC - --- COMMAND ---------- - ----------------------------------------------------------------------------------------- ---修改时间:20240821 ---修改人:FanXujia ---修改内容: ---导出模板整合,由原来的十几个文件改为3个文件 ----------------------------------------------------------------------------------------- - --- COMMAND ---------- - --- -------------------------------------------将所有overview 数据整合在同一张表里-------------------------------------------------------------- --- -- 注意事项: --- --rank1字段:Total行排名设为99,其他行如果数据源是null,则设为98。总之不能为null。 --- -- 每个数据源文件(file_name)判断是否Total行的方式不尽相同,具体参考代码。 --- --sales_quarter字段:数据源中有两种格式,24Q1以及2024Q1,因此需要统一成2024Q1这种格式。 --- -- 有些因为是YTD的值,源文件里sales_quarter是null,需将YTD字段中的时间拆出来作为sales_quarter字段的值 --- --最终表的source_name字段的内容是各个TA,RC字段是南区、北区等。而数据源文件中都叫source_name,每个源文件需按实际情况拆分成2列 --- --yyyymm字段:Q1是3月、Q2是6月、Q3是9月、Q4是12月。 -insert overwrite table dws.dws_zk_retail_overview_data -( -rank1 -,product_desc -,product_type -,common_name -,brand -,corp -,corporation -,corp_type -,sales_quarter -,yyyymm -,ytd -,sales_amount -,sales_amount_ly -,sales_amount_total -,sales_amount_ly_total -,rx_type -,top1_brand_val -,top1_brand_gr -,top2_brand_val -,top2_brand_gr -,top1_incremental_brand -,top1_incremental_gr -,top2_incremental_brand -,top2_incremental_gr -,ytd_gr -,ytd_ms -,ytd_delta_ms -,dtp_name -,file_name -,source_name -,rc -,rx_otc -,top_incre -,etl_insert_dt -,etl_update_dt -) -with overview as -( --- select --- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' --- when rank1 is null then '98' --- else replace(rank1,' ','') end as rank1 --- ,null as product_desc --- ,null as product_type --- ,null as common_name --- ,null as brand --- ,corp_desc as corp --- ,corporation --- ,corp_type --- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) --- else sales_quarter --- end as sales_quarter --- ,null as ytd --- ,sales_amount --- ,null as rx_type --- ,null as top1_brand_val --- ,null as top1_brand_gr --- ,null as top2_brand_val --- ,null as top2_brand_gr --- ,null as top1_incremental_brand --- ,null as top1_incremental_gr --- ,null as top2_incremental_brand --- ,null as top2_incremental_gr --- ,ytd_gr --- ,ytd_ms --- ,ytd_delta_ms --- ,null as dtp_name --- ,'全国-Rx-TOP集团' file_name --- ,null source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_nataional_top_corp --- union all --- select --- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' --- when rank1 is null then '98' --- else replace(rank1,' ','') end as rank1 --- ,product_desc --- ,product_type --- ,common_name --- ,brand --- ,corp --- ,corporation --- ,null as corp_type --- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) --- else sales_quarter --- end as sales_quarter --- ,null as ytd --- ,sales_amount --- ,null as rx_type --- ,null as top1_brand_val --- ,null as top1_brand_gr --- ,null as top2_brand_val --- ,null as top2_brand_gr --- ,null as top1_incremental_brand --- ,null as top1_incremental_gr --- ,null as top2_incremental_brand --- ,null as top2_incremental_gr --- ,ytd_gr --- ,ytd_ms --- ,ytd_delta_ms --- ,null as dtp_name --- ,'全国-Rx-TOP产品' file_name --- ,null source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_nataional_top_product --- union all --- select --- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' --- when rank1 is null then '98' --- else replace(rank1,' ','') end as rank1 --- ,product_desc --- ,product_type --- ,common_name --- ,brand --- ,corp --- ,corporation --- ,null as corp_type --- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) --- else sales_quarter --- end as sales_quarter --- ,null as ytd --- ,sales_amount --- ,null as rx_type --- ,null as top1_brand_val --- ,null as top1_brand_gr --- ,null as top2_brand_val --- ,null as top2_brand_gr --- ,null as top1_incremental_brand --- ,null as top1_incremental_gr --- ,null as top2_incremental_brand --- ,null as top2_incremental_gr --- ,ytd_gr --- ,ytd_ms --- ,ytd_delta_ms --- ,null as dtp_name --- ,'全国-Rx-TOP增量产品' file_name --- ,null source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_nataional_top_incr_product --- union all --- select --- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' --- when rank1 is null then '98' --- else replace(rank1,' ','') end as rank1 --- ,null as product_desc --- ,null as product_type --- ,null as common_name --- ,null as brand --- ,corp_desc corp --- ,corporation --- ,corp_type --- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) --- else sales_quarter --- end as sales_quarter --- ,null as ytd --- ,sales_amount --- ,null as rx_type --- ,null as top1_brand_val --- ,null as top1_brand_gr --- ,null as top2_brand_val --- ,null as top2_brand_gr --- ,null as top1_incremental_brand --- ,null as top1_incremental_gr --- ,null as top2_incremental_brand --- ,null as top2_incremental_gr --- ,ytd_gr --- ,ytd_ms --- ,ytd_delta_ms --- ,null as dtp_name --- ,'全国-Rx+OTC-TOP集团' file_name --- ,null source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_nataional_rx_otc_corp - --- union all --- select --- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' --- when rank1 is null then '98' --- else replace(rank1,' ','') end as rank1 --- ,product_desc --- ,product_type --- ,common_name --- ,brand --- ,corp --- ,corporation --- ,null as corp_type --- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) --- else sales_quarter --- end as sales_quarter --- ,null as ytd --- ,sales_amount --- ,null as rx_type --- ,null as top1_brand_val --- ,null as top1_brand_gr --- ,null as top2_brand_val --- ,null as top2_brand_gr --- ,null as top1_incremental_brand --- ,null as top1_incremental_gr --- ,null as top2_incremental_brand --- ,null as top2_incremental_gr --- ,ytd_gr --- ,ytd_ms --- ,ytd_delta_ms --- ,null as dtp_name --- ,'全国-Rx+OTC-TOP产品' file_name --- ,null source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_nataional_rx_otc_product - - --- union all --- select --- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' --- when rank1 is null then '98' --- else replace(rank1,' ','') end as rank1 --- ,null as product_desc --- ,null as product_type --- ,null as common_name --- ,null as brand --- ,corp_desc as corp --- ,corp_name --- ,corp_type --- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) --- else sales_quarter --- end as sales_quarter --- ,null as ytd --- ,sales_amount --- ,null as rx_type --- ,null as top1_brand_val --- ,null as top1_brand_gr --- ,null as top2_brand_val --- ,null as top2_brand_gr --- ,null as top1_incremental_brand --- ,null as top1_incremental_gr --- ,null as top2_incremental_brand --- ,null as top2_incremental_gr --- ,ytd_gr --- ,ytd_ms --- ,ytd_delta_ms --- ,null as dtp_name --- ,'全国-分TA-TOP集团' file_name --- ,source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_nataional_ta_top_corp - --- union all - --- select --- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' --- when rank1 is null then '98' --- else replace(rank1,' ','') end as rank1 --- ,product_desc --- ,null as product_type --- ,common_name --- ,null as brand --- ,corp --- ,corp_name corporation --- ,null as corp_type --- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) --- else sales_quarter --- end as sales_quarter --- ,null as ytd --- ,sales_amount --- ,null as rx_type --- ,null as top1_brand_val --- ,null as top1_brand_gr --- ,null as top2_brand_val --- ,null as top2_brand_gr --- ,null as top1_incremental_brand --- ,null as top1_incremental_gr --- ,null as top2_incremental_brand --- ,null as top2_incremental_gr --- ,ytd_gr --- ,ytd_ms --- ,ytd_delta_ms --- ,null as dtp_name --- ,'全国-分TA-TOP产品' file_name --- ,source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_nataional_ta_top_product --- union all --- select --- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' --- when rank1 is null then '98' --- else replace(rank1,' ','') end as rank1 --- ,product_desc --- ,null as product_type --- ,common_name --- ,null as brand --- ,corp --- ,corp_name as corporation --- ,null as corp_type --- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) --- else sales_quarter --- end as sales_quarter --- ,null as ytd --- ,sales_amount --- ,null as rx_type --- ,null as top1_brand_val --- ,null as top1_brand_gr --- ,null as top2_brand_val --- ,null as top2_brand_gr --- ,null as top1_incremental_brand --- ,null as top1_incremental_gr --- ,null as top2_incremental_brand --- ,null as top2_incremental_gr --- ,ytd_gr --- ,ytd_ms --- ,ytd_delta_ms --- ,null as dtp_name --- ,'全国-分TA-TOP增量产品' file_name --- ,source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_nataional_ta_incr_product - --- union all --- select --- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' --- when rank1 is null then '98' --- else replace(rank1,' ','') end as rank1 --- ,null as product_desc --- ,null as product_type --- ,null as common_name --- ,null as brand --- ,corp_desc as corp --- ,corporation --- ,corp_type --- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) --- else sales_quarter --- end as sales_quarter --- ,null as ytd --- ,sales_amount --- ,null as rx_type --- ,null as top1_brand_val --- ,null as top1_brand_gr --- ,null as top2_brand_val --- ,null as top2_brand_gr --- ,null as top1_incremental_brand --- ,null as top1_incremental_gr --- ,null as top2_incremental_brand --- ,null as top2_incremental_gr --- ,ytd_gr --- ,ytd_ms --- ,ytd_delta_ms --- ,null as dtp_name --- ,'分大区-Rx-TOP集团' file_name --- ,null source_name --- ,source_name rc --- from dwd.dwd_gnd_ext_retail_area_top_corp - --- union all --- select --- case when upper(type_name) = 'RX' then '99' --- else '98' end as rank1 --- ,null as product_desc --- ,null as product_type --- ,null as common_name --- ,null as brand --- ,null as corp --- ,null as corporation --- ,type_name as corp_type --- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) --- else sales_quarter --- end as sales_quarter --- ,null as ytd --- ,sales_amount --- ,null as rx_type --- ,null as top1_brand_val --- ,null as top1_brand_gr --- ,null as top2_brand_val --- ,null as top2_brand_gr --- ,null as top1_incremental_brand --- ,null as top1_incremental_gr --- ,null as top2_incremental_brand --- ,null as top2_incremental_gr --- ,null as ytd_gr --- ,null as ytd_ms --- ,null as ytd_delta_ms --- ,null as dtp_name --- ,'Rx市场' file_name --- ,null as source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_rx_market ---这个没有ytd 三个字段添加 - --- union all --- select --- case when upper(corporation) like 'TOTAL %' then '99' --- else '98' end as rank1 --- ,null as product_desc --- ,null as product_type --- ,null as common_name --- ,null as brand --- ,null as corp --- ,corporation --- ,null as corp_type --- ,concat('20',trim(replace(quarter_flag,'YTD','' ))) as sales_quarter --- ,quarter_flag as ytd --- ,null as sales_amount --- ,null as rx_type --- ,replace(top1_brand_val,'-','') top1_brand_val --- ,top1_brand_gr top1_brand_gr --- ,replace(top2_brand_val,'-','') top2_brand_val --- ,top2_brand_gr top2_brand_gr --- ,replace(top1_incremental_brand,'-','') top1_incremental_brand --- ,top1_incremental_gr --- ,replace(top2_incremental_brand,'-','') top2_incremental_brand --- ,top2_incremental_gr --- ,null as ytd_gr --- ,null as ytd_ms --- ,null as ytd_delta_ms --- ,null as dtp_name --- ,'Rx-TOP集团' file_name --- ,null as source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_rx_top_corp ---这个没有ytd 三个字段添加 --- union all - --- select --- case when upper(corporation) like '% TTL' then '99' --- else '98' end as rank1 --- ,null as product_desc --- ,null as product_type --- ,null as common_name --- ,null as brand --- ,null as corp --- ,corporation --- ,null as corp_type --- ,concat('20',trim(replace(quarter_flag,'YTD','' ))) as sales_quarter --- ,quarter_flag as ytd --- ,NULL as sales_amount --- ,rx_type --- ,replace(top1_brand_val,'-','') as top1_brand_val --- ,top1_brand_gr --- ,replace(top2_brand_val,'-','') as top2_brand_val --- , top2_brand_gr --- ,replace(top1_incremental_brand,'-','') as top1_incremental_brand --- , top1_incremental_gr --- ,replace(top2_incremental_brand,'-','') as top2_incremental_brand --- ,top2_incremental_gr --- ,null as ytd_gr --- ,null as ytd_ms --- ,null as ytd_delta_ms --- ,null as dtp_name --- ,'分TA-TOP集团' file_name --- ,trim(replace(rx_type,' TTL','' ) ) as source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_rx_ta_top_corp ---这个没有ytd 三个字段添加 --- union all - --- select --- '99' as rank1 --- ,null as product_desc --- ,null as product_type --- ,null as common_name --- ,null as brand --- ,null as corp --- ,null as corporation --- ,null as corp_type --- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) --- else sales_quarter --- end as sales_quarter --- ,null as ytd --- ,sales_amount --- ,null as rx_type --- ,null as top1_brand_val --- ,null as top1_brand_gr --- ,null as top2_brand_val --- ,null as top2_brand_gr --- ,null as top1_incremental_brand --- ,null as top1_incremental_gr --- ,null as top2_incremental_brand --- ,null as top2_incremental_gr --- ,null as ytd_gr --- ,null as ytd_ms --- ,null as ytd_delta_ms --- ,dtp_name --- ,'DTP 整体市场' file_name --- ,NULL as source_name --- ,null rc --- from dwd.dwd_gnd_ext_retail_dtp_overall_market ---这个没有ytd 三个字段添加 rx_type - - --- union all -select - case when replace(upper(corp_name),'-','') = 'DTP TOTAL' then '99' - when rank1 is null then '98' - else replace(rank1,' ','') end as rank1 - ,null as product_desc - ,null as product_type - ,null as common_name - ,null as brand - ,replace(corp_name,'-','') as corp - ,null as corporation - ,null as corp_type - ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) - else sales_quarter - end as sales_quarter - ,null as ytd - ,sales_amount - ,null as rx_type - ,null as top1_brand_val - ,null as top1_brand_gr - ,null as top2_brand_val - ,null as top2_brand_gr - ,null as top1_incremental_brand - ,null as top1_incremental_gr - ,null as top2_incremental_brand - ,null as top2_incremental_gr - ,null as ytd_gr - ,null as ytd_ms - ,null as ytd_delta_ms - ,null as dtp_name - ,'DTP TOP10企业排名' file_name - ,NULL as source_name - ,null rc - ,'Rx' as rx_otc - ,'TOP' as top_incre -from dwd.dwd_gnd_ext_retail_dtp_top_copd ---这个没有ytd 三个字段添加 -union all -select - case when replace(upper(brand_name),'-','') = 'DTP TOTAL' then '99' - when rank1 is null then '98' - else replace(rank1,' ','') end as rank1 - ,null as product_desc - ,null as product_type - ,null as common_name - ,brand_name as brand - ,replace(corp_name,'-','') as corp - ,null as corporation - ,null as corp_type - ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) - else sales_quarter - end as sales_quarter - ,null as ytd - ,sales_amount - ,null as rx_type - ,null as top1_brand_val - ,null as top1_brand_gr - ,null as top2_brand_val - ,null as top2_brand_gr - ,null as top1_incremental_brand - ,null as top1_incremental_gr - ,null as top2_incremental_brand - ,null as top2_incremental_gr - ,null as ytd_gr - ,null as ytd_ms - ,null as ytd_delta_ms - ,null as dtp_name - ,'DTP TOP10品牌排名' file_name - ,NULL as source_name - ,null rc - ,'Rx' as rx_otc - ,'TOP' as top_incre -from dwd.dwd_gnd_ext_retail_dtp_top_brand ---这个没有ytd 三个字段添加 -union all - -select - case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' - when rank1 is null then '98' - else replace(rank1,' ','') end as rank1 - ,null as product_desc - ,null as product_type - ,null as common_name - ,null as brand - ,corp_desc as corp - ,corporation - ,`type` as corp_type - ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) - else sales_quarter - end as sales_quarter - ,null as ytd - ,sales_amount - ,null as rx_type - ,null as top1_brand_val - ,null as top1_brand_gr - ,null as top2_brand_val - ,null as top2_brand_gr - ,null as top1_incremental_brand - ,null as top1_incremental_gr - ,null as top2_incremental_brand - ,null as top2_incremental_gr - ,null as ytd_gr - ,null as ytd_ms - ,null as ytd_delta_ms - ,null as dtp_name - ,'全国-集团排名' file_name - ,case when ta = 'GI' then concat(ta,'-',rx_otc) - when ta = 'NIAD-excl. GLP1' then 'DM' - else ta end as source_name - ,null rc - ,rx_otc - ,case when upper(top_top_incre) = 'TOP SALES' then 'TOP' else 'Incre' end as top_incre -from dwd.dwd_gnd_retail_national_corp_rank - -union all -select - case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' - when rank1 is null then '98' - else replace(rank1,' ','') end as rank1 - ,prod_desc as product_desc - ,`type` as product_type - ,cmps_desc as common_name - ,brand - ,corp_desc as corp - ,corporation - ,null as corp_type - ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) - else sales_quarter - end as sales_quarter - ,null as ytd - ,sales_amount - ,null as rx_type - ,null as top1_brand_val - ,null as top1_brand_gr - ,null as top2_brand_val - ,null as top2_brand_gr - ,null as top1_incremental_brand - ,null as top1_incremental_gr - ,null as top2_incremental_brand - ,null as top2_incremental_gr - ,null as ytd_gr - ,null as ytd_ms - ,null as ytd_delta_ms - ,null as dtp_name - ,'全国-产品排名' file_name - ,case when ta = 'GI' then concat(ta,'-',rx_otc) - when ta = 'NIAD-excl. GLP1' then 'DM' - else ta end as source_name - ,null rc - ,rx_otc - ,case when upper(top_top_incre) = 'TOP SALES' then 'TOP' else 'Incre' end as top_incre -from dwd.dwd_gnd_retail_national_prd_rank -union all -select - case when upper(replace(rank1,' ','')) = 'TOTAL' then '99' - when rank1 is null then '98' - else replace(rank1,' ','') end as rank1 - ,null as product_desc - ,null as product_type - ,null as common_name - ,null as brand - ,corp_desc as corp - ,corporation - ,`type` as corp_type - ,case when length(sales_quarter) = 4 then concat('20',sales_quarter) - else sales_quarter - end as sales_quarter - ,null as ytd - ,sales_amount - ,null as rx_type - ,null as top1_brand_val - ,null as top1_brand_gr - ,null as top2_brand_val - ,null as top2_brand_gr - ,null as top1_incremental_brand - ,null as top1_incremental_gr - ,null as top2_incremental_brand - ,null as top2_incremental_gr - ,null as ytd_gr - ,null as ytd_ms - ,null as ytd_delta_ms - ,null as dtp_name - ,'大区-集团排名' file_name - ,case when ta = 'GI' then concat(ta,'-',rx_otc) - when ta = 'NIAD-excl. GLP1' then 'DM' - else ta end as source_name - ,region rc - ,rx_otc - ,case when upper(top_top_incre) = 'TOP SALES' then 'TOP' else 'Incre' end as top_incre -from dwd.dwd_gnd_retail_region_corp_rank -) - --- --计算Total值,按文件、季度、TA、rx_otc、top_incre进行合计 --- --因为数据中本身就包含了Total行,因此直接筛选了rank1 = '99'(即筛选Total行)进行合计 -,overview_total as ( -select file_name, - source_name, - rx_otc, - top_incre, - sales_quarter, - rc, - sum(sales_amount) sales_amount_total -from overview -where rank1 = '99' -group by file_name,sales_quarter,rx_otc,top_incre,source_name,rc -) - --- --取到文件中的最大时间,在获取ly值的时候,用于限定时间范围 -,overview_maxyq as ( -select max(sales_quarter) max_sales_quarter - from overview -where sales_quarter is not null -) - --- --在数据源的基础上,增加yyyymm字段、sales_amount_ly、sales_amount_total、sales_amount_ly_total字段 -select case when max(rank1) < 10000 then max(rank1) else cast(max(rank1) as int) - 10000 end as rank1 -,product_desc -,product_type -,common_name -,brand -,corp -,corporation -,corp_type -,sales_quarter -,case when right(sales_quarter,1) = '1' then concat(left(sales_quarter,4),'03') - when right(sales_quarter,1) = '2' then concat(left(sales_quarter,4),'06') - when right(sales_quarter,1) = '3' then concat(left(sales_quarter,4),'09') - when right(sales_quarter,1) = '4' then concat(left(sales_quarter,4),'12') - end as yyyymm -,max(ytd) ytd -,sum(sales_amount) as sales_amount -,sum(sales_amount_ly) as sales_amount_ly -,sum(sales_amount_total) as sales_amount_total -,sum(sales_amount_ly_total) as sales_amount_ly_total -,rx_type -,max(top1_brand_val) top1_brand_val -,max(top1_brand_gr) top1_brand_gr -,max(top2_brand_val) top2_brand_val -,max(top2_brand_gr) top2_brand_gr -,max(top1_incremental_brand) top1_incremental_brand -,max(top1_incremental_gr) top1_incremental_gr -,max(top2_incremental_brand) top2_incremental_brand -,max(top2_incremental_gr) top2_incremental_gr -,max(ytd_gr) ytd_gr -,max(ytd_ms) ytd_ms -,max(ytd_delta_ms) ytd_delta_ms -,dtp_name -,file_name -,source_name -,rc -,rx_otc -,top_incre -,from_utc_timestamp(current_timestamp(),'UTC+8') etl_insert_dt -,from_utc_timestamp(current_timestamp(),'UTC+8') etl_update_dt -from -( -select -cast(rank1 as int) + 10000 rank1 -,product_desc -,product_type -,common_name -,brand -,corp -,corporation -,corp_type -,t1.sales_quarter -,ytd -,sales_amount -,0 as sales_amount_ly -,t2.sales_amount_total -,0 as sales_amount_ly_total -,rx_type -,top1_brand_val -,top1_brand_gr -,top2_brand_val -,top2_brand_gr -,top1_incremental_brand -,top1_incremental_gr -,top2_incremental_brand -,top2_incremental_gr -,ytd_gr -,ytd_ms -,ytd_delta_ms -,dtp_name -,t1.file_name -,t1.source_name -,t1.rc -,t1.rx_otc -,t1.top_incre -from overview t1 -left join overview_total t2 -on t1.file_name = t2.file_name -and nvl(t1.sales_quarter,'') = nvl(t2.sales_quarter,'') -and t1.rx_otc = t2.rx_otc -and t1.top_incre = t2.top_incre -and NVL(t1.source_name,'') = NVL(t2.source_name,'') -and NVL(t1.rc,'') = NVL(t2.rc,'') -union all -select -cast(rank1 as int) rank1 -,product_desc -,product_type -,common_name -,brand -,corp -,corporation -,corp_type -,concat(cast(left(t1.sales_quarter,4) as int) + 1,right(t1.sales_quarter,2)) as sales_quarter -,null as ytd -,0 as sales_amount -,sales_amount as sales_amount_ly -,0 as sales_amount_total -,t2.sales_amount_total as sales_amount_ly_total -,rx_type -,null as top1_brand_val -,null as top1_brand_gr -,null as top2_brand_val -,null as top2_brand_gr -,null as top1_incremental_brand -,null as top1_incremental_gr -,null as top2_incremental_brand -,null as top2_incremental_gr -,null as ytd_gr -,null as ytd_ms -,null as ytd_delta_ms -,dtp_name -,t1.file_name -,t1.source_name -,t1.rc -,t1.rx_otc -,t1.top_incre -from overview t1 -left join overview_total t2 -on t1.file_name = t2.file_name -and nvl(t1.sales_quarter,'') = nvl(t2.sales_quarter,'') -and t1.rx_otc = t2.rx_otc -and t1.top_incre = t2.top_incre -and NVL(t1.source_name,'') = NVL(t2.source_name,'') -and NVL(t1.rc,'') = NVL(t2.rc,'') -left join overview_maxyq t3 -on 1=1 -where concat(cast(left(t1.sales_quarter,4) as int) + 1,right(t1.sales_quarter,2)) <= t3.max_sales_quarter -) t -group by -product_desc -,product_type -,common_name -,brand -,corp -,corporation -,corp_type -,sales_quarter -,rx_type -,dtp_name -,file_name -,source_name -,rc -,rx_otc -,top_incre - --- COMMAND ---------- - --- --------将free report overview 导出的合并文件写入dm -insert overwrite table dm.dm_zk_retail_overview_data( -rank1, -product_desc, -product_type, -common_name, -brand, -corp, -corporation, -corp_type, -sales_quarter, -yyyymm, -ytd, -sales_amount, -sales_amount_ly, -sales_amount_total, -sales_amount_ly_total, -rx_type, -top1_brand_val, -top1_brand_gr, -top2_brand_val, -top2_brand_gr, -top1_incremental_brand, -top1_incremental_gr, -top2_incremental_brand, -top2_incremental_gr, -ytd_gr, -ytd_ms, -ytd_delta_ms, -dtp_name, -file_name, -source_name, -rc, -rx_otc, -top_incre, -etl_insert_dt, -etl_update_dt -) -select -trim(rank1) rank1 -,product_desc -,product_type -,common_name -,brand -,nvl(corp,'' ) corp -,corporation -,corp_type -,sales_quarter -,yyyymm -,case when ytd is null then 'N' ELSE 'Y' END ytd -,sales_amount -,sales_amount_ly -,sales_amount_total -,sales_amount_ly_total -,rx_type -,top1_brand_val -,cast(top1_brand_gr as double) top1_brand_gr -,top2_brand_val -,cast(top2_brand_gr as double) top2_brand_gr -,top1_incremental_brand -,cast(top1_incremental_gr as double) top1_incremental_gr -,top2_incremental_brand -,cast(top2_incremental_gr as double) top2_incremental_gr -,cast(ytd_gr as double ) ytd_gr -,cast(ytd_ms as double) ytd_ms -,cast(ytd_delta_ms as double) ytd_delta_ms -,dtp_name -,file_name -,trim(source_name) source_name -,rc -,rx_otc -,top_incre - ,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_overview_data - --- COMMAND ---------- - -------------------------------retail/EC原始维度,从b2c配置表直取----------------------------------------- -insert overwrite table dws.dws_zk_retail_product_info -select distinct - product_id - ,iqvia_pack_code - ,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 - ,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_pack_property; -------------------------------retail/EC原始维度,从b2c直取 写入dm----------------------------------------- -insert overwrite table dm.dm_zk_retail_product_info -select distinct - product_id - ,iqvia_pack_code - ,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 - ,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_product_info; - --- COMMAND ---------- - -------------------------------retail 自有部分需要导出数据,从品牌数据报表直取,无特殊加工----------------------------------------- ---注意事项: -----market 排除数据为:从pack 文件中通过比例拆分写入品牌数据的 品牌数据,包括多达一、天一宁等品牌,这些并非原始文件中的数据,故排除。 ----- (具体拆分代码详见:1 dwd_inc_gnd_ext_retail_nataional -insert overwrite table dws.dws_zk_retail_brand -with max_yq as ( -select TA,market,rc_name_en,province_city,ranked_by,max(yq) as yq -from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all -WHERE market NOT IN ('pack-CV-高血压-化学药-全国.xlsx','pack-CV-他汀类+血脂康-全国.xlsx') -group by TA,market,rc_name_en,province_city,ranked_by -) - -select distinct - t1.TA, - t1.market, - t1.rc_name_en, - t1.province_city - ,key_brand_ytd - ,key_brand_rank_ytd - ,top_brand_ytd - ,top_brand_ms_ytd - ,top_brand_inc_ms_ytd - ,top_brand_gr_ytd - ,key_brand_qtd - ,key_brand_rank_qtd - ,top_brand_qtd - ,top_brand_ms_qtd - ,top_brand_inc_ms_qtd - ,top_brand_gr_qtd - ,t1.ranked_by - ,from_utc_timestamp(current_timestamp(),'UTC+8') etl_insert_dt - ,from_utc_timestamp(current_timestamp(),'UTC+8') etl_update_dt -from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all t1 -inner join max_yq t2 -on t1.TA = t2.TA -and t1.market = t2.market -and t1.rc_name_en = t2.rc_name_en -and t1.province_city = t2.province_city -and t1.yq = t2.yq -and t1.ranked_by = t2.ranked_by -; - --- dm_zk_retail_brand - -------------------------------retail 自有部分需要导出数据,写入dm----------------------------------------- -insert overwrite table dm.dm_zk_retail_brand -select distinct - TA,market,rc_name_en,province_city - ,key_brand_ytd - ,cast(key_brand_rank_ytd as double) key_brand_rank_ytd - ,top_brand_ytd - ,cast(top_brand_ms_ytd as double) top_brand_ms_ytd - ,cast(top_brand_inc_ms_ytd as double) top_brand_inc_ms_ytd - ,cast(top_brand_gr_ytd as double) top_brand_gr_ytd - ,key_brand_qtd - ,cast(key_brand_rank_qtd as double ) key_brand_rank_qtd - ,top_brand_qtd - ,cast(top_brand_ms_qtd as double ) top_brand_ms_qtd - ,cast(top_brand_inc_ms_qtd as double ) top_brand_inc_ms_qtd - ,cast(top_brand_gr_qtd as double ) top_brand_gr_qtd - ,ranked_by -from dws.dws_zk_retail_brand ; - - - - - - --- COMMAND ---------- - ----------------------------------------------------------------------------------------- ---修改时间:20240821 ---修改人:FanXujia ---修改内容: ---导出模板整合,由原来的十几个文件改为3个文件 ---《全国-集团排名》:dwd.dwd_gnd_retail_national_corp_rank ---《全国-产品排名》:dwd.dwd_gnd_retail_national_prd_rank ---《大区-集团排名》:dwd.dwd_gnd_retail_region_corp_rank ----------------------------------------------------------------------------------------- -insert overwrite table dws.dws_zk_retail_export -( -ta, -rx_otc, -top_top_incre, -rank1, -corp_desc, -corporation, -type, -prod_desc, -cmps_desc, -brand, -region, -sales_quarter, -sales_amount, -file_name, -etl_insert_dt, -etl_update_dt -) ---《全国-集团排名》 -select distinct -ta, -rx_otc, -top_top_incre, -case when upper(trim(rank1)) = 'TOTAL' then '99' - when rank1 is null then '98' - else trim(rank1) end as rank1, -corp_desc, -corporation, -type, -null as prod_desc, -null as cmps_desc, -null as brand, -null as region, -sales_quarter, -sales_amount, -'全国-集团排名' as file_name, -from_utc_timestamp(current_timestamp(),'UTC+8') as etl_insert_dt, -from_utc_timestamp(current_timestamp(),'UTC+8') as etl_update_dt -from dwd.dwd_gnd_retail_national_corp_rank -union all ---《全国-产品排名》 -select distinct -ta, -rx_otc, -top_top_incre, -case when upper(trim(rank1)) = 'TOTAL' then '99' - when rank1 is null then '98' - else trim(rank1) end as rank1, -corp_desc, -corporation, -type, -prod_desc, -cmps_desc, -brand, -null as region, -sales_quarter, -sales_amount, -'全国-产品排名' as file_name, -from_utc_timestamp(current_timestamp(),'UTC+8') as etl_insert_dt, -from_utc_timestamp(current_timestamp(),'UTC+8') as etl_update_dt -from dwd.dwd_gnd_retail_national_prd_rank -union all ---《大区-集团排名》 -select distinct -ta, -rx_otc, -top_top_incre, -case when upper(trim(rank1)) = 'TOTAL' then '99' - when rank1 is null then '98' - else trim(rank1) end as rank1, -corp_desc, -corporation, -type, -null as prod_desc, -null as cmps_desc, -null as brand, -region, -sales_quarter, -sales_amount, -'大区-集团排名' as file_name, -from_utc_timestamp(current_timestamp(),'UTC+8') as etl_insert_dt, -from_utc_timestamp(current_timestamp(),'UTC+8') as etl_update_dt -from dwd.dwd_gnd_retail_region_corp_rank; - - ---写入dm表 -insert overwrite table dm.dm_zk_retail_export -( -ta, -rx_otc, -top_top_incre, -rank1, -corp_desc, -corporation, -type, -prod_desc, -cmps_desc, -brand, -region, -sales_quarter, -sales_amount, -file_name, -etl_insert_dt, -etl_update_dt -) -select -ta, -rx_otc, -top_top_incre, -rank1, -corp_desc, -corporation, -type, -prod_desc, -cmps_desc, -brand, -region, -sales_quarter, -sales_amount, -file_name, -etl_insert_dt, -etl_update_dt -from dws.dws_zk_retail_export; \ No newline at end of file diff --git a/z 01 dm_tf_external_sales.sql b/z 01 dm_tf_external_sales.sql deleted file mode 100644 index 359ca4a..0000000 --- a/z 01 dm_tf_external_sales.sql +++ /dev/null @@ -1,1187 +0,0 @@ --- Databricks notebook source --- MAGIC %md --- MAGIC ## 清空表 - --- COMMAND ---------- - -TRUNCATE TABLE dm.dm_tf_external_sales - --- COMMAND ---------- - --- MAGIC %md --- MAGIC ## 各渠道逻辑 - --- COMMAND ---------- - --- DBTITLE 1,CHPA --- DELETE FROM --- dm.dm_tf_external_sales --- WHERE --- DATA_SOURCE = 'IQVIA-CHPA(Monthly)'; - -INSERT INTO dm.dm_tf_external_sales ( - YYYYMM, - PACK_COD, - AUDIT_COD, - DATA_SOURCE, - SALES_UNIT_CAL, - SALES_UNIT_CAL_LY, - SALES_VALUE_CAL, - SALES_VALUE_CAL_LY, - CONUTING_UNIT, - CONUTING_UNIT_LY, - prescription, - prescription_ly, - PACK_FLAG, - PROD_FLAG, - DTP_FLAG, - cmps_flag, - PLATFORM_TYPE, - STORE_NAME, - STORE_TYPE, - REGION_TYPE, - inst_code, - dept_name, - new_code, - area, - h_level, - reimburse, - reimburse_type, - prescription_source, - CORP_COD - ) - SELECT - A.YYYYMM, - A.PACK_COD, - A.AUDIT_COD, - 'IQVIA-CHPA(Monthly)' DATA_SOURCE, - ------------------------------------- - SUM(SALES_UNIT_CAL) SALES_UNIT_CAL, - SUM(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY, - SUM(SALES_VALUE_CAL) SALES_VALUE_CAL, - SUM(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY, - SUM(CONUTING_UNIT) CONUTING_UNIT, - SUM(CONUTING_UNIT_LY) as CONUTING_UNIT_LY, - null as prescription, - null as prescription_ly, - ------------------------------------- - --Retail藏数逻辑标签------------------ - 1 PACK_FLAG, - 1 PROD_FLAG, - 0 DTP_FLAG, - null as cmps_flag, - ------------------------------------- - --EC数据标签-------------------------- - '' PLATFORM_TYPE, - '' STORE_NAME, - '' STORE_TYPE, - ------------------------------------- - --COUNTY数据标签---------------------- - '' REGION_TYPE, - ------------------------------------- - --AIA数据标签------------------------- - null inst_code, --可以移除,报告没有使用 - ------------------------------------- - --XIE HE 数据标签--------------------- - '' as dept_name, - '' as new_code, - '' as area, - '' as h_level, - '' as reimburse, - '' as reimburse_type, - '' as prescription_source, - ------------------------------------- - '' as CORP_COD --可以移除,报告没有使用 - ------------------------------------- - FROM - dm.dm_ims_tf_sales A - where - A.YYYYMM >= '202001' - AND A.MARKET = 'IMS ALL Market' - GROUP BY - A.YYYYMM, - A.PACK_COD, - A.AUDIT_COD - --- COMMAND ---------- - --- DBTITLE 1,COUNTY --- DELETE FROM --- dm.dm_tf_external_sales --- WHERE --- DATA_SOURCE = 'IQVIA-COUNTY(Quarterly)'; - -INSERT INTO dm.dm_tf_external_sales ( - YYYYMM, - PACK_COD, - AUDIT_COD, - DATA_SOURCE, - SALES_UNIT_CAL, - SALES_UNIT_CAL_LY, - SALES_VALUE_CAL, - SALES_VALUE_CAL_LY, - CONUTING_UNIT, - CONUTING_UNIT_LY, - prescription, - prescription_ly, - PACK_FLAG, - PROD_FLAG, - DTP_FLAG, - cmps_flag, - PLATFORM_TYPE, - STORE_NAME, - STORE_TYPE, - REGION_TYPE, - inst_code, - dept_name, - new_code, - area, - h_level, - reimburse, - reimburse_type, - prescription_source, - CORP_COD - ) - SELECT - A.YYYYMM, - A.PACK_COD, - A.AUDIT_COD, - 'IQVIA-COUNTY(Quarterly)' DATA_SOURCE, - ------------------------------------- - SUM(SALES_UNIT_CAL) SALES_UNIT_CAL, - SUM(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY, - SUM(SALES_VALUE_CAL) SALES_VALUE_CAL, - SUM(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY, - SUM(CONUTING_UNIT) CONUTING_UNIT, - SUM(CONUTING_UNIT_LY) as CONUTING_UNIT_LY, - null as prescription, - null as prescription_ly, - ------------------------------------- - --Retail藏数逻辑标签------------------ - 1 PACK_FLAG, - 1 PROD_FLAG, - 0 DTP_FLAG, - null as cmps_flag, - ------------------------------------- - --EC数据标签-------------------------- - '' PLATFORM_TYPE, - '' STORE_NAME, - '' STORE_TYPE, - ------------------------------------- - --COUNTY数据标签---------------------- - NVL(A.REGION_TYPE, '') REGION_TYPE, - ------------------------------------- - --AIA数据标签------------------------- - null inst_code, --可以移除,报告没有使用 - ------------------------------------- - --XIE HE 数据标签--------------------- - '' as dept_name, - '' as new_code, - '' as area, - '' as h_level, - '' as reimburse, - '' as reimburse_type, - '' as prescription_source, - ------------------------------------- - '' as CORP_COD --可以移除,报告没有使用 - ------------------------------------- - FROM - DM.dm_ext_county_tf_sales_region A - where - A.YYYYMM >= '202001' - AND A.MARKET = 'IMS ALL Market' - GROUP BY - A.YYYYMM, - A.PACK_COD, - A.AUDIT_COD, - A.REGION_TYPE - --- COMMAND ---------- - --- DBTITLE 1,THC --- DELETE FROM --- dm.dm_tf_external_sales --- WHERE --- DATA_SOURCE = 'THC(Quarterly)'; - -INSERT INTO dm.dm_tf_external_sales ( - YYYYMM, - PACK_COD, - AUDIT_COD, - DATA_SOURCE, - SALES_UNIT_CAL, - SALES_UNIT_CAL_LY, - SALES_VALUE_CAL, - SALES_VALUE_CAL_LY, - CONUTING_UNIT, - CONUTING_UNIT_LY, - prescription, - prescription_ly, - PACK_FLAG, - PROD_FLAG, - DTP_FLAG, - cmps_flag, - PLATFORM_TYPE, - STORE_NAME, - STORE_TYPE, - REGION_TYPE, - inst_code, - dept_name, - new_code, - area, - h_level, - reimburse, - reimburse_type, - prescription_source, - CORP_COD - ) - SELECT - A.YYYYMM, - A.PACK_COD, - A.AUDIT_COD, - 'THC(Quarterly)' DATA_SOURCE, - ------------------------------------- - SALES_UNIT_CAL, - SALES_UNIT_CAL_LY, - SALES_VALUE_CAL, - SALES_VALUE_CAL_LY, - COUNTING_UNIT, - COUNTING_UNIT_LY , - null as prescription, - null as prescription_ly, - ------------------------------------- - --Retail藏数逻辑标签------------------ - 1 PACK_FLAG, - 1 PROD_FLAG, - 0 DTP_FLAG, - null as cmps_flag, - ------------------------------------- - --EC数据标签-------------------------- - '' PLATFORM_TYPE, - '' STORE_NAME, - '' STORE_TYPE, - ------------------------------------- - --COUNTY数据标签---------------------- - '' REGION_TYPE, - ------------------------------------- - --AIA数据标签------------------------- - null inst_code, --可以移除,报告没有使用 - ------------------------------------- - --XIE HE 数据标签--------------------- - '' as dept_name, - '' as new_code, - '' as area, - '' as h_level, - '' as reimburse, - '' as reimburse_type, - '' as prescription_source, - ------------------------------------- - '' as CORP_COD --可以移除,报告没有使用 - ------------------------------------- - FROM - dm.dm_ext_thc_sales A - where - AUDIT_COD = 'CHT' - --- COMMAND ---------- - --- DBTITLE 1,CHC_RAW_DATA -------------------------------------------------------------------------------CHC------------------------------------------------------------------- -CREATE OR REPLACE TEMPORARY VIEW V_CHC_RAW_DATA AS -with exchc_rawdata_temp as ( --主表数据 - select - CASE - WHEN SUBSTRING(yq, 5,2) = 'Q1' THEN CONCAT(SUBSTRING(yq, 1, 4), '03') - WHEN SUBSTRING(yq, 5, 2) = 'Q2' THEN CONCAT(SUBSTRING(yq, 1, 4), '06') - WHEN SUBSTRING(yq, 5, 2) = 'Q3' THEN CONCAT(SUBSTRING(yq, 1, 4), '09') - WHEN SUBSTRING(yq, 5, 2) = 'Q4' THEN CONCAT(SUBSTRING(yq, 1, 4), '12') - ELSE yq - END AS yq, - city_c, - pack_cod, - prod_cod, - value, - totalunit, - countingunit - from dwd.dwd_gnd_chc_rawdata - where year >= '2021' -), -temp_corp as ( - select - PACK_COD, - CORP_COD, - CORP_DES - from dm.dm_ims_td_pack_property - group by PACK_COD,CORP_COD,CORP_DES -), -chc_pack_property_clean as ( -select distinct - iqvia_pack_code , - iqvia_prod_code , - pack_cod , - 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 , - builtinarchivedate , - source_file_path , - source_file_name , - etl_insert_dt , - countingunit -from dwd.dwd_gnd_ext_chc_pack_property -), -temp_corp_temp as ( -- chc本身唯独表 存在corp_cod以及 CORP_DES 与手工数据不一致的问题 - select - t1.pack_cod, - t1.iqvia_pack_code, - t1.countingunit, - coalesce(t2.CORP_COD, t1.CORP_COD) as CORP_COD , - coalesce(t2.CORP_DES,t1.CORP_DES ) as CORP_DES - from chc_pack_property_clean t1 - left join temp_corp t2 - on t1.iqvia_pack_code = t2.PACK_COD -), -dim_exchc_geo as ( - -- geo相关信息 在CHPA中没有 苏州、无锡、福州等数据,根据业务反馈,按照 city_c当作AUDIT_COD - select - t1.city_e, - concat( t1.city_c , '市') as city_c, - upper( nvl(t2.AUDIT_COD ,t1.city_e) ) as AUDIT_COD - from dwd.dwd_gnd_ext_chc_geo t1 - left join dm.dm_ims_td_geo t2 - on t1.city_e= t2.city --- --20260122 按照新的业务逻辑,直接用城市名称匹配获得省份code --- SELECT DISTINCT city_name,province_code,province_name FROM DM.dm_td_geography --- WHERE nvl(city_code,'')!='' and nvl(province_code,'')!='' and nvl(province_name,'')!='' -), -exchc_rawdata_iqvia as ( - select - t1.yq , - t1.city_c, - -- t1.pack_cod pack_cod_old, - t3.iqvia_pack_code as pack_cod, - t3.CORP_COD as CORP_COD, - C.AUDIT_COD as AUDIT_COD, - t1.prod_cod, - t1.value, - t1.totalunit, - -- t1.countingunit, - t1.totalunit * nvl(t3.countingunit,1 ) as countingunit - -- t2.countingunit as tt - from exchc_rawdata_temp t1 - left join temp_corp_temp t3 - on t1.pack_cod = t3.pack_cod - LEFT JOIN dim_exchc_geo C - ON t1.CITY_C = C.city_c -), -value_temp as( - select - a.yq, - a.AUDIT_COD, - a.pack_cod, - --t1.prod_cod, - a.CORP_COD, - sum(a.value) value, - sum(a.totalunit) totalunit, - sum(a.countingunit) countingunit, - sum(value_ly) value_ly, - sum(totalunit_ly) totalunit_ly, - sum(countingunit_ly) countingunit_ly - from ( - select - t1.yq, - t1.AUDIT_COD, - t1.pack_cod, - --t1.prod_cod, - t1.CORP_COD, - t1.value, - t1.totalunit, - t1.countingunit, - 0 value_ly, - 0 totalunit_ly, - 0 countingunit_ly - from exchc_rawdata_iqvia t1 - union all - select - cast(t1.yq+100 as int), - t1.AUDIT_COD, - t1.pack_cod, - --t1.prod_cod, - t1.CORP_COD, - 0 value, - 0 totalunit, - 0 countingunit, - t1.value value_ly, - t1.totalunit totalunit_ly, - t1.countingunit countingunit_ly - from exchc_rawdata_iqvia t1 - WHERE T1.YQ+100 <= (SELECT MAX(YQ) FROM exchc_rawdata_iqvia) - ) a - group by a.yq, - a.AUDIT_COD, - a.pack_cod, - --t1.prod_cod, - a.CORP_COD -) - -select - t1.yq as YYYYMM , - t1.AUDIT_COD, - t1.pack_cod, - t1.CORP_COD - --t1.prod_cod - ,sum(totalunit) AS SALES_UNIT_CAL -- totalunit - ,sum(totalunit_ly) AS SALES_UNIT_CAL_LY --totalunit_ly - ,sum(value) AS SALES_VALUE_CAL --value - ,sum(value_ly ) AS SALES_VALUE_CAL_LY - ,sum(countingunit) AS CONUTING_UNIT --countingunit - ,sum(countingunit_ly) AS CONUTING_UNIT_LY - ,'CHC(Quarterly)' as DATA_SOURCE - from value_temp t1 - group by 1,2,3,4 - --- COMMAND ---------- - --- DBTITLE 1,CHC --- DELETE FROM --- dm.dm_tf_external_sales --- WHERE --- DATA_SOURCE = 'CHC(Quarterly)'; - -INSERT INTO dm.dm_tf_external_sales ( - YYYYMM, - PACK_COD, - AUDIT_COD, - DATA_SOURCE, - SALES_UNIT_CAL, - SALES_UNIT_CAL_LY, - SALES_VALUE_CAL, - SALES_VALUE_CAL_LY, - CONUTING_UNIT, - CONUTING_UNIT_LY, - prescription, - prescription_ly, - PACK_FLAG, - PROD_FLAG, - DTP_FLAG, - cmps_flag, - PLATFORM_TYPE, - STORE_NAME, - STORE_TYPE, - REGION_TYPE, - inst_code, - dept_name, - new_code, - area, - h_level, - reimburse, - reimburse_type, - prescription_source, - CORP_COD - ) - SELECT - A.YYYYMM, - A.PACK_COD, - A.AUDIT_COD, - 'CHC(Quarterly)' DATA_SOURCE, - ------------------------------------- - SALES_UNIT_CAL, - SALES_UNIT_CAL_LY, - SALES_VALUE_CAL, - SALES_VALUE_CAL_LY, - CONUTING_UNIT, - CONUTING_UNIT_LY, - null as prescription, - null as prescription_ly, - ------------------------------------- - --Retail藏数逻辑标签------------------ - 1 PACK_FLAG, - 1 PROD_FLAG, - 0 DTP_FLAG, - null as cmps_flag, - ------------------------------------- - --EC数据标签-------------------------- - '' PLATFORM_TYPE, - '' STORE_NAME, - '' STORE_TYPE, - ------------------------------------- - --COUNTY数据标签---------------------- - '' REGION_TYPE, - ------------------------------------- - --AIA数据标签------------------------- - null inst_code, --可以移除,报告没有使用 - ------------------------------------- - --XIE HE 数据标签--------------------- - '' as dept_name, - '' as new_code, - '' as area, - '' as h_level, - '' as reimburse, - '' as reimburse_type, - '' as prescription_source, - ------------------------------------- - '' as CORP_COD --可以移除,报告没有使用 - ------------------------------------- - FROM - V_CHC_RAW_DATA A - WHERE - YYYYMM >= ( - SELECT - CAST((LEFT(MAX(YYYYMM), 4) - 3) * 100 + 1 AS INT) - FROM - V_CHC_RAW_DATA - ) - --- COMMAND ---------- - --- DBTITLE 1,Retail --- DELETE FROM --- dm.dm_tf_external_sales --- WHERE --- DATA_SOURCE = 'Retail(Quarterly)'; - -INSERT INTO dm.dm_tf_external_sales ( - YYYYMM, - PACK_COD, - AUDIT_COD, - DATA_SOURCE, - SALES_UNIT_CAL, - SALES_UNIT_CAL_LY, - SALES_VALUE_CAL, - SALES_VALUE_CAL_LY, - CONUTING_UNIT, - CONUTING_UNIT_LY, - prescription, - prescription_ly, - PACK_FLAG, - PROD_FLAG, - DTP_FLAG, - cmps_flag, - PLATFORM_TYPE, - STORE_NAME, - STORE_TYPE, - REGION_TYPE, - inst_code, - dept_name, - new_code, - area, - h_level, - reimburse, - reimburse_type, - prescription_source, - CORP_COD - ) -SELECT - A.YYYYMM, - A.iqvia_pack_code, - trim(A.AUDIT_COD) AS AUDIT_COD, - 'Retail(Quarterly)' DATA_SOURCE, - ------------------------------------- - SUM(sales_unit) SALES_UNIT_CAL, - SUM(sales_unit_ly) as SALES_UNIT_CAL_LY, - SUM(sales_value) SALES_VALUE_CAL, - SUM(sales_value_LY) as SALES_VALUE_CAL_LY, - SUM(counting_unit) CONUTING_UNIT, - SUM(counting_unit_LY) as CONUTING_UNIT_LY, - null as prescription, - null as prescription_ly, - ------------------------------------- - --Retail藏数逻辑标签------------------ - A.PACK_FLAG, - A.brand_flag PROD_FLAG, - 0 DTP_FLAG, - case - when - max(lower(C.CMPS_DES)) in ('atorvastatin', 'rosuvastatin', 'esomeprazole', 'omeprazole') - then - 1 - else 0 - end as cmps_flag, - ------------------------------------- - --EC数据标签-------------------------- - '' PLATFORM_TYPE, - '' STORE_NAME, - '' STORE_TYPE, - ------------------------------------- - --COUNTY数据标签---------------------- - '' REGION_TYPE, - ------------------------------------- - --AIA数据标签------------------------- - null inst_code, --可以移除,报告没有使用 - ------------------------------------- - --XIE HE 数据标签--------------------- - '' as dept_name, - '' as new_code, - '' as area, - '' as h_level, - '' as reimburse, - '' as reimburse_type, - '' as prescription_source, - ------------------------------------- - '' as CORP_COD --可以移除,报告没有使用 -------------------------------------- -FROM - dm.dm_zk_retail_sales A - LEFT JOIN ( - select DISTINCT - iqvia_pack_code, - CMPS_DES - from - dm.dm_zk_retail_pack_property - ) C - ON A.iqvia_pack_code = C.iqvia_pack_code -------------------------------------- -LEFT JOIN (SELECT OTHERS_MONTH FROM tmp.tmp_dm_td_ext_retail_niad_month) D -------------------------------------- -where - A.YYYYMM >= '202001' AND A.YYYYMM <= D.OTHERS_MONTH -GROUP BY - A.YYYYMM, - A.iqvia_pack_code, - A.AUDIT_COD, - A.PACK_FLAG, - A.brand_flag - --- COMMAND ---------- - --- DBTITLE 1,DTP --- DELETE FROM --- dm.dm_tf_external_sales --- WHERE --- DATA_SOURCE = 'DTP(Quarterly)'; - -INSERT INTO dm.dm_tf_external_sales ( - YYYYMM, - PACK_COD, - AUDIT_COD, - DATA_SOURCE, - SALES_UNIT_CAL, - SALES_UNIT_CAL_LY, - SALES_VALUE_CAL, - SALES_VALUE_CAL_LY, - CONUTING_UNIT, - CONUTING_UNIT_LY, - prescription, - prescription_ly, - PACK_FLAG, - PROD_FLAG, - DTP_FLAG, - cmps_flag, - PLATFORM_TYPE, - STORE_NAME, - STORE_TYPE, - REGION_TYPE, - inst_code, - dept_name, - new_code, - area, - h_level, - reimburse, - reimburse_type, - prescription_source, - CORP_COD - ) - SELECT - A.YYYYMM, - A.iqvia_pack_code, - trim(A.AUDIT_COD) AS AUDIT_COD, - 'DTP(Quarterly)' DATA_SOURCE, - ------------------------------------- - SUM(sales_unit) SALES_UNIT_CAL, - SUM(sales_unit_ly) as SALES_UNIT_CAL_LY, - SUM(sales_value) SALES_VALUE_CAL, - SUM(sales_value_LY) as SALES_VALUE_CAL_LY, - SUM(counting_unit) CONUTING_UNIT, - SUM(counting_unit_LY) as CONUTING_UNIT_LY, - null as prescription, - null as prescription_ly, - ------------------------------------- - --Retail藏数逻辑标签------------------ - 1 PACK_FLAG, - brand_flag AS PROD_FLAG, - 1 DTP_FLAG, - null as cmps_flag, - ------------------------------------- - --EC数据标签-------------------------- - '' PLATFORM_TYPE, - '' STORE_NAME, - '' STORE_TYPE, - ------------------------------------- - --COUNTY数据标签---------------------- - '' REGION_TYPE, - ------------------------------------- - --AIA数据标签------------------------- - null inst_code, --可以移除,报告没有使用 - ------------------------------------- - --XIE HE 数据标签--------------------- - '' as dept_name, - '' as new_code, - '' as area, - '' as h_level, - '' as reimburse, - '' as reimburse_type, - '' as prescription_source, - ------------------------------------- - '' as CORP_COD --可以移除,报告没有使用 - ------------------------------------- - FROM - dm.dm_zk_retail_dtp_sales A - where - A.YYYYMM >= '202001' - GROUP BY - A.YYYYMM, - A.iqvia_pack_code, - A.AUDIT_COD, - A.brand_flag, - A.pack_flag - --- COMMAND ---------- - --- DBTITLE 1,EC --- DELETE FROM --- dm.dm_tf_external_sales --- WHERE --- DATA_SOURCE = 'EC(Monthly)'; - -INSERT INTO dm.dm_tf_external_sales ( - YYYYMM, - PACK_COD, - AUDIT_COD, - DATA_SOURCE, - SALES_UNIT_CAL, - SALES_UNIT_CAL_LY, - SALES_VALUE_CAL, - SALES_VALUE_CAL_LY, - CONUTING_UNIT, - CONUTING_UNIT_LY, - prescription, - prescription_ly, - PACK_FLAG, - PROD_FLAG, - DTP_FLAG, - cmps_flag, - PLATFORM_TYPE, - STORE_NAME, - STORE_TYPE, - REGION_TYPE, - inst_code, - dept_name, - new_code, - area, - h_level, - reimburse, - reimburse_type, - prescription_source, - CORP_COD - ) - SELECT - A.YYYYMM, - A.iqvia_pack_code, - A.AUDIT_COD, - 'EC(Monthly)' DATA_SOURCE, - ------------------------------------- - SUM(sales_unit) SALES_UNIT_CAL, - SUM(sales_unit_ly) SALES_UNIT_CAL_LY, - SUM(sales_value) SALES_VALUE_CAL, - SUM(sales_value_LY) SALES_VALUE_CAL_LY, - SUM(counting_unit) CONUTING_UNIT, - SUM(counting_unit_LY) CONUTING_UNIT_LY, - null as prescription, - null as prescription_ly, - ------------------------------------- - --Retail藏数逻辑标签------------------ - 1 PACK_FLAG, - 1 PROD_FLAG, - 0 DTP_FLAG, - null as cmps_flag, - ------------------------------------- - --EC数据标签-------------------------- - A.PLATFORM, - A.STORE_NAME, - A.STORE_TYPE, - ------------------------------------- - --COUNTY数据标签---------------------- - '' REGION_TYPE, - ------------------------------------- - --AIA数据标签------------------------- - null inst_code, --可以移除,报告没有使用 - ------------------------------------- - --XIE HE 数据标签--------------------- - '' as dept_name, - '' as new_code, - '' as area, - '' as h_level, - '' as reimburse, - '' as reimburse_type, - '' as prescription_source, - ------------------------------------- - '' as CORP_COD --可以移除,报告没有使用 - ------------------------------------- - FROM - dm.dm_zk_ec_sales A - LEFT JOIN (SELECT OTHERS_MONTH FROM tmp.tmp_dm_td_ext_ec_niad_month) D - where - A.YYYYMM >= '202001' and A.YYYYMM<=D.others_month - GROUP BY - A.YYYYMM, - A.iqvia_pack_code, - A.AUDIT_COD, - A.PLATFORM, - A.STORE_NAME, - A.STORE_TYPE, - A.PACK_FLAG, - A.PROD_FLAG - --- COMMAND ---------- - --- DBTITLE 1,AIA --- DELETE FROM --- dm.dm_tf_external_sales --- WHERE --- DATA_SOURCE = 'AIA(Monthly)'; - -INSERT INTO dm.dm_tf_external_sales ( - YYYYMM, - PACK_COD, - AUDIT_COD, - DATA_SOURCE, - SALES_UNIT_CAL, - SALES_UNIT_CAL_LY, - SALES_VALUE_CAL, - SALES_VALUE_CAL_LY, - CONUTING_UNIT, - CONUTING_UNIT_LY, - prescription, - prescription_ly, - PACK_FLAG, - PROD_FLAG, - DTP_FLAG, - cmps_flag, - PLATFORM_TYPE, - STORE_NAME, - STORE_TYPE, - REGION_TYPE, - inst_code, - dept_name, - new_code, - area, - h_level, - reimburse, - reimburse_type, - prescription_source, - CORP_COD - ) - SELECT - A.ym, - A.PACK_COD, - A.AUDIT_COD, - 'AIA(Monthly)' DATA_SOURCE, - ------------------------------------- - sum(volume) as SALES_UNIT_CAL, - sum(volume_ly) as SALES_UNIT_CAL_LY, - sum(value) as SALES_VALUE_CAL, - sum(value_ly) as SALES_VALUE_CAL_LY, - sum(counting_unit) as CONUTING_UNIT, - sum(counting_unit_ly) as CONUTING_UNIT_LY, - null as prescription, - null as prescription_ly, - ------------------------------------- - --Retail藏数逻辑标签------------------ - 1 PACK_FLAG, - 1 PROD_FLAG, - 0 DTP_FLAG, - null as cmps_flag, - ------------------------------------- - --EC数据标签-------------------------- - '' PLATFORM_TYPE, - '' STORE_NAME, - '' STORE_TYPE, - ------------------------------------- - --COUNTY数据标签---------------------- - '' REGION_TYPE, - ------------------------------------- - --AIA数据标签------------------------- - inst_code AS inst_code, --可以移除,报告没有使用 - ------------------------------------- - --XIE HE 数据标签--------------------- - '' as dept_name, - '' as new_code, - '' as area, - '' as h_level, - '' as reimburse, - '' as reimburse_type, - '' as prescription_source, - ------------------------------------- - '' as CORP_COD --可以移除,报告没有使用 - ------------------------------------- - from - dm.dm_ext_aia_sales A - --取202401年之后的数 - where - ym >= '202401' - group by - ym, - pack_cod, - corp_cod, - AUDIT_COD, - inst_code - --- COMMAND ---------- - --- DBTITLE 1,XIEHE --- DELETE FROM --- dm.dm_tf_external_sales --- WHERE --- DATA_SOURCE = 'XH Data(Quarterly)'; - -INSERT INTO dm.dm_tf_external_sales ( - YYYYMM, - PACK_COD, - AUDIT_COD, - DATA_SOURCE, - SALES_UNIT_CAL, - SALES_UNIT_CAL_LY, - SALES_VALUE_CAL, - SALES_VALUE_CAL_LY, - CONUTING_UNIT, - CONUTING_UNIT_LY, - prescription, - prescription_ly, - PACK_FLAG, - PROD_FLAG, - DTP_FLAG, - cmps_flag, - PLATFORM_TYPE, - STORE_NAME, - STORE_TYPE, - REGION_TYPE, - inst_code, - dept_name, - new_code, - area, - h_level, - reimburse, - reimburse_type, - prescription_source, - CORP_COD - ) - SELECT - A.YYYYMM, - A.iqvia_pack_code, - A.AUDIT_COD, - 'XH Data(Quarterly)' DATA_SOURCE, - ------------------------------------- - 0 SALES_UNIT_CAL, - 0 SALES_UNIT_CAL_LY, - SUM(sales_value) SALES_VALUE_CAL, - SUM(sales_value_LY) SALES_VALUE_CAL_LY, - SUM(A.counting_unit) CONUTING_UNIT, - SUM(A.counting_unit_ly) CONUTING_UNIT_LY, - null as prescription, - null as prescription_ly, - ------------------------------------- - --Retail藏数逻辑标签------------------ - 1 PACK_FLAG, - 1 PROD_FLAG, - 0 DTP_FLAG, - null as cmps_flag, - ------------------------------------- - --EC数据标签-------------------------- - '' PLATFORM_TYPE, - '' STORE_NAME, - '' STORE_TYPE, - ------------------------------------- - --COUNTY数据标签---------------------- - '' REGION_TYPE, - ------------------------------------- - --AIA数据标签------------------------- - null inst_code, --可以移除,报告没有使用 - ------------------------------------- - --XIE HE 数据标签--------------------- - NVL(A.dept_name, '') dept_name, - NVL(A.new_code, '') new_code, - NVL(A.province, '') area, - NVL(A.h_level, '') h_level, - NVL(A.reimburse, '') reimburse, - NVL(A.reimburse_type, '') reimburse_type, - NVL(A.prescription_source, '') prescription_source, - ------------------------------------- - '' as CORP_COD --可以移除,报告没有使用 - ------------------------------------- - FROM - dm.dm_ext_xiehe_sales A - WHERE - A.YYYYMM >= '202001' - GROUP BY - A.YYYYMM, - A.iqvia_pack_code, - A.AUDIT_COD, - A.dept_name, - A.new_code, - A.province, - A.h_level, - A.reimburse, - A.reimburse_type, - A.prescription_source - --- COMMAND ---------- - --- MAGIC %md --- MAGIC ## 特殊逻辑 - --- COMMAND ---------- - --- DBTITLE 1,更新pack code -update dm.dm_tf_external_sales set PACK_COD = concat('PACK_COD_',DATA_SOURCE) -where PACK_COD is null or PACK_COD = ''; -update dm.dm_tf_external_sales set AUDIT_COD = 'ROC' -where AUDIT_COD is null or AUDIT_COD = ''; - - --- COMMAND ---------- - --- MAGIC %md --- MAGIC ## NIAD单独页面数据 - --- COMMAND ---------- - -update dm.dm_zk_retail_sales set DATA_SOURCE = 'Retail(Quarterly)' - --- COMMAND ---------- - --- DBTITLE 1,NIAD --- ============================================================================= --- 脚本名称: dm.dm_tf_external_sales_niad 数据装载脚本 --- 功能描述: 将零售和电商的销售数据(仅限NIAD市场)插入目标表 --- 数据来源: dm.dm_zk_retail_sales, dm.dm_zk_ec_sales, dm.dm_td_external_market_pack_mapping_temp --- 目标表: dm.dm_tf_external_sales_niad --- 更新方式: INSERT OVERWRITE(全量覆盖) --- 创建时间: 2026-03-09 --- ============================================================================= - -INSERT OVERWRITE TABLE dm.dm_tf_external_sales_niad -( - YYYYMM, -- 年月分区键 - PACK_COD, -- 药品包装代码 - AUDIT_COD, -- 审计代码 - SALES_UNIT_CAL, -- 本期销量(单位) - SALES_UNIT_CAL_LY, -- 去年同期销量(单位) - SALES_VALUE_CAL, -- 本期销售额 - SALES_VALUE_CAL_LY, -- 去年同期销售额 - CONUTING_UNIT, -- 计数单位(本期) - CONUTING_UNIT_LY, -- 计数单位(去年同期) - DATA_SOURCE, -- 数据来源(零售/电商) - DATE_KEY, -- 日期键(转换为日期类型) - PACK_SOURCE, -- 包装来源唯一标识 - AUDIT_SOURCE -- 审计来源唯一标识 -) - -WITH mkt_pack AS ( - SELECT DISTINCT - PACK_COD, - DATA_SOURCE - FROM dm.dm_td_external_market_pack_mapping_temp - WHERE DATA_SOURCE IN ('Retail(Quarterly)', 'EC(Monthly)') -- 仅限零售和电商 - AND MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' -- NIAD市场过滤 -), - -sales AS ( - -- 零售数据(季度) - SELECT - YYYYMM, - iqvia_pack_code AS PACK_COD, - AUDIT_COD, - sales_unit AS SALES_UNIT_CAL, - sales_unit_ly AS SALES_UNIT_CAL_LY, - sales_value AS SALES_VALUE_CAL, - sales_value_ly AS SALES_VALUE_CAL_LY, - counting_unit AS CONUTING_UNIT, - counting_unit_ly AS CONUTING_UNIT_LY, - DATA_SOURCE, - -- 将日期转换放在CTE中统一处理,避免重复解析 - TO_DATE(YYYYMM, 'yyyyMM') AS DATE_KEY, - CONCAT(iqvia_pack_code, DATA_SOURCE) AS PACK_SOURCE, - CONCAT(AUDIT_COD, DATA_SOURCE) AS AUDIT_SOURCE - FROM dm.dm_zk_retail_sales - WHERE YYYYMM >= '202001' -- 仅保留2020年及以后数据 - - UNION ALL - - -- 电商数据(月度) - SELECT - YYYYMM, - iqvia_pack_code AS PACK_COD, - AUDIT_COD, - sales_unit AS SALES_UNIT_CAL, - sales_unit_ly AS SALES_UNIT_CAL_LY, - sales_value AS SALES_VALUE_CAL, - sales_value_ly AS SALES_VALUE_CAL_LY, - counting_unit AS CONUTING_UNIT, - counting_unit_ly AS CONUTING_UNIT_LY, - DATA_SOURCE, - TO_DATE(YYYYMM, 'yyyyMM') AS DATE_KEY, - CONCAT(iqvia_pack_code, DATA_SOURCE) AS PACK_SOURCE, - CONCAT(AUDIT_COD, DATA_SOURCE) AS AUDIT_SOURCE - FROM dm.dm_zk_ec_sales - WHERE YYYYMM >= '202001' -- 仅保留2020年及以后数据 -) -SELECT - a.YYYYMM, - a.PACK_COD, - a.AUDIT_COD, - a.SALES_UNIT_CAL, - a.SALES_UNIT_CAL_LY, - a.SALES_VALUE_CAL, - a.SALES_VALUE_CAL_LY, - a.CONUTING_UNIT, - a.CONUTING_UNIT_LY, - a.DATA_SOURCE, - a.DATE_KEY, - a.PACK_SOURCE, - a.AUDIT_SOURCE - FROM sales a - INNER JOIN mkt_pack b - ON a.PACK_COD = b.PACK_COD - AND a.DATA_SOURCE = b.DATA_SOURCE; \ No newline at end of file diff --git a/z 10 dm_td_external_market_pack_mapping_作废.sql b/z 10 dm_td_external_market_pack_mapping_作废.sql deleted file mode 100644 index 0e90786..0000000 --- a/z 10 dm_td_external_market_pack_mapping_作废.sql +++ /dev/null @@ -1,1439 +0,0 @@ --- Databricks notebook source ---------------------------------------------------------------------------- ---修改时间:20241126 ---修改人:Fanxujia ---修改内容: ---THC渠道分为14省的数据源以及全国的数据源两种,分在了2个事实表里,下面在维表在引用事实表时,需要将两个事实表union在一起 ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ---修改时间:20241129 ---修改人:Fanxujia ---修改内容: ---补充Retail直取中DTP的手工文件里的pack_cod ----------------------------------------------------------------------------- -create or replace temporary view external_sales_union as -select distinct -PACK_COD, -DATA_SOURCE -from dm.dm_tf_external_sales -union all -select distinct -PACK_COD, -DATA_SOURCE -from dm.dm_tf_external_sales_thc -union all -select distinct -pack_code, -'Retail(Quarterly)' as DATA_SOURCE -from dm.dm_zk_retail_sales_kpi -where NVL(pack_code,'Others') <> 'Others' -and pack_code <> '' - --- COMMAND ---------- - --------------------------------------------------------------------------------------------------------------------------------------------------- ---修改时间:20240812 ---修改人:FanXujia ---修改内容: ---在从DM_TD_EXTERNAL_KEYCOMPATITOR表里获取市场描述(MARKET_DESC字段)时,需要增加一个DATA_SOURCE的条件。 ---修改原因: ---对于同一个市场,每个渠道的市场描述并不一样。 --------------------------------------------------------------------------------------------------------------------------------------------------- - --- COMMAND ---------- - -/* -修改日期:20250311 -修改人:chenwu -修改内容:修改COUNTINGUNIT_MARKET_RATIO 取unit的值 -*/ -/* -修改日期:20250513 -修改人:chenwu -修改内容:修改RE Market 为 RE Market (Global) -*/ -INSERT OVERWRITE dm.dm_td_external_market_pack_mapping_temp( - PACK_COD,PACK_DES,PROD_DES,PROD_DES_C,MARKET,TA,KEY_COMPETITOR,AZ_RELATED,CLASS, - VALUE_MARKET_RATIO,UNIT_MARKET_RATIO,COUNTINGUNIT_MARKET_RATIO,PDOT_MARKET_RATIO,MARKET_DESC,TA_RN,DATA_SOURCE,dept_name,key_dept_name - ,new_code - ,area -) -WITH MKT_PACK_RATIO AS ( - -------------------------------------根据沟通,市场+产品对应的ratio是唯一的------------------------------------------ -SELECT A.MARKET,A.PACK_COD,MAX(A.Value_Market_Ratio) Value_Market_Ratio,MAX(A.Unit_Market_Ratio) Unit_Market_Ratio,MAX(A.PDOT_Market_Ratio) CountingUnit_Market_Ratio,MAX(A.CountingUnit_Market_Ratio) PDOT_Market_Ratio --因为之前的报告在使用源表的数据,因此命名是反的。 -FROM DWS.dws_ims_td_market_ratio A -WHERE A.YM = (SELECT MAX(YM) FROM DWS.dws_ims_td_market_ratio) -GROUP BY A.MARKET,A.PACK_COD -) ---------------------------------------------------------------------CHPA------------------------------------------------------------------ -SELECT B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET,B.TA,B.KEY_COMPETITOR, -CASE - WHEN B.MARKET = 'DM Market' THEN 'DM' - WHEN B.MARKET = 'CV Market' THEN 'CV' - WHEN B.MARKET = 'RD Market' THEN 'RD' - WHEN B.MARKET = 'RE Market' THEN 'RE' --WHEN B.MARKET = 'RE Market' THEN 'RE' - WHEN B.MARKET IN ('GI Market','Retail_GI Market') THEN 'GI' - WHEN B.MARKET = 'ONC Market' THEN 'ONCO' - WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD' - WHEN B.MARKET = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE - END AZ_RELATED - ,B.CLASS,NVL(B.VALUE_MARKET_RATIO,1) VALUE_MARKET_RATIO,NVL(B.UNIT_MARKET_RATIO,1) UNIT_MARKET_RATIO,NVL(B.COUNTINGUNIT_MARKET_RATIO,1) COUNTINGUNIT_MARKET_RATIO,NVL(B.PDOT_MARKET_RATIO,1) PDOT_MARKET_RATIO,B.MARKET_DESC, -CASE - WHEN B.MARKET = 'DM Market' THEN 1 - WHEN B.MARKET = 'CV Market' THEN 2 - WHEN B.MARKET = 'RD Market' THEN 3 - WHEN B.MARKET = 'RE Market' THEN 4 - WHEN B.MARKET = 'GI Market' THEN 5 - WHEN B.MARKET = 'ONC Market' THEN 6 - WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7 - WHEN B.MARKET = 'RARE Market' THEN 8 --20250701 chenwu 新增RARE - ELSE 99 END TA_RN -,'IQVIA-CHPA(Monthly)' DATA_SOURCE -,'' as dept_name -,'' as key_dept_name -,'' as new_code -,'' as area -FROM ( -SELECT B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET,MAX(C.TA) TA,MAX(E.KEY_COMPETITOR) KEY_COMPETITOR,MAX(E.IS_AZ) IS_AZ,MAX(E.AZ_MAIN) AZ_MAIN,MAX(E.AZ_RELATED) AZ_RELATED,MAX(E.CLASS) CLASS,MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO,MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO,MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO,MAX(D.PDOT_MARKET_RATIO) PDOT_MARKET_RATIO,max(g.MARKET_DESC) MARKET_DESC -FROM (SELECT PACK_COD,COUNT(*) RUM FROM DM.DM_TF_EXTERNAL_SALES --- WHERE YYYYMM >= (SELECT CAST((LEFT(MAX(YYYYMM),4)-3)*100+1 AS INT) FROM DM.DM_TF_EXTERNAL_SALES) --- AND DATA_SOURCE = 'IQVIA-CHPA(Monthly)' -where DATA_SOURCE = 'IQVIA-CHPA(Monthly)' -GROUP BY PACK_COD) A -LEFT JOIN DM.DM_IMS_TD_PACK_PROPERTY B -ON B.PACK_COD = A.PACK_COD --AND B.MARKET <> 'IMS ALL Market' -LEFT JOIN DM.DM_IMS_TD_MARKET_PROPERTY C -ON B.MARKET = C.MARKET -LEFT JOIN DM.DM_IMS_TD_PACK_PROPERTY E ON B.MARKET_PACK_KEY = E.MARKET_PACK_KEY -LEFT JOIN MKT_PACK_RATIO D ON B.MARKET = D.MARKET AND B.PACK_COD = D.PACK_COD -LEFT JOIN (SELECT MARKET,DATA_SOURCE,MAX(MARKETDESC) MARKET_DESC FROM DM.DM_TD_EXTERNAL_KEYCOMPATITOR GROUP BY MARKET,DATA_SOURCE) G ON B.MARKET = G.MARKET AND UPPER(G.DATA_SOURCE) = 'IQVIA-CHPA(MONTHLY)' -GROUP BY B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET) B -WHERE B.PACK_COD IS NOT NULL AND B.MARKET IS NOT NULL -UNION ALL ---------------------------------------------------------------------Retail------------------------------------------------------------------ -SELECT - B.PACK_COD, - B.PACK_DES, - B.PROD_DES, - B.PROD_DES_C, - CASE - when B.MARKET = 'Non AZ Retail Related Market' - and b.TA = 'CV' THEN 'CV Market' - else b.MARKET - end MARKET, - CASE - WHEN B.MARKET = 'RE Market' THEN 'RE' - when B.TA = 'RE' THEN NULL--只有 'RE Market (Global)' 的ta是 RE - WHEN B.MARKET = 'Soliris Related Market' THEN 'RARE'--20250701 chenwu 新增RARE - ELSE b.TA - END TA, - B.KEY_COMPETITOR, - CASE - WHEN B.MARKET = 'RE Market' THEN 'RE' - when B.TA = 'RE' THEN NULL--只有 'RE Market (Global)' 的ta是 RE - WHEN B.MARKET = 'CVRM_OTHER Market' THEN 'CVRM' - WHEN B.MARKET = 'Soliris Related Market' THEN 'RARE'--20250701 chenwu 新增RARE - ELSE b.TA - END AZ_RELATED, - B.CLASS, - nvl(B.VALUE_MARKET_RATIO, 1), - nvl(B.UNIT_MARKET_RATIO, 1), - nvl(B.UNIT_MARKET_RATIO, 1) COUNTINGUNIT_MARKET_RATIO, --应该取unit的值作为count --,1 COUNTINGUNIT_MARKET_RATIO - nvl(B.PDOT_MARKET_RATIO, 1), - B.MARKET_DESC, - -- CASE WHEN b.TA='DM' THEN 1 WHEN b.TA ='CV' OR B.MARKET='CV Market' THEN 2 WHEN b.TA ='RD' THEN 3 WHEN b.MARKET ='RE Market' OR b.TA ='RE' or b.MARKET = 'Inhaled Extended Market by Brand' THEN 4 WHEN b.TA ='GI' THEN 5 WHEN b.TA ='ONCO' THEN 6 - CASE - WHEN b.TA = 'DM' THEN 1 - WHEN b.TA = 'CV' OR B.MARKET = 'CV Market' THEN 2 - WHEN b.TA = 'RD' THEN 3 - WHEN b.MARKET = 'RE Market' THEN 4 - WHEN b.TA = 'GI' THEN 5 - WHEN b.TA = 'ONCO' THEN 6 - WHEN B.MARKET = 'CVRM_OTHER Market' THEN 0 - WHEN B.MARKET = 'Soliris Related Market' THEN 8--20250701 chenwu 新增RARE - ELSE 99 - END TA_RN, - 'Retail(Quarterly)' DATA_SOURCE, - '' as dept_name, - '' as key_dept_name, - '' as new_code, - '' as area -FROM - ( - SELECT - a.MARKET_PACK_KEY, - B.iqvia_pack_code PACK_COD, - a.PACK_DES, - a.PROD_DES, - a.PROD_DES_C, - a.MARKET, - MAX(a.TA) TA, - MAX(a.KEY_COMPETITOR) KEY_COMPETITOR, - MAX(a.CLASS) CLASS, - MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO, - MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO, - MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO, - MAX(D.COUNTINGUNIT_MARKET_RATIO) PDOT_MARKET_RATIO, - max(g.MARKET_DESC) MARKET_DESC - FROM - DM.dm_zk_retail_pack_property a - LEFT JOIN DM.dm_zk_retail_market_property B ON A.iqvia_pack_code = B.iqvia_pack_code - and a.market = b.market - LEFT JOIN dm.dm_zk_retail_market_ratio D ON B.MARKET = D.MARKET - AND B.iqvia_pack_code = D.pack_cod - LEFT JOIN ( - SELECT - MARKET, - DATA_SOURCE, - MAX(MARKETDESC) MARKET_DESC - FROM - DM.DM_TD_EXTERNAL_KEYCOMPATITOR - GROUP BY - MARKET, - DATA_SOURCE - ) G ON B.MARKET = G.MARKET - AND UPPER(G.DATA_SOURCE) = 'RETAIL(QUARTERLY)' - WHERE - A.iqvia_pack_code in ( - select - distinct iqvia_pack_code - from - dm.dm_zk_retail_sales - ) - GROUP BY - a.MARKET_PACK_KEY, - B.iqvia_pack_code, - a.PACK_DES, - a.PROD_DES, - a.PROD_DES_C, - a.MARKET - ) b ---------------------------------------------------------DTP-------------------------------------------------------- -UNION ALL -SELECT B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C -,MARKET -,CASE - WHEN B.TA IS NULL AND MARKET = 'ONC Market' - THEN 'ONCO' -ELSE B.TA END AS TA -,B.KEY_COMPETITOR -,CASE - WHEN B.TA IS NULL AND MARKET = 'ONC Market' - THEN 'ONCO' - ELSE B.TA END AS AZ_RELATED -,B.CLASS -,nvl(B.VALUE_MARKET_RATIO,1) -,nvl(B.UNIT_MARKET_RATIO,1) -,nvl(B.UNIT_MARKET_RATIO,1) COUNTINGUNIT_MARKET_RATIO --应该取unit的值作为count--,1 COUNTINGUNIT_MARKET_RATIO -,nvl(B.PDOT_MARKET_RATIO,1) -,B.MARKET_DESC, -CASE - WHEN B.TA IS NULL AND MARKET = 'ONC Market' THEN 6 - WHEN B.TA IS NULL THEN 99 - WHEN B.TA ='CVRM' THEN 0 - WHEN B.TA ='DM' THEN 1 - WHEN B.TA ='CV' THEN 2 - WHEN B.TA ='RD' THEN 3 - WHEN B.TA ='RE' THEN 4 - WHEN B.TA ='GI' THEN 5 - WHEN B.TA ='ONCO' THEN 6 - WHEN B.TA ='NIAD' THEN 7 - WHEN B.TA ='RARE' THEN 8 - WHEN B.TA ='NP' THEN 9 - ELSE 99 END TA_RN -,'DTP(Quarterly)' DATA_SOURCE -,'' as dept_name -,'' as key_dept_name -,'' as new_code -,'' as area -FROM ( -SELECT a.MARKET_PACK_KEY,a.iqvia_pack_code PACK_COD,a.PACK_DES,a.PROD_DES,a.PROD_DES_C -,a.MARKET,max(t3.TA) AS TA,max(t3.TA_ORDER) AS TA_ORDER -,MAX(a.KEY_COMPETITOR) KEY_COMPETITOR,MAX(a.CLASS) CLASS,MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO,MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO,MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO,MAX(D.COUNTINGUNIT_MARKET_RATIO) PDOT_MARKET_RATIO,max(g.MARKET_DESC) MARKET_DESC -FROM DM.dm_zk_retail_dtp_pack_property a -LEFT JOIN dm.dm_zk_retail_market_ratio D ON a.MARKET = D.MARKET AND a.iqvia_pack_code = D.pack_cod -LEFT JOIN (SELECT MARKET,DATA_SOURCE,MAX(MARKETDESC) MARKET_DESC FROM DM.DM_TD_EXTERNAL_KEYCOMPATITOR GROUP BY MARKET,DATA_SOURCE) G ON a.MARKET = G.MARKET AND UPPER(G.DATA_SOURCE) = 'RETAIL(QUARTERLY)' -LEFT JOIN ( - select - MARKET,IF(TA = 'ONC' ,'ONCO',TA) AS TA,TA_ORDER - from dwd.dwd_td_ims_market_ta_order - group by 1,2,3) t3 ON a.MARKET = t3.market -WHERE A.iqvia_pack_code in (select distinct iqvia_pack_code from dm.dm_zk_retail_dtp_sales) -GROUP BY a.MARKET_PACK_KEY,A.iqvia_pack_code,a.PACK_DES,a.PROD_DES,a.PROD_DES_C,a.MARKET -) b - ---------------------------------------------------------RETAIL all market-------------------------------------------------------- -union all -SELECT B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C -,'Retail All Market' MARKET ,TA,b.KEY_COMPETITOR,null AZ_RELATED,null CLASS, -1 VALUE_MARKET_RATIO,1 UNIT_MARKET_RATIO,1 COUNTINGUNIT_MARKET_RATIO,1 PDOT_MARKET_RATIO,null MARKET_DESC,null TA_RN -,'Retail(Quarterly)' DATA_SOURCE -,'' as dept_name -,'' as key_dept_name -,'' as new_code -,'' as area -FROM ( -SELECT a.iqvia_pack_code PACK_COD,a.PACK_DES,a.PROD_DES,a.PROD_DES_C,null TA,MAX(a.KEY_COMPETITOR) KEY_COMPETITOR -FROM DM.dm_zk_retail_pack_property a -WHERE A.iqvia_pack_code in (select distinct iqvia_pack_code from dm.dm_zk_retail_sales) -GROUP BY A.iqvia_pack_code,a.PACK_DES,a.PROD_DES,a.PROD_DES_C -union -select a.pack_code,nvl(b.pack_des,c.pack_des),nvl(b.PROD_DES,c.PROD_DES),nvl(b.PROD_DES_C,c.PROD_DES_C),null ta,MAX(b.KEY_COMPETITOR) KEY_COMPETITOR -from dm.dm_zk_retail_sales_kpi a -left join dm.dm_zk_retail_pack_property b on a.pack_code =b.iqvia_pack_code -left join (select distinct iqvia_pack_code,pack_des,PROD_DES,PROD_DES_C from dm.dm_zk_retail_pack_property ) c on a.pack_code =c.iqvia_pack_code -where a.pack_code <>'' ---排除Others以及null -and NVL(a.pack_code,'Others') <> 'Others' -GROUP BY a.pack_code,nvl(b.pack_des,c.pack_des),nvl(b.PROD_DES,c.PROD_DES),nvl(b.PROD_DES_C,c.PROD_DES_C) -) b - ---------------------------------------------------------DTP all market-------------------------------------------------------- -union all -SELECT B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C -,'DTP ALL Market' MARKET ,TA,b.KEY_COMPETITOR,null AZ_RELATED,null CLASS, -1 VALUE_MARKET_RATIO,1 UNIT_MARKET_RATIO,1 COUNTINGUNIT_MARKET_RATIO,1 PDOT_MARKET_RATIO,null MARKET_DESC,null TA_RN -,'DTP(Quarterly)' DATA_SOURCE -,'' as dept_name -,'' as key_dept_name -,'' as new_code -,'' as area -FROM ( - SELECT a.iqvia_pack_code PACK_COD,a.PACK_DES,a.PROD_DES,a.PROD_DES_C,null TA,MAX(a.KEY_COMPETITOR) KEY_COMPETITOR -FROM DM.dm_zk_retail_dtp_pack_property a -WHERE A.iqvia_pack_code in (select distinct iqvia_pack_code from dm.dm_zk_retail_dtp_sales) -GROUP BY A.iqvia_pack_code,a.PACK_DES,a.PROD_DES,a.PROD_DES_C -) B - --- COMMAND ---------- - ---------------------------------------------------------------------------------- ---修改时间:20240918 ---修改人:FanXuJia ---修改内容: ---计算market_ratio\pdot_ratio时,之前使用的是现成的dws表,但是这个dws表不是外部数据报告的,更新频率也不是每天更新。会存在一定的更新时间差。 ---因此改成使用dwd层表 ------------------------------------------------------------------------------------ -INSERT into dm.dm_td_external_market_pack_mapping_temp( - PACK_COD,PACK_DES,PROD_DES,PROD_DES_C,MARKET,TA,KEY_COMPETITOR,AZ_RELATED,CLASS, - VALUE_MARKET_RATIO,UNIT_MARKET_RATIO,COUNTINGUNIT_MARKET_RATIO,PDOT_MARKET_RATIO,MARKET_DESC,TA_RN,DATA_SOURCE,dept_name,key_dept_name - ,new_code - ,area -) -WITH county_brand_ratio as ( - SELECT distinct market, - CASE WHEN length(cmps_cod) < 6 THEN RIGHT(CONCAT('000000',cmps_cod),6) ELSE cmps_cod END AS cmps_cod, - cmps_des, - CASE WHEN length(pack_cod) < 12 and pack_cod REGEXP '^[0-9]' THEN RIGHT(CONCAT('000000000000',pack_cod),12) ELSE pack_cod END AS pack_cod, - pack_des, - level, - type, - ratio - from dwd.dwd_gnd_ims_tblBrandRatio -) -,MKT_PACK_RATIO_tmp as ( -select t1.market, -t1.PACK_COD, ---优先取pack粒度的ratio -case when t2.ratio is not null then t1.Market_Ratio * t2.ratio - else t1.Market_Ratio * NVL(t3.ratio,1) -end as Value_Market_Ratio, -case when t4.ratio is not null then t1.Market_Ratio * t4.ratio - else t1.Market_Ratio * NVL(t5.ratio,1) -end as Unit_Market_Ratio, -t1.Market_Ratio as CountingUnit_Market_Ratio, -case when t6.ratio is not null then t1.Market_Ratio * t6.ratio - else t1.Market_Ratio * NVL(t7.ratio,1) -end as PDOT_Market_Ratio -from DWS.dws_ims_td_market t1 ---value by Pack -left join (select * from county_brand_ratio where UPPER(TYPE) = 'VALUE' and UPPER(Level) = 'PACK') t2 -on t1.MARKET = t2.market -and t1.PACK_COD = t2.pack_cod ---value by MOLECULE -LEFT JOIN (select * from county_brand_ratio where UPPER(TYPE) = 'VALUE' and UPPER(Level) = 'MOLECULE') t3 -on t1.MARKET = t3.market -and t1.CMPS_COD = t3.cmps_cod ---unit by Pack -left join (select * from county_brand_ratio where UPPER(TYPE) = 'UNIT' and UPPER(Level) = 'PACK') t4 -on t1.MARKET = t4.market -and t1.PACK_COD = t4.pack_cod ---unit by MOLECULE -LEFT JOIN (select * from county_brand_ratio where UPPER(TYPE) = 'UNIT' and UPPER(Level) = 'MOLECULE') t5 -on t1.MARKET = t5.market -and t1.CMPS_COD = t5.cmps_cod ---PDOT by Pack -left join (select * from county_brand_ratio where UPPER(TYPE) = 'COUNTINGUNIT' and UPPER(Level) = 'PACK') t6 -on t1.MARKET = t6.market -and t1.PACK_COD = t6.pack_cod ---PDOT by MOLECULE -LEFT JOIN (select * from county_brand_ratio where UPPER(TYPE) = 'COUNTINGUNIT' and UPPER(Level) = 'MOLECULE') t7 -on t1.MARKET = t7.market -and t1.CMPS_COD = t7.cmps_cod -) -,MKT_PACK_RATIO as ( -select market, -PACK_COD, -max(Value_Market_Ratio) as Value_Market_Ratio, -max(Unit_Market_Ratio) as Unit_Market_Ratio, -max(CountingUnit_Market_Ratio) as CountingUnit_Market_Ratio, -max(PDOT_Market_Ratio) as PDOT_Market_Ratio -FROM MKT_PACK_RATIO_tmp -GROUP BY market,PACK_COD -) - - -SELECT B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET,B.TA,B.KEY_COMPETITOR, -CASE -WHEN B.MARKET = 'DM Market' THEN 'DM' -WHEN B.MARKET = 'CV Market' THEN 'CV' -WHEN B.MARKET = 'RD Market' THEN 'RD' -WHEN B.MARKET = 'RE Market' THEN 'RE' -WHEN B.MARKET = 'GI Market' THEN 'GI' -WHEN B.MARKET = 'ONC Market' THEN 'ONCO' -WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD' -WHEN B.MARKET = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE -END AZ_RELATED -,B.CLASS,nvl(CAST(B.VALUE_MARKET_RATIO AS decimal(20,16)),1),nvl(CAST(B.UNIT_MARKET_RATIO AS decimal(20,16)),1),nvl(CAST(B.COUNTINGUNIT_MARKET_RATIO AS decimal(20,16)),1),nvl(CAST(B.PDOT_MARKET_RATIO AS decimal(20,16)),1),B.MARKET_DESC, -CASE -WHEN B.MARKET = 'DM Market' THEN 1 -WHEN B.MARKET = 'CV Market' THEN 2 -WHEN B.MARKET = 'RD Market' THEN 3 -WHEN B.MARKET = 'RE Market' THEN 4 -WHEN B.MARKET = 'GI Market' THEN 5 -WHEN B.MARKET = 'ONC Market' THEN 6 -WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7 -WHEN B.MARKET = 'RARE Market' THEN 8--20250701 chenwu 新增RARE -ELSE 99 END TA_RN,'IQVIA-COUNTY(Quarterly)' DATA_SOURCE,'' as dept_name,'' as key_dept_name -,'' as new_code -,'' as area -FROM ( -SELECT B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET,MAX(C.TA) TA,MAX(E.KEY_COMPETITOR) KEY_COMPETITOR,MAX(E.IS_AZ) IS_AZ,MAX(E.AZ_MAIN) AZ_MAIN,MAX(E.AZ_RELATED) AZ_RELATED,MAX(E.CLASS) CLASS,MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO,MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO,MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO,MAX(D.PDOT_MARKET_RATIO) PDOT_MARKET_RATIO,MAX(G.MARKET_DESC) MARKET_DESC -FROM (SELECT PACK_COD,COUNT(*) RUM FROM DM.DM_TF_EXTERNAL_SALES --- WHERE YYYYMM >= (SELECT CAST((LEFT(MAX(YYYYMM),4)-3)*100+1 AS INT) FROM DM.DM_TF_EXTERNAL_SALES) --- AND DATA_SOURCE = 'IQVIA-COUNTY(Quarterly)' -where DATA_SOURCE = 'IQVIA-COUNTY(Quarterly)' -GROUP BY PACK_COD) A -LEFT JOIN dm.dm_ext_county_td_pack_property B -ON B.PACK_COD = A.PACK_COD --AND B.MARKET <> 'IMS ALL Market' -LEFT JOIN DM.DM_IMS_TD_MARKET_PROPERTY C -ON B.MARKET = C.MARKET -LEFT JOIN DM.dm_ext_county_td_pack_property E ON B.MARKET_PACK_KEY = E.MARKET_PACK_KEY -LEFT JOIN MKT_PACK_RATIO D ON B.MARKET = D.MARKET AND B.PACK_COD = D.PACK_COD -LEFT JOIN (SELECT MARKET,DATA_SOURCE,MAX(MARKETDESC) MARKET_DESC FROM DM.DM_TD_EXTERNAL_KEYCOMPATITOR GROUP BY MARKET,DATA_SOURCE) G ON B.MARKET = G.MARKET AND UPPER(G.DATA_SOURCE) = 'IQVIA-COUNTY(QUARTERLY)' -WHERE A.PACK_COD IS NOT NULL -GROUP BY B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET) B -WHERE B.PACK_COD IS NOT NULL AND B.MARKET IS NOT NULL - - --- COMMAND ---------- - -delete from dm.dm_td_external_market_pack_mapping_temp where DATA_SOURCE = 'THC(Quarterly)' - --- COMMAND ---------- - ------------------------------------------------THC--------------------------------------------------------------------------------------------------------- -with exchc_rawdata_temp as ( --主表数据 - select - temp.yq, - pack_property.iqvia_pack_code as pack_cod - from ( - select - CASE - WHEN SUBSTRING(yq, 5,2) = 'Q1' THEN CONCAT(SUBSTRING(yq, 1, 4), '03') - WHEN SUBSTRING(yq, 5, 2) = 'Q2' THEN CONCAT(SUBSTRING(yq, 1, 4), '06') - WHEN SUBSTRING(yq, 5, 2) = 'Q3' THEN CONCAT(SUBSTRING(yq, 1, 4), '09') - WHEN SUBSTRING(yq, 5, 2) = 'Q4' THEN CONCAT(SUBSTRING(yq, 1, 4), '12') - ELSE yq - END AS yq, - pack_cod - from dwd.dwd_gnd_ext_thc_rawdata - ) temp - LEFT JOIN dwd.dwd_gnd_ext_thc_pack_property pack_property - ON temp.pack_cod = pack_property.pack_cod -), -A as( - SELECT - PACK_COD, - COUNT(*) RUM - FROM exchc_rawdata_temp - -- WHERE yq >= (SELECT CAST((LEFT(MAX(yq),4)-3)*100+1 AS INT) FROM exchc_rawdata_temp) - GROUP BY PACK_COD -) -insert into table dm.dm_td_external_market_pack_mapping_temp -( - PACK_COD,PACK_DES,PROD_DES,PROD_DES_C,MARKET,TA,KEY_COMPETITOR,AZ_RELATED,CLASS, - VALUE_MARKET_RATIO,UNIT_MARKET_RATIO,COUNTINGUNIT_MARKET_RATIO,PDOT_MARKET_RATIO,MARKET_DESC,TA_RN,DATA_SOURCE,dept_name,key_dept_name - ,new_code - ,area -) -SELECT - B.PACK_COD, - B.PACK_DES, - B.PROD_DES, - B.PROD_DES_C, - B.MARKET, - B.TA, - B.KEY_COMPETITOR, - CASE - WHEN B.MARKET = 'DM Market' THEN 'DM' - WHEN B.MARKET = 'CV Market' THEN 'CV' - WHEN B.MARKET = 'RD Market' THEN 'RD' - WHEN B.MARKET = 'RE Market' THEN 'RE' - WHEN B.MARKET = 'GI Market' THEN 'GI' - WHEN B.MARKET = 'ONC Market' THEN 'ONCO' - WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD' - WHEN B.MARKET = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE - END AZ_RELATED, - B.Class, - cast (B.VALUE_MARKET_RATIO as double), - cast (B.UNIT_MARKET_RATIO as double), - cast (B.COUNTINGUNIT_MARKET_RATIO as double), - cast (B.PDOT_MARKET_RATIO as double), - B.MARKET_DESC, - CASE - WHEN B.MARKET = 'DM Market' THEN 1 - WHEN B.MARKET = 'CV Market' THEN 2 - WHEN B.MARKET = 'RD Market' THEN 3 - WHEN B.MARKET = 'RE Market' THEN 4 - WHEN B.MARKET = 'GI Market' THEN 5 - WHEN B.MARKET = 'ONC Market' THEN 6 - WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7 - WHEN B.MARKET = 'RARE Market' THEN 8--20250701 chenwu 新增RARE - ELSE 99 - END TA_RN, - 'THC(Quarterly)' DATA_SOURCE - ,'' as dept_name - ,'' as key_dept_name - ,'' as new_code - ,'' as area -FROM ( - SELECT - B.MARKET_PACK_KEY, - B.PACK_COD, - B.PACK_DES, - B.PROD_DES, - B.PROD_DES_C, - B.MARKET, - nvl(MAX(C.TA),'') as TA, - MAX(E.KEY_COMPETITOR) as KEY_COMPETITOR, - NULL IS_AZ, - NULL AZ_MAIN, - NULL AZ_Related, - nvl(MAX(E.CLASS),'') CLASS, - MAX(nvl(B.Market_Ratio,1)) as VALUE_MARKET_RATIO, -- sales * VALUE_MARKET_RATIO = 折算好的sales - MAX(nvl(B.Market_Ratio,1) ) as UNIT_MARKET_RATIO, -- unint * UNIT_MARKET_RATIO = 折算好的 UNIT - MAX(nvl(B.Market_Ratio,1)) as COUNTINGUNIT_MARKET_RATIO, -- counting_unit*COUNTINGUNIT_MARKET_RATIO = 折算好的 CU - MAX(nvl(B.Market_Ratio,1) * nvl(D.Ratio,1)) as PDOT_MARKET_RATIO, -- counting_unit*Market_Ratio*PDOT_MARKET_RATIO = PDOT - max(g.MARKET_DESC) MARKET_DESC - FROM A - LEFT JOIN dm.dm_thc_pack_property B -- 以A表 pack_code 为准 获取需要的维度信息 - ON B.PACK_COD = A.PACK_COD - LEFT JOIN DM.DM_IMS_TD_MARKET_PROPERTY C -- THC没有TA字段,关联CHC的相关表,获取TA - ON B.MARKET = C.MARKET - LEFT JOIN dm.dm_thc_pack_property E -- MARKET_PACK_KEY 为唯一主键,获取KEY_COMPETITOR,class等信息 - ON B.MARKET_PACK_KEY = E.MARKET_PACK_KEY - LEFT JOIN( --转码 补0 - select - MARKET, - b.iqvia_pack_code as pack_cod, - ratio - from dwd.dwd_gnd_ext_thc_brandratio a - inner join dwd.dwd_gnd_ext_thc_pack_property b - on (case when length(a.pack_cod) < 12 and a.pack_cod REGEXP '^[0-9]' then right(concat('000000000000',a.pack_cod),12) else a.pack_cod end ) - =(case when length(b.pack_cod) < 12 and b.pack_cod REGEXP '^[0-9]' then right(concat('000000000000',b.pack_cod),12) else b.pack_cod end ) - ) D - ON B.MARKET = D.MARKET - AND B.PACK_COD = D.PACK_COD - LEFT JOIN ( --获取 MARKET_DESC 字段 - SELECT - MARKET,DATA_SOURCE, - MAX(MARKETDESC) MARKET_DESC - FROM DM.DM_TD_EXTERNAL_KEYCOMPATITOR - GROUP BY MARKET,DATA_SOURCE - ) G - ON B.MARKET = G.MARKET AND UPPER(G.DATA_SOURCE) = 'THC(QUARTERLY)' - GROUP BY B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET -) B -WHERE B.PACK_COD IS NOT NULL AND B.MARKET IS NOT NULL - --- COMMAND ---------- - ------------------------------------------------CHC---------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------- - -with exchc_rawdata_temp as ( --主表数据 - select - temp.yq, - pack_property.iqvia_pack_code as pack_cod - from ( - select - CASE - WHEN SUBSTRING(yq, 5,2) = 'Q1' THEN CONCAT(SUBSTRING(yq, 1, 4), '03') - WHEN SUBSTRING(yq, 5, 2) = 'Q2' THEN CONCAT(SUBSTRING(yq, 1, 4), '06') - WHEN SUBSTRING(yq, 5, 2) = 'Q3' THEN CONCAT(SUBSTRING(yq, 1, 4), '09') - WHEN SUBSTRING(yq, 5, 2) = 'Q4' THEN CONCAT(SUBSTRING(yq, 1, 4), '12') - ELSE yq - END AS yq, - pack_cod - from dwd.dwd_gnd_chc_rawdata - ) temp - LEFT JOIN dwd.dwd_gnd_ext_chc_pack_property pack_property - ON temp.pack_cod = pack_property.pack_cod -), -A as( - SELECT - PACK_COD, - COUNT(*) RUM - FROM exchc_rawdata_temp - -- WHERE yq >= (SELECT CAST((LEFT(MAX(yq),4)-3)*100+1 AS INT) FROM exchc_rawdata_temp) - GROUP BY PACK_COD -) -insert into table dm.dm_td_external_market_pack_mapping_temp( - PACK_COD,PACK_DES,PROD_DES,PROD_DES_C,MARKET,TA,KEY_COMPETITOR,AZ_RELATED,CLASS, - VALUE_MARKET_RATIO,UNIT_MARKET_RATIO,COUNTINGUNIT_MARKET_RATIO,PDOT_MARKET_RATIO,MARKET_DESC,TA_RN,DATA_SOURCE,dept_name,key_dept_name - ,new_code - ,area -) -SELECT - B.PACK_COD, - B.PACK_DES, - B.PROD_DES, - B.PROD_DES_C, - B.MARKET, - B.TA, - B.KEY_COMPETITOR, - CASE - WHEN B.MARKET = 'DM Market' THEN 'DM' - WHEN B.MARKET = 'CV Market' THEN 'CV' - -- WHEN B.MARKET = 'RD Market' THEN 'RD' --RD隐藏 - when B.MARKET = 'RE Market' THEN 'RE' --RE改为Respules Market - WHEN B.MARKET = 'GI Market' THEN 'GI' - WHEN B.MARKET = 'ONC Market' THEN 'ONCO' - WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD' - WHEN B.MARKET = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE - END AZ_RELATED, - B.Class, - cast (B.VALUE_MARKET_RATIO as double), - cast (B.UNIT_MARKET_RATIO as double), - cast (B.COUNTINGUNIT_MARKET_RATIO as double), - cast (B.PDOT_MARKET_RATIO as double), - B.MARKET_DESC, - CASE - WHEN B.MARKET = 'DM Market' THEN 1 - WHEN B.MARKET = 'CV Market' THEN 2 - -- WHEN B.MARKET = 'RD Market' THEN 3 - WHEN B.MARKET = 'RE Market' THEN 4 - WHEN B.MARKET = 'GI Market' THEN 5 - WHEN B.MARKET = 'ONC Market' THEN 6 - WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7 - WHEN B.MARKET = 'RARE Market' THEN 8 --20250701 chenwu 新增RARE - ELSE 99 - END TA_RN, - 'CHC(Quarterly)' DATA_SOURCE - ,'' as dept_name - ,'' as key_dept_name - ,'' as new_code - ,'' as area -FROM ( - SELECT - B.MARKET_PACK_KEY, - B.PACK_COD, - B.PACK_DES, - B.PROD_DES, - B.PROD_DES_C, - B.MARKET, - nvl(MAX(C.TA) ,'') TA, - -- null as TA, - MAX(E.KEY_COMPETITOR) KEY_COMPETITOR, - --MAX(B.KEY_COMPETITOR) KEY_COMPETITOR, - --MAX(E.IS_AZ) IS_AZ, - --MAX(E.AZ_MAIN) AZ_MAIN, - --MAX(E.AZ_RELATED) AZ_RELATED, - NULL as IS_AZ, - NULL as AZ_MAIN, - NULL as AZ_Related, - nvl (MAX(E.CLASS) ,'' ) CLASS, - MAX(nvl(B.Market_Ratio,1)) as VALUE_MARKET_RATIO, -- sales * VALUE_MARKET_RATIO = 折算好的sales - MAX(nvl(B.Market_Ratio,1)) as UNIT_MARKET_RATIO, -- unint * UNIT_MARKET_RATIO = 折算好的 UNIT - MAX(nvl(B.Market_Ratio,1)) as COUNTINGUNIT_MARKET_RATIO, -- counting_unit*COUNTINGUNIT_MARKET_RATIO = 折算好的 CU - MAX(nvl(B.Market_Ratio,1) * nvl(D.Ratio,1)) PDOT_MARKET_RATIO, -- counting_unit*Market_Ratio*PDOT_MARKET_RATIO = PDOT - max(G.MARKET_DESC) as MARKET_DESC - FROM A - LEFT JOIN dm.dm_chc_pack_property B -- 以A表 pack_code 为准 获取需要的维度信息 - ON B.PACK_COD = A.PACK_COD - LEFT JOIN DM.DM_IMS_TD_MARKET_PROPERTY C -- THC没有TA字段,关联CHPA的相关表,获取TA - ON B.MARKET = C.MARKET - LEFT JOIN dm.dm_chc_pack_property E --MARKET_PACK_KEY 为唯一主键,获取KEY_COMPETITOR,class等信息 - ON B.MARKET_PACK_KEY = E.MARKET_PACK_KEY - LEFT JOIN( -- 获取RATIO ,其中chc_brandratio没有补0和转码的操作,这里处理 - select - MARKET, - b.iqvia_pack_code as pack_cod, - ratio - from dwd.dwd_gnd_ext_chc_brandratio a - inner join dwd.dwd_gnd_ext_chc_pack_property b - on (case when length(a.pack_cod) < 12 and a.pack_cod REGEXP '^[0-9]' then right(concat('000000000000',a.pack_cod),12) else a.pack_cod end ) - =(case when length(b.pack_cod) < 12 and b.pack_cod REGEXP '^[0-9]' then right(concat('000000000000',b.pack_cod),12) else b.pack_cod end ) - ) D - ON B.MARKET = D.MARKET - AND B.PACK_COD = D.PACK_COD - LEFT JOIN ( --获取 MARKET_DESC 字段 - SELECT - MARKET,DATA_SOURCE, - MAX(MARKETDESC) MARKET_DESC - FROM DM.DM_TD_EXTERNAL_KEYCOMPATITOR - GROUP BY MARKET,DATA_SOURCE - ) G - ON B.MARKET = G.MARKET AND UPPER(G.DATA_SOURCE) = 'CHC(QUARTERLY)' - GROUP BY B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET - -) B -WHERE B.PACK_COD IS NOT NULL AND B.MARKET IS NOT NULL -and market!='RD Market' - --- COMMAND ---------- - -/* -修改人:chenwu -修改时间:20250319 -修改内容: EC的数据源cu恒等于1 -*/ ------------------------------------------------------------ec----------------------------------------------------------- -INSERT into dm.dm_td_external_market_pack_mapping_temp( - PACK_COD,PACK_DES,PROD_DES,PROD_DES_C,MARKET,TA,KEY_COMPETITOR,AZ_RELATED,CLASS, - VALUE_MARKET_RATIO,UNIT_MARKET_RATIO,COUNTINGUNIT_MARKET_RATIO,PDOT_MARKET_RATIO,MARKET_DESC,TA_RN,DATA_SOURCE,dept_name,key_dept_name - ,new_code - ,area -) -SELECT - B.PACK_COD, - B.PACK_DES, - B.PROD_DES, - B.PROD_DES_C, - B.MARKET, - CASE - WHEN B.MARKET = 'RE Market' THEN 'RE' - when B.TA = 'RE' THEN NULL--只有 'RE Market (Global)' 的ta是 RE - ELSE B.TA - END TA, - B.KEY_COMPETITOR, - CASE - WHEN B.MARKET = 'RE Market' THEN 'RE' - WHEN B.TA = 'RE' THEN NULL - ELSE B.TA - END AZ_RELATED, - B.CLASS, - nvl(B.VALUE_MARKET_RATIO, 1), - nvl(B.UNIT_MARKET_RATIO, 1), - 1, - --nvl(B.COUNTINGUNIT_MARKET_RATIO,1), --ec的数据源cu恒等于1 - nvl(B.PDOT_MARKET_RATIO, 1), - B.MARKET_DESC, - CASE - WHEN b.TA = 'DM' THEN 1 - WHEN b.TA = 'CV' THEN 2 - WHEN b.TA = 'RE' THEN 4 - WHEN b.TA = 'GI' THEN 5 - WHEN B.TA = 'CVRM' THEN 0 - ELSE 99 - END TA_RN, - 'EC(Monthly)' DATA_SOURCE, - '' as dept_name, - '' as key_dept_name, - '' as new_code, - '' as area -FROM - ( - SELECT - a.MARKET_PACK_KEY, - B.iqvia_pack_code PACK_COD, - a.PACK_DES, - a.PROD_DES, - a.PROD_DES_C, - a.MARKET, - MAX(a.TA) TA, - MAX(a.KEY_COMPETITOR) KEY_COMPETITOR, - MAX(a.CLASS) CLASS, - MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO, - MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO, - MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO, - MAX(D.COUNTINGUNIT_MARKET_RATIO) PDOT_MARKET_RATIO, - max(g.MARKET_DESC) MARKET_DESC - FROM - DM.dm_zk_ec_pack_property a - LEFT JOIN DM.dm_zk_ec_market_property B ON A.iqvia_pack_code = B.iqvia_pack_code - and a.market = b.market - LEFT JOIN dm.dm_zk_ec_market_ratio D ON B.MARKET = D.MARKET - AND B.iqvia_pack_code = D.PACK_COD - LEFT JOIN ( - SELECT - MARKET, - DATA_SOURCE, - MAX(MARKETDESC) MARKET_DESC - FROM - DM.DM_TD_EXTERNAL_KEYCOMPATITOR - GROUP BY - MARKET, - DATA_SOURCE - ) G ON B.MARKET = G.MARKET - AND UPPER(G.DATA_SOURCE) = 'EC(MONTHLY)' - WHERE - A.iqvia_pack_code in ( - select - distinct iqvia_pack_code - from - dm.dm_zk_ec_sales - ) - GROUP BY - a.MARKET_PACK_KEY, - B.iqvia_pack_code, - a.PACK_DES, - a.PROD_DES, - a.PROD_DES_C, - a.MARKET - ) b - - --- COMMAND ---------- - - ------------------------------------------------AIA---------------------------------------------------------------------------------- -WITH AIA_brand_ratio as ( - SELECT distinct market, - CASE WHEN length(cmps_cod) < 6 THEN RIGHT(CONCAT('000000',cmps_cod),6) ELSE cmps_cod END AS cmps_cod, - cmps_des, - CASE WHEN length(pack_cod) < 12 and pack_cod REGEXP '^[0-9]' THEN RIGHT(CONCAT('000000000000',pack_cod),12) ELSE pack_cod END AS pack_cod, - pack_des, - level, - type, - ratio - from dwd.dwd_gnd_ims_tblBrandRatio -) -,AIA_RATIO as ( -select t1.PACK_COD, -t1.MARKET, -case when t6.ratio is not null then t1.market_ratio * t6.ratio - else t1.market_ratio * NVL(t7.ratio,1) -end as VALUE_MARKET_RATIO, -case when t2.ratio is not null then t1.market_ratio * t2.ratio - else t1.market_ratio * NVL(t3.ratio,1) -end as UNIT_MARKET_RATIO, -t1.market_ratio as COUNTINGUNIT_MARKET_RATIO, -case when t4.ratio is not null then t1.market_ratio * t4.ratio - else t1.market_ratio * NVL(t5.ratio,1) -end as PDOT_MARKET_RATIO -from dm.dm_aia_pack_property t1 ---unit by Pack -left join (select * from AIA_brand_ratio where upper(type) = 'UNIT' and upper(Level) = 'PACK') t2 -on t1.PACK_COD = t2.pack_cod -and t1.MARKET = t2.market ---unit by MOLECULE -left join (select * from AIA_brand_ratio where upper(type) = 'UNIT' and upper(Level) = 'MOLECULE') t3 -on t1.CMPS_COD = t3.cmps_cod -and t1.MARKET = t3.market ---PDOT by Pack -left join (select * from AIA_brand_ratio where upper(type) = 'COUNTINGUNIT' and upper(Level) = 'PACK') t4 -on t1.PACK_COD = t4.pack_cod -and t1.MARKET = t4.market ---PDOT by MOLECULE -left join (select * from AIA_brand_ratio where upper(type) = 'COUNTINGUNIT' and upper(Level) = 'MOLECULE') t5 -on t1.CMPS_COD = t5.cmps_cod -and t1.MARKET = t5.market ---VALUE by Pack -left join (select * from AIA_brand_ratio where upper(type) = 'VALUE' and upper(Level) = 'PACK') t6 -on t1.PACK_COD = t6.pack_cod -and t1.MARKET = t6.market ---VALUE by MOLECULE -left join (select * from AIA_brand_ratio where upper(type) = 'VALUE' and upper(Level) = 'MOLECULE') t7 -on t1.CMPS_COD = t7.cmps_cod -and t1.MARKET = t7.market - -) -,MARKET_PACK_MAPPING_1 as ( - select - B.MARKET_PACK_KEY, - B.PACK_COD, - B.PACK_DES, - B.PROD_DES, - B.PROD_DES_C, - B.MARKET as MARKET_AIA, - --AIA有一部分market有通过手工表AIA_IMS_Market_Mapping更新过名称 - --使用旧的从CHPA里来的名称关联 - B.MARKET_CHPA, - nvl(MAX(C.TA),'') as TA, - MAX(E.KEY_COMPETITOR) KEY_COMPETITOR, - NULL as IS_AZ, - NULL as AZ_MAIN, - NULL as AZ_Related, - nvl(MAX(E.CLASS),'') as CLASS, - MAX(t1.VALUE_MARKET_RATIO) as VALUE_MARKET_RATIO, -- sales * VALUE_MARKET_RATIO = 折算好的sales - MAX(t1.UNIT_MARKET_RATIO) as UNIT_MARKET_RATIO, -- unint * UNIT_MARKET_RATIO = 折算好的 UNIT - MAX(t1.COUNTINGUNIT_MARKET_RATIO) as COUNTINGUNIT_MARKET_RATIO, -- counting_unit*COUNTINGUNIT_MARKET_RATIO = 折算好的 CU - MAX(t1.PDOT_MARKET_RATIO) as PDOT_MARKET_RATIO, -- counting_unit*Market_Ratio*PDOT_MARKET_RATIO = PDOT - max(G.MARKET_DESC) as MARKET_DESC - from ( - SELECT - PACK_COD, - COUNT(*) RUM - FROM DM.DM_TF_EXTERNAL_SALES - WHERE DATA_SOURCE = 'AIA(Monthly)' - GROUP BY PACK_COD - ) A - left join dm.dm_aia_pack_property B - on A.PACK_COD = B.PACK_COD - left join DM.DM_IMS_TD_MARKET_PROPERTY C - --AIA有一部分market有通过手工表AIA_IMS_Market_Mapping更新过名称 - --使用旧的从CHPA里来的名称关联 - on B.MARKET_CHPA = C.MARKET - left join dm.dm_aia_pack_property E - on B.MARKET_PACK_KEY = E.MARKET_PACK_KEY - left join AIA_RATIO t1 - on B.PACK_COD = t1.PACK_COD - and B.MARKET = t1.MARKET - left join ( - select MARKET,DATA_SOURCE, - MAX(MARKETDESC) MARKET_DESC - from DM.DM_TD_EXTERNAL_KEYCOMPATITOR - group by market,DATA_SOURCE - ) G - --竞品配置信息表里使用的AIA的市场名称,因此使用AIA市场名关联 - on B.MARKET = G.MARKET and upper(G.DATA_SOURCE) = 'AIA(Monthly)' - group by B.MARKET_PACK_KEY,B.PACK_COD,B.PACK_DES,B.PROD_DES,B.PROD_DES_C,B.MARKET,B.MARKET_CHPA -) - - -insert into table dm.dm_td_external_market_pack_mapping_temp ( -PACK_COD, -PACK_DES, -PROD_DES, -PROD_DES_C, -MARKET, -TA, -KEY_COMPETITOR, -AZ_RELATED, -CLASS, -VALUE_MARKET_RATIO, -UNIT_MARKET_RATIO, -COUNTINGUNIT_MARKET_RATIO, -PDOT_MARKET_RATIO, -MARKET_DESC, -TA_RN, -DATA_SOURCE, -dept_name, -key_dept_name, -new_code, -area -) - select - PACK_COD, - PACK_DES, - PROD_DES, - PROD_DES_C, - MARKET_AIA as MARKET, - TA, - KEY_COMPETITOR, - case when MARKET_CHPA = 'DM Market' THEN 'DM' - when MARKET_CHPA = 'CV Market' THEN 'CV' - when MARKET_CHPA = 'RD Market' THEN 'RD' - when MARKET_CHPA = 'RE Market' THEN 'RE' - when MARKET_CHPA = 'GI Market' THEN 'GI' - when MARKET_CHPA = 'ONC Market' THEN 'ONCO' - when MARKET_CHPA = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD' - WHEN MARKET_CHPA = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE - end as AZ_RELATED, - Class, - cast(VALUE_MARKET_RATIO as double) as VALUE_MARKET_RATIO, - cast(UNIT_MARKET_RATIO as double) as UNIT_MARKET_RATIO, - cast(COUNTINGUNIT_MARKET_RATIO as double) as COUNTINGUNIT_MARKET_RATIO, - cast(PDOT_MARKET_RATIO as double) as PDOT_MARKET_RATIO, - MARKET_DESC, - case when MARKET_CHPA = 'DM Market' THEN 1 - when MARKET_CHPA = 'CV Market' THEN 2 - when MARKET_CHPA = 'RD Market' THEN 3 - when MARKET_CHPA = 'RE Market' THEN 4 - when MARKET_CHPA = 'GI Market' THEN 5 - when MARKET_CHPA = 'ONC Market' THEN 6 - when MARKET_CHPA = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7 - WHEN MARKET_CHPA = 'RARE Market' THEN 8--20250701 chenwu 新增RARE - else 99 - end as TA_RN, - 'AIA(Monthly)' as DATA_SOURCE - ,'' as dept_name - ,'' as key_dept_name - ,'' as new_code - ,'' as area - from MARKET_PACK_MAPPING_1 - where PACK_COD IS NOT NULL - and MARKET_AIA IS NOT NULL - - --- COMMAND ---------- - -INSERT into table dm.dm_td_external_market_pack_mapping_temp( - PACK_COD, - PACK_DES, - PROD_DES, - PROD_DES_C, - MARKET, - TA, - KEY_COMPETITOR, - AZ_RELATED, - CLASS, - VALUE_MARKET_RATIO, - UNIT_MARKET_RATIO, - COUNTINGUNIT_MARKET_RATIO, - PDOT_MARKET_RATIO, - MARKET_DESC, - TA_RN, - DATA_SOURCE, - dept_name, - key_dept_name, - new_code, - area - ) - - WITH MKT_PACK_RATIO AS ( - -------------------------------------根据沟通,市场+产品对应的ratio是唯一的------------------------------------------ - SELECT - A.MARKET, - A.PACK_COD, - MAX(A.Value_Market_Ratio) Value_Market_Ratio, - MAX(A.Unit_Market_Ratio) Unit_Market_Ratio, - MAX(A.PDOT_Market_Ratio) CountingUnit_Market_Ratio, - MAX(A.CountingUnit_Market_Ratio) PDOT_Market_Ratio --因为之前的报告在使用源表的数据,因此命名是反的。 - FROM - DWS.dws_ims_td_market_ratio A - WHERE - A.YM = ( - SELECT - MAX(YM) - FROM - DWS.dws_ims_td_market_ratio - ) - GROUP BY - A.MARKET, - A.PACK_COD - ) -------------------------------------------------------------------xiehe------------------------------------------------------------------ -SELECT - B.PACK_COD, - B.PACK_DES, - B.PROD_DES, - B.PROD_DES_C, - B.MARKET, - B.TA, - B.KEY_COMPETITOR, - CASE - WHEN B.MARKET = 'DM Market' THEN 'DM' - WHEN B.MARKET = 'CV Market' THEN 'CV' - WHEN B.MARKET = 'RD Market' THEN 'RD' - WHEN B.MARKET = 'RE Market' THEN 'RE' - WHEN B.MARKET IN ('GI Market', 'Retail_GI Market') THEN 'GI' - WHEN B.MARKET = 'ONC Market' THEN 'ONCO' - WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 'NIAD' - WHEN B.MARKET = 'RARE Market' THEN 'RARE'--20250701 chenwu 新增RARE - END AZ_RELATED, - B.CLASS, - NVL(B.VALUE_MARKET_RATIO, 1) VALUE_MARKET_RATIO, - NVL(B.UNIT_MARKET_RATIO, 1) UNIT_MARKET_RATIO, - NVL(B.COUNTINGUNIT_MARKET_RATIO, 1) COUNTINGUNIT_MARKET_RATIO, - NVL(B.PDOT_MARKET_RATIO, 1) PDOT_MARKET_RATIO, - B.MARKET_DESC, - CASE - WHEN B.MARKET = 'DM Market' THEN 1 - WHEN B.MARKET = 'CV Market' THEN 2 - WHEN B.MARKET = 'RD Market' THEN 3 - WHEN B.MARKET = 'RE Market' THEN 4 - WHEN B.MARKET = 'GI Market' THEN 5 - WHEN B.MARKET = 'ONC Market' THEN 6 - WHEN B.MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' THEN 7 - WHEN B.MARKET = 'RARE Market' THEN 8 --20250701 chenwu 新增RARE - ELSE 99 - END TA_RN, - 'XH Data(Quarterly)' DATA_SOURCE, - B.dept_name, - B.key_dept_name, - B.new_code, - B.area -FROM - ( - SELECT - B.MARKET_PACK_KEY, - B.PACK_COD, - B.PACK_DES, - B.PROD_DES, - B.PROD_DES_C, - B.MARKET, - NVL(H.dept_name,'') dept_name, - NVL(t1.CORE_DEPT,'Others') as key_dept_name, - NVL(H.new_code,'') new_code, - NVL(H.area,'') area, - MAX(C.TA) TA, - MAX(E.KEY_COMPETITOR) KEY_COMPETITOR, - MAX(E.IS_AZ) IS_AZ, - MAX(E.AZ_MAIN) AZ_MAIN, - MAX(E.AZ_RELATED) AZ_RELATED, - MAX(E.CLASS) CLASS, - MAX(D.VALUE_MARKET_RATIO) VALUE_MARKET_RATIO, - MAX(D.UNIT_MARKET_RATIO) UNIT_MARKET_RATIO, - MAX(D.COUNTINGUNIT_MARKET_RATIO) COUNTINGUNIT_MARKET_RATIO, - MAX(D.PDOT_MARKET_RATIO) PDOT_MARKET_RATIO, - max(g.MARKET_DESC) MARKET_DESC - FROM - ( - SELECT - PACK_COD, - COUNT(*) RUM - FROM - DM.DM_TF_EXTERNAL_SALES - WHERE DATA_SOURCE = 'XH Data(Quarterly)' - GROUP BY - PACK_COD - ) A - LEFT JOIN dm.dm_xiehe_pack_property B ON B.PACK_COD = A.PACK_COD --AND B.MARKET <> 'IMS ALL Market' - LEFT JOIN DM.DM_IMS_TD_MARKET_PROPERTY C ON B.MARKET = C.MARKET - LEFT JOIN dm.dm_xiehe_pack_property E ON B.MARKET_PACK_KEY = E.MARKET_PACK_KEY - LEFT JOIN MKT_PACK_RATIO D ON B.MARKET = D.MARKET - AND B.PACK_COD = D.PACK_COD - LEFT JOIN ( - SELECT - MARKET,DATA_SOURCE, - MAX(MARKETDESC) MARKET_DESC - FROM - DM.DM_TD_EXTERNAL_KEYCOMPATITOR - GROUP BY - MARKET,DATA_SOURCE - ) G ON B.MARKET = G.MARKET and G.DATA_SOURCE = 'XH Data(Quarterly)' - left join ( - SELECT distinct PACK_COD,dept_name,new_code,area - from dm.dm_td_external_packinfo_temp2 - where DATA_SOURCE = 'XH Data(Quarterly)' - ) H - on A.PACK_COD = H.PACK_COD - left join dm.dm_td_xiehe_core_dept t1 - on B.MARKET = t1.MARKET - AND H.dept_name = t1.Dept_NAME - GROUP BY - B.MARKET_PACK_KEY, - B.PACK_COD, - B.PACK_DES, - B.PROD_DES, - B.PROD_DES_C, - B.MARKET, - H.dept_name, - H.new_code, - H.area, - t1.CORE_DEPT - ) B -WHERE - B.PACK_COD IS NOT NULL - AND B.MARKET IS NOT NULL - --- COMMAND ---------- - ---修改时间:20240808 ---修改人:FanXuJia ---修改背景: ---事实表与维度表来自于不同手工表,目前存在一部分pack_cod在事实表有销量,但是在维度表没有这个pack_cod,导致销量缺失。 ---因此需要从事实表补全这部分pack_cod -with tmp_market_pack as ( -select distinct - t1.PACK_COD, - CASE WHEN t1.DATA_SOURCE = 'CHC(Quarterly)' then 'CHC ALL Market' - WHEN t1.DATA_SOURCE = 'IQVIA-CHPA(Monthly)' then 'IMS ALL Market' - WHEN t1.DATA_SOURCE = 'IQVIA-COUNTY(Quarterly)' then 'IMS ALL Market' - WHEN t1.DATA_SOURCE = 'Retail(Quarterly)' then 'Retail All Market' - WHEN t1.DATA_SOURCE = 'THC(Quarterly)' then 'THC ALL Market' - WHEN t1.DATA_SOURCE = 'EC(Monthly)' then 'EC ALL Market' - WHEN t1.DATA_SOURCE = 'AIA(Monthly)' then 'NON Market' - WHEN t1.DATA_SOURCE = 'XH Data(Quarterly)' then 'XIEHE ALL Market' - WHEN t1.DATA_SOURCE = 'DTP(Quarterly)' then 'DTP ALL Market' - end as market, - t1.DATA_SOURCE -from external_sales_union t1 -left join dm.dm_td_external_market_pack_mapping_temp t2 -on t1.PACK_COD = t2.PACK_COD -and t1.DATA_SOURCE = t2.DATA_SOURCE -where t2.PACK_COD is null -) -,tmp_packinfo as ( -select -PACK_COD, -new_code, -area, -dept_name, -DATA_SOURCE, -max(PACK_DES) as PACK_DES, -max(PROD_DES) as PROD_DES, -max(PROD_DES_C) as PROD_DES_C -from dm.dm_td_external_packinfo_temp2 -group by PACK_COD,DATA_SOURCE,dept_name,new_code,area -) - -INSERT into table dm.dm_td_external_market_pack_mapping_temp( - PACK_COD, - PACK_DES, - PROD_DES, - PROD_DES_C, - MARKET, - TA, - KEY_COMPETITOR, - AZ_RELATED, - CLASS, - VALUE_MARKET_RATIO, - UNIT_MARKET_RATIO, - COUNTINGUNIT_MARKET_RATIO, - PDOT_MARKET_RATIO, - MARKET_DESC, - TA_RN, - DATA_SOURCE, - dept_name, - key_dept_name, - new_code, - area - ) - select - t1.PACK_COD, - t2.PACK_DES, - t2.PROD_DES, - t2.PROD_DES_C, - t1.MARKET, - null as TA, - null as KEY_COMPETITOR, - null as AZ_RELATED, - null as CLASS, - 1 as VALUE_MARKET_RATIO, - 1 as UNIT_MARKET_RATIO, - 1 as COUNTINGUNIT_MARKET_RATIO, - 1 as PDOT_MARKET_RATIO, - null as MARKET_DESC, - null as TA_RN, - t1.DATA_SOURCE, - t2.dept_name, - NVL(t3.CORE_DEPT,'Others') as key_dept_name, - t2.new_code, - t2.area - from tmp_market_pack t1 - left join tmp_packinfo t2 - on t1.PACK_COD = t2.PACK_COD - and t1.DATA_SOURCE = t2.DATA_SOURCE - left join dm.dm_td_xiehe_core_dept t3 - on t1.MARKET = t3.MARKET - AND t2.dept_name = t3.Dept_NAME - - --- COMMAND ---------- - ------------------------------------------------------------ ---修改时间:20241118 ---修改人:Fanxujia ---修改内容: ---使用手工文件排除掉每个渠道的不需要显示的市场,不再Hardcoding ------------------------------------------------------------ --- ---------------------------------------------------------------------------- --- --修改时间:20240813 --- --修改人:FanXujia --- --修改内容:CHC_AZ related TA ALL这个市场只在CHC中显示,其他渠道排除这个市场(Hard Coding) --- ---------------------------------------------------------------------------- --- delete from dm.dm_td_external_market_pack_mapping_temp --- where upper(MARKET) = 'CHC_AZ RELATED TA ALL' --- and data_source <> 'CHC(Quarterly)' - --- COMMAND ---------- - ------------------------------------------------------------ ---修改时间:20241118 ---修改人:Fanxujia ---修改内容: ---使用手工文件排除掉每个渠道的不需要显示的市场 ------------------------------------------------------------ -with hidden_market as ( -select distinct -case when upper(data_source) = 'CHPA' then 'IQVIA-CHPA(Monthly)' - when upper(data_source) = '科室市场' then 'XH Data(Quarterly)' - when upper(data_source) = 'EC' then 'EC(Monthly)' - when upper(data_source) = 'RETAIL' then 'Retail(Quarterly)' - when upper(data_source) = 'CHC' then 'CHC(Quarterly)' - when upper(data_source) = 'AIA' then 'AIA(Monthly)' - when upper(data_source) = 'THC' then 'THC(Quarterly)' - when upper(data_source) = 'COUNTY' then 'IQVIA-COUNTY(Quarterly)' - else data_source -end as data_source, -upper(hidden_market) as hidden_market -from dwd.dwd_gnd_ims_hidden_market -union all -select -'AIA(Monthly)' as data_source, -'AIA ALL Market' -) - - -MERGE into dm.dm_td_external_market_pack_mapping_temp as t1 -USING hidden_market t2 -on t1.data_source = t2.data_source -and upper(t1.MARKET) = upper(t2.hidden_market) -WHEN MATCHED THEN -DELETE - --- COMMAND ---------- - --------------------------------------------------------------------------------- ---修改时间:20241025 ---修改人:Fanxujia ---修改内容: ---外部数据报告优化,将PBI的逻辑搬到DBR --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- ---修改时间:20241209 ---修改人:Fanxujia ---修改内容: ---新增Key Competitor CN,从packinfo表里关联出中文名,关联不到的保留英文名 --------------------------------------------------------------------------------- -with mapping as ( -select distinct PROD_DES,PROD_DES_C -from dm.dm_td_external_packinfo -) -,market_pack_mapping_1 as ( -SELECT - PACK_COD, - max(PROD_DES) PROD_DES, - max(PROD_DES_C) PROD_DES_C, - MARKET, - ---------------------------------------------------------------------------------- - --修改时间:20241217 - --修改人:Fanxujia - --修改内容: - --取不到Class时,不再显示成中划线-,改成Others - ---------------------------------------------------------------------------------- - CASE - -- WHEN NVL(max(CLASS), '') = '' OR max(CLASS) = 'Others' THEN '-' - WHEN NVL(max(CLASS), '') = '' THEN 'Others' - ELSE UPPER(max(CLASS)) - END CLASS, - CAST(max(VALUE_MARKET_RATIO) AS DECIMAL(38, 20)) VALUE_MARKET_RATIO, - CAST(max(UNIT_MARKET_RATIO) AS DECIMAL(38, 20)) UNIT_MARKET_RATIO, - CAST(max(COUNTINGUNIT_MARKET_RATIO) AS DECIMAL(38, 20)) COUNTINGUNIT_MARKET_RATIO, - CAST(max(PDOT_MARKET_RATIO) AS DECIMAL(38, 20)) PDOT_MARKET_RATIO, - DATA_SOURCE, - case when max(KEY_COMPETITOR) is null or max(KEY_COMPETITOR) = '' then 'Others' - else max(KEY_COMPETITOR) - end as KEY_COMPETITOR, - NEW_CODE -FROM DM.dm_td_external_market_pack_mapping_temp -group by PACK_COD,NEW_CODE,DATA_SOURCE,MARKET -) - -, mapping_match_key_competitor_with_mutil as ( -select -t3.PACK_COD, -t3.PROD_DES, -t3.PROD_DES_C, -t3.MARKET, -t3.CLASS, -t3.VALUE_MARKET_RATIO, -t3.UNIT_MARKET_RATIO, -t3.COUNTINGUNIT_MARKET_RATIO, -t3.PDOT_MARKET_RATIO, -t3.DATA_SOURCE, -case WHEN t3.KEY_COMPETITOR ='Others' and (UPPER(t2.keycompetitor) is not null or UPPER(t2.keycompetitor) <>'') -THEN upper(t2.keycompetitor) ELSE t3.KEY_COMPETITOR -end as KEY_COMPETITOR, -t3.NEW_CODE -,t2.no1 - from market_pack_mapping_1 t3 -left join dm.dm_td_external_packinfo t1 -on t1.PACK_COD = t3.PACK_COD and t1.DATA_SOURCE = t3.DATA_SOURCE -left JOIN (SELECT ROW_NUMBER() OVER(ORDER BY - CASE - WHEN ATC1_Code IS NOT NULL THEN 1 - WHEN ATC2_Code IS NOT NULL THEN 2 - WHEN ATC3_Code IS NOT NULL THEN 3 - WHEN ATC4_Code IS NOT NULL THEN 4 - WHEN Molecule_Code IS NOT NULL THEN 5 - WHEN Product_Code IS NOT NULL THEN 6 - WHEN Pack_Code IS NOT NULL THEN 7 - ELSE 999 END, - CASE WHEN NFC1_CODE IS NULL THEN 0 ELSE 1 END, - CASE WHEN NFC2_CODE IS NULL THEN 0 ELSE 1 END, - CASE WHEN NFC2_CODE IS NULL THEN 0 ELSE 1 END, - CASE WHEN NFC3_CODE IS NULL THEN 0 ELSE 1 END - ) AS no1,* FROM dwd.dwd_gnd_ims_tblkeycompetitor)t2 - on - COALESCE(t1.ATC1_COD,'') = case when t2.ATC1_Code is null then COALESCE(t1.ATC1_COD,'') else t2.ATC1_Code end - and COALESCE(t1.ATC2_COD,'') = case when t2.ATC2_Code is null then COALESCE(t1.ATC2_COD,'') else t2.ATC2_Code end - and COALESCE(t1.ATC3_COD,'') = case when t2.ATC3_Code is null then COALESCE(t1.ATC3_COD,'') else t2.ATC3_Code end - and COALESCE(t1.ATC4_COD,'') = case when t2.ATC4_Code is null then COALESCE(t1.ATC4_COD,'') else t2.ATC4_Code end - and COALESCE(t1.APP1_COD,'') = case when t2.NFC1_Code is null then COALESCE(t1.APP1_COD,'') else t2.NFC1_Code end - and COALESCE(t1.APP2_COD,'') = case when t2.NFC2_Code is null then COALESCE(t1.APP2_COD,'') else t2.NFC2_Code end - and COALESCE(t1.APP3_COD,'') = case when t2.NFC3_Code is null then COALESCE(t1.APP3_COD,'') else t2.NFC3_Code end - and COALESCE(t1.CORP_COD,'') = case when t2.corporation_code is null then COALESCE(t1.CORP_COD,'') else t2.corporation_code end - and COALESCE(t1.MANU_COD,'') = case when t2.Manufacturer_Code is null then COALESCE(t1.MANU_COD,'') else t2.Manufacturer_Code end - and COALESCE(t1.PROD_COD,'') = case when t2.Product_Code is null then COALESCE(t1.PROD_COD,'') else t2.Product_Code end - and COALESCE(t1.PACK_COD,'') = case when t2.Pack_Code is null then COALESCE(t1.PACK_COD,'') else t2.Pack_Code end - and COALESCE(t1.STGH_DES,'') = case when t2.Strength is null then COALESCE(t1.STGH_DES,'') else t2.Strength end - and COALESCE(t1.CMPS_COD,'') = case when t2.Molecule_Code is null then COALESCE(t1.CMPS_COD,'') else t2.Molecule_Code end - and COALESCE(t3.market ,'') = case when t2.market is null then COALESCE(t3.market,'') else t2.market end -) - -,mapping_match_key_competitor as ( - select * from (select *, row_number() over (partition by PACK_COD,MARKET,DATA_SOURCE,NEW_CODE,CLASS order by no1 desc ) as new_order FROM mapping_match_key_competitor_with_mutil ) as new - WHERE new.new_order =1 - ) - -insert overwrite table DM.dm_td_external_market_pack_mapping -( -PACK_COD, -PROD_DES, -PROD_DES_C, -MARKET, -CLASS, -VALUE_MARKET_RATIO, -UNIT_MARKET_RATIO, -COUNTINGUNIT_MARKET_RATIO, -PDOT_MARKET_RATIO, -DATA_SOURCE, -KEY_COMPETITOR, -KEY_COMPETITOR_CN, -NEW_CODE -) - -SELECT - t1.PACK_COD, - t1.PROD_DES, - t1.PROD_DES_C, - t1.MARKET, - t1.CLASS, - t1.VALUE_MARKET_RATIO, - t1.UNIT_MARKET_RATIO, - t1.COUNTINGUNIT_MARKET_RATIO, - t1.PDOT_MARKET_RATIO, - t1.DATA_SOURCE, - t1.KEY_COMPETITOR, - COALESCE(t2.PROD_DES_C,t1.KEY_COMPETITOR) as KEY_COMPETITOR_CN, - t1.NEW_CODE -FROM mapping_match_key_competitor t1 -left join mapping t2 -on upper(t1.KEY_COMPETITOR) = upper(t2.PROD_DES) - --- COMMAND ---------- - -update dm.dm_td_external_market_pack_mapping - set KEY_COMPETITOR = 'OTHERS',KEY_COMPETITOR_CN ='OTHERS' -where upper(KEY_COMPETITOR) = 'OTHERS' or upper(KEY_COMPETITOR_CN) = 'OTHERS' - --- COMMAND ---------- - -create or replace temporary view dim_KEY_COMPETITOR -as -select upper(KEY_COMPETITOR) as KEY_COMPETITOR,max(KEY_COMPETITOR_CN) as KEY_COMPETITOR_CN -from dm.dm_td_external_market_pack_mapping -group by KEY_COMPETITOR - --- COMMAND ---------- - -MERGE INTO dm.dm_td_external_market_pack_mapping AS t1 -USING dim_KEY_COMPETITOR AS t2 - ON t1.KEY_COMPETITOR = t2.KEY_COMPETITOR -WHEN MATCHED THEN - UPDATE SET t1.KEY_COMPETITOR_CN = t2.KEY_COMPETITOR_CN; diff --git a/z 11 dm_td_external_market_作废.sql b/z 11 dm_td_external_market_作废.sql deleted file mode 100644 index e816a9c..0000000 --- a/z 11 dm_td_external_market_作废.sql +++ /dev/null @@ -1,186 +0,0 @@ --- Databricks notebook source -INSERT OVERWRITE dm.dm_td_external_market ( - MARKET, DATA_SOURCE, TA, AZ_RELATED, MARKET_DESC, TA_RN, PRD_FLAG -) - SELECT - MARKET, - DATA_SOURCE, - MAX(TA) TA, - MAX(AZ_RELATED) AZ_RELATED, - MAX(MARKET_DESC) MARKET_DESC, - MAX(TA_RN) TA_RN, - case - when - DATA_SOURCE = 'Retail(Quarterly)' - and MARKET in ( - 'Antacid anti GI swelling agent+PPI Oral', - 'HTN Market', - 'NIAD (Non-Insulin Anti-Diabetic) Market', - 'Respules Market', - 'Statin+XZK Market', - 'BUDESONIDE Market', - 'Rosuvastatin Market', - 'Atorvastatin Market', - 'Metoprolol Succinate Market', - 'PPI Oral ESOMEPRAZOLE Market', - 'PPI Oral OMEPRAZOLE Market', - --'Inhaled Extended Market by Brand', - 'Inhaled Extended Market', - 'Non-Omeprazole', - 'Metoprolol Tartrate Market', - 'Pediatric Cough Market', - 'Anti-HER2 Market', - 'Anti-HER2 mBC Market', - 'Fasenra Market', - 'Severe Asthma' - ) - then - '1' - when - data_source = 'DTP(Quarterly)' - and market in ( - 'EGFR TKI Market', - 'Anti-HER2 Market', - 'Anti-HER2 mBC Market', - 'Fasenra/Tezspire Related Market', - 'Severe Asthma', - 'Tezspire CRSwNP Market', - 'CRSwNP Market', - 'PAM Market' - ) - then - '1' - else '0' - end as PRD_FLAG - FROM - DM.dm_td_external_market_pack_mapping_temp - GROUP BY - MARKET, - DATA_SOURCE - --- COMMAND ---------- - -refresh table dm.DM_TD_EXTERNAL_MARKET - --- COMMAND ---------- - -insert overwrite dm.dm_td_external_market_ta(MARKET,DATA_SOURCE,TA,TA_RN,MARKET_DESC) -SELECT MARKET,DATA_SOURCE, AZ_RELATED TA, TA_RN,MARKET_DESC -FROM DM.DM_TD_EXTERNAL_MARKET -WHERE MARKET NOT LIKE '%ALL Market%' and MARKET <>'Retail All Market' -UNION all -SELECT MARKET,DATA_SOURCE, 'CVRM' TA,0 TA_RN,MARKET_DESC -FROM DM.DM_TD_EXTERNAL_MARKET -WHERE MARKET = 'CVRM Market' - --- COMMAND ---------- - ------------------------------------------------------------------------------------ ---修改时间:20240822 ---修改人:FanXujia ---修改内容: ---Retail、EC渠道,新增NIAD这个TA。数据与DM相同 ------------------------------------------------------------------------------------ -insert into dm.dm_td_external_market_ta -( -MARKET, -DATA_SOURCE, -TA, -TA_RN, -MARKET_DESC -) -select -market, -DATA_SOURCE, -'NIAD' as TA, -7 as TA_RN, -MARKET_DESC -from dm.dm_td_external_market_ta -where DATA_SOURCE = 'Retail(Quarterly)' -and ta = 'DM' -union all -select -market, -DATA_SOURCE, -'NIAD' as TA, -7 as TA_RN, -MARKET_DESC -from dm.dm_td_external_market_ta -where DATA_SOURCE = 'EC(Monthly)' -and ta = 'DM' - ---20250701 chenwu 新增RARE = ONCO的Soliris Related Market -union all -select -market, -DATA_SOURCE, -'RARE' as TA, -8 as TA_RN, -MARKET_DESC -from dm.dm_td_external_market_ta -where DATA_SOURCE = 'Retail(Quarterly)' -and ta = 'ONCO' -and MARKET = 'Soliris Related Market' - --- COMMAND ---------- - --- log 20250226 auth:庄伟 --- 对market_ta表中市场进行补全,并依据手工文件维护的 market 与 ta 映射,匹配出完整市场对应的TA_NAME - - -insert overwrite table dm.dm_td_external_market_ta -( -MARKET, -DATA_SOURCE, -TA, -TA_MAP, -TA_RN, -MARKET_DESC -) - ------找出在market维度表中不存在的market列表 -with temp_ma_ext_market -as -( -select -distinct -MARKET -,DATA_SOURCE -from dm.dm_td_external_market -where MARKET NOT IN ( -select distinct MARKET -from dm.dm_td_external_market_ta -) -) - - -------补充完整的market列表,并通过market匹配手工文件维护的market对应的TA -select -distinct -dim_mkt_ta.MARKET, -DATA_SOURCE, -dim_mkt_ta.TA, -nvl(mkt_property.ta,mkt_property_key.TA) TA_MAP, -dim_mkt_ta.TA_RN, -dim_mkt_ta.MARKET_DESC -from -( -select MARKET,DATA_SOURCE,TA,TA_RN,MARKET_DESC -from dm.dm_td_external_market_ta -)dim_mkt_ta -left join dm.dm_ims_td_market_property mkt_property -on dim_mkt_ta.market = mkt_property.market -left join dm.dm_ims_td_market_property mkt_property_key -on dim_mkt_ta.MARKET = mkt_property_key.MARKET_KEY -union all -select - ext_market.MARKET, - ext_market.DATA_SOURCE, - null TA, - mkt_property.TA TA_MAP, - '99' as TA_RN, - null MARKET_DESC -from temp_ma_ext_market ext_market -left join dm.dm_ims_td_market_property mkt_property -on ext_market. market =mkt_property.MARKET -where nvl(mkt_property.TA,'')<>''; \ No newline at end of file