# Databricks notebook source # MAGIC %sql # MAGIC -- CREATE or REPLACE TABLE tmp.tmp_retail_dtp_final_sales ( # MAGIC -- year STRING, # MAGIC -- yq STRING, # MAGIC -- yyyymm STRING, # MAGIC -- iqvia_pack_code STRING, # MAGIC -- geo_key STRING, # MAGIC -- count_unit DOUBLE, # MAGIC -- average_price DOUBLE, # MAGIC -- sales_value DECIMAL(20,10), # MAGIC -- sales_unit DECIMAL(20,10), # MAGIC -- counting_units_obversion DECIMAL(20,10), # MAGIC -- counting_unit DECIMAL(20,10), # MAGIC -- sales_value_ly DECIMAL(20,10), # MAGIC -- sales_unit_ly DECIMAL(20,10), # MAGIC -- counting_unit_ly DECIMAL(20,10), # MAGIC -- pack_flag INT, # MAGIC -- brand_flag INT) # MAGIC -- USING delta # MAGIC -- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/TMP/tmp_retail_dtp_final_sales'; # COMMAND ---------- ############################################################START############################################################## ### STEP-1: insert splited pack data into tmp final table: tmp_retail_final_dtp_sales ### STEP-2: calculate OTHERS data # COMMAND ---------- # MAGIC %md # MAGIC ## STEP-1: insert splited pack data # COMMAND ---------- # MAGIC %sql # MAGIC ------------------------------------------------------------------------------------- # MAGIC -- STEP-1: insert splited pack data into tmp final table # MAGIC -- insert into tmp_retail_dtp_final_sales (dws.dws_retail_dtp_sales) # MAGIC ------------------------------------------------------------------------------------- # MAGIC with tmp_has_roc as ( # MAGIC select # MAGIC pack_code, # MAGIC time # MAGIC from tmp.tmp_retail_dtp_pack_rawdata # MAGIC where region = 'ROC' # MAGIC ), temp_dtp_pack_property as ( # MAGIC select iqvia_pack_code,max(counting_unit) as counting_unit,max(unit) as unit # MAGIC from dwd.dwd_gnd_ext_dtp_pack_property # MAGIC group by iqvia_pack_code # MAGIC ), tmp_pack as ( # MAGIC select # MAGIC a.year, # MAGIC a.time, # MAGIC a.pack_code, # MAGIC a.region, # MAGIC a.counting_unit, # MAGIC a.average_price, # MAGIC a.sales_amount, # MAGIC a.sales_volume, # MAGIC a.counting_units_obversion, # MAGIC ------------------------------------------------------ # MAGIC -- counting_unit取值逻辑: # MAGIC -- 不能直接取原始pack文件表中的值,改为取pack_property表中counting_unit / unit的值 # MAGIC a.sales_volume * (b.counting_unit/ coalesce(b.unit,1)) as counting_unit_property, # MAGIC ------------------------------------------------------ # MAGIC a.data_flag, # MAGIC a.brand_flag, # MAGIC -- a.prescription_nature, # MAGIC -- a.medicine_attribute, # MAGIC -- a.dosage_form, # MAGIC -- a.object, # MAGIC -- a.zk_classify1, # MAGIC -- a.zk_classify2, # MAGIC -- a.zk_classify3, # MAGIC a.target_points # MAGIC -- a.common_name, # MAGIC -- a.brand_name, # MAGIC -- a.product_name, # MAGIC -- a.pack_des, # MAGIC -- a.factory, # MAGIC -- a.corp_des # MAGIC from tmp.tmp_retail_dtp_pack_rawdata a # MAGIC left join temp_dtp_pack_property b # MAGIC ---------------------------------------------------- # MAGIC -- format iqvia_pack_code from dwd_gnd_ext_dtp_pack_property # MAGIC -- on a.pack_code = # MAGIC -- case when length(trim(b.iqvia_pack_code)) < 12 and trim(b.iqvia_pack_code) REGEXP '^[0-9]' # MAGIC -- then right(concat('000000000000',trim(b.iqvia_pack_code)),12) # MAGIC -- else trim(b.iqvia_pack_code) # MAGIC -- end # MAGIC -- ---------------------------------------------------- # MAGIC -- and nvl(a.prescription_nature,'') = nvl(b.prescription_nature,'') # MAGIC -- and nvl(a.medicine_attribute,'') = nvl(b.medicine_attribute,'') # MAGIC -- and nvl(a.dosage_form,'') = nvl(b.dosage_form,'') # MAGIC -- and nvl(a.object,'') = nvl(b.object,'') # MAGIC -- and nvl(a.zk_classify1,'') = nvl(b.zk_classify1,'') # MAGIC -- and nvl(a.zk_classify2,'') = nvl(b.zk_classify2,'') # MAGIC -- and nvl(a.zk_classify3,'') = nvl(b.zk_classify3,'') # MAGIC -- and nvl(a.target_points,'') = nvl(b.target_points,'') # MAGIC -- and nvl(a.common_name,'') = nvl(b.common_name,'') # MAGIC -- and nvl(a.brand_name,'') = nvl(b.brand_name,'') # MAGIC -- and nvl(a.product_name,'') = nvl(b.product_name,'') # MAGIC -- and nvl(a.pack_des,'') = nvl(b.zk_pack_des,'') # MAGIC -- and nvl(a.factory,'') = nvl(b.factory,'') # MAGIC -- and nvl(a.corp_des,'') = nvl(b.zk_corp_des,'') # MAGIC -- 20260226 不再使用上面的join条件,改为product_id关联 # MAGIC on a.iqvia_pack_code = b.iqvia_pack_code # MAGIC ),tmp_pack_this_year_with_roc ( # MAGIC select # MAGIC a.year, # MAGIC concat(a.year, 'Q', CEIL(CAST(RIGHT(a.time,2) AS INT)/3)) as yq, # MAGIC a.time as yyyymm, # MAGIC a.pack_code as iqvia_pack_code, # MAGIC a.region as geo_key, # MAGIC a.counting_unit as count_unit, # MAGIC a.average_price, # MAGIC a.sales_amount as sales_value, # MAGIC a.sales_volume as sales_unit, # MAGIC a.counting_units_obversion, # MAGIC a.counting_unit_property as counting_unit, # MAGIC case when a.data_flag = 0 then 1 else 2 end as pack_flag, # MAGIC case when a.brand_flag = 1 then 1 else 2 end as brand_flag, # MAGIC -- a.prescription_nature, # MAGIC -- a.medicine_attribute, # MAGIC -- a.dosage_form, # MAGIC -- a.object, # MAGIC -- a.zk_classify1, # MAGIC -- a.zk_classify2, # MAGIC -- a.zk_classify3, # MAGIC a.target_points # MAGIC -- a.common_name, # MAGIC -- a.brand_name, # MAGIC -- a.product_name, # MAGIC -- a.pack_des, # MAGIC -- a.factory, # MAGIC -- a.corp_des # MAGIC from tmp_pack a # MAGIC where exists ( # MAGIC select * from tmp_has_roc c # MAGIC where c.pack_code = a.pack_code # MAGIC and c.time = a.time # MAGIC ) and a.region <> '全国' # MAGIC ), tmp_pack_next_year_with_roc ( # MAGIC select # MAGIC cast(a.year + 1 as int) as year, # MAGIC concat(cast(a.year + 1 as int) , 'Q', CEIL(CAST(RIGHT(a.time,2) AS INT)/3)) as yq, # MAGIC cast(a.time + 100 as int) as yyyymm, # MAGIC a.pack_code as iqvia_pack_code, # MAGIC a.region as geo_key, # MAGIC a.counting_unit as count_unit, # MAGIC a.average_price, # MAGIC a.sales_amount as sales_value_ly, # MAGIC a.sales_volume as sales_unit_ly, # MAGIC a.counting_unit_property as counting_unit_ly, # MAGIC a.counting_units_obversion, # MAGIC case when a.data_flag = 0 then 1 else 2 end as pack_flag, # MAGIC case when a.brand_flag = 1 then 1 else 2 end as brand_flag, # MAGIC -- a.prescription_nature, # MAGIC -- a.medicine_attribute, # MAGIC -- a.dosage_form, # MAGIC -- a.object, # MAGIC -- a.zk_classify1, # MAGIC -- a.zk_classify2, # MAGIC -- a.zk_classify3, # MAGIC a.target_points # MAGIC -- a.common_name, # MAGIC -- a.brand_name, # MAGIC -- a.product_name, # MAGIC -- a.pack_des, # MAGIC -- a.factory, # MAGIC -- a.corp_des # MAGIC from tmp_pack a # MAGIC where a.time + 100 <= (select max(time) from tmp_pack) # MAGIC and exists ( # MAGIC select * from tmp_has_roc c # MAGIC where c.pack_code = a.pack_code # MAGIC and c.time = a.time # MAGIC ) and a.region <> '全国' # MAGIC ), tmp_pack_with_roc as ( # MAGIC select # MAGIC ifnull(a.year, b.year) as year, # MAGIC ifnull(a.yq, b.yq) as yq , # 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.geo_key, b.geo_key) as geo_key, # MAGIC ifnull(a.count_unit, b.count_unit) as count_unit, # MAGIC ifnull(a.average_price, b.average_price) as average_price, # 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.counting_units_obversion, b.counting_units_obversion) as counting_units_obversion, # 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.geo_key = b.geo_key # MAGIC -- and nvl(a.prescription_nature,'') = nvl(b.prescription_nature,'') # MAGIC -- and nvl(a.medicine_attribute,'') = nvl(b.medicine_attribute,'') # MAGIC -- and nvl(a.dosage_form,'') = nvl(b.dosage_form,'') # MAGIC -- and nvl(a.object,'') = nvl(b.object,'') # MAGIC -- and nvl(a.zk_classify1,'') = nvl(b.zk_classify1,'') # MAGIC -- and nvl(a.zk_classify2,'') = nvl(b.zk_classify2,'') # MAGIC -- and nvl(a.zk_classify3,'') = nvl(b.zk_classify3,'') # MAGIC and nvl(a.target_points,'') = nvl(b.target_points,'') # MAGIC -- and nvl(a.common_name,'') = nvl(b.common_name,'') # MAGIC -- and nvl(a.brand_name,'') = nvl(b.brand_name,'') # MAGIC -- and nvl(a.product_name,'') = nvl(b.product_name,'') # MAGIC -- and nvl(a.pack_des,'') = nvl(b.pack_des,'') # MAGIC -- and nvl(a.factory,'') = nvl(b.factory,'') # MAGIC -- and nvl(a.corp_des,'') = nvl(b.corp_des,'') # MAGIC ), tmp_pack_this_year_without_roc ( # MAGIC select # MAGIC left(a.time, 4) year, # MAGIC concat(a.year, 'Q', CEIL(CAST(RIGHT(a.time,2) AS INT)/3)) as yq, # MAGIC a.time as yyyymm, # MAGIC a.pack_code as iqvia_pack_code, # MAGIC 'ROC' as geo_key, # MAGIC a.counting_unit as count_unit, # MAGIC a.average_price, # MAGIC a.sales_amount as sales_value, # MAGIC a.sales_volume as sales_unit, # MAGIC a.counting_units_obversion, # MAGIC a.counting_unit_property as counting_unit, # 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 -- a.prescription_nature, # MAGIC -- a.medicine_attribute, # MAGIC -- a.dosage_form, # MAGIC -- a.object, # MAGIC -- a.zk_classify1, # MAGIC -- a.zk_classify2, # MAGIC -- a.zk_classify3, # MAGIC a.target_points # MAGIC -- a.common_name, # MAGIC -- a.brand_name, # MAGIC -- a.product_name, # MAGIC -- a.pack_des, # MAGIC -- a.factory, # MAGIC -- a.corp_des # MAGIC from tmp_pack a # MAGIC where not exists ( # MAGIC select * from tmp_has_roc c # MAGIC where c.pack_code = a.pack_code # MAGIC and c.time = a.time # MAGIC ) # MAGIC ), tmp_pack_next_year_without_roc as ( # MAGIC select # MAGIC cast(a.year + 1 as int) as year, # MAGIC concat(cast(a.year + 1 as int) , 'Q', CEIL(CAST(RIGHT(a.time,2) AS INT)/3)) as yq, # MAGIC cast(a.time + 100 as int) as yyyymm, # MAGIC a.pack_code as iqvia_pack_code, # MAGIC 'ROC' as geo_key, # MAGIC a.counting_unit as count_unit, # MAGIC a.average_price, # MAGIC a.sales_amount as sales_value_ly, # MAGIC a.sales_volume as sales_unit_ly, # MAGIC a.counting_unit_property as counting_unit_ly, # MAGIC a.counting_units_obversion, # 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 -- a.prescription_nature, # MAGIC -- a.medicine_attribute, # MAGIC -- a.dosage_form, # MAGIC -- a.object, # MAGIC -- a.zk_classify1, # MAGIC -- a.zk_classify2, # MAGIC -- a.zk_classify3, # MAGIC a.target_points # MAGIC -- a.common_name, # MAGIC -- a.brand_name, # MAGIC -- a.product_name, # MAGIC -- a.pack_des, # MAGIC -- a.factory, # MAGIC -- a.corp_des # MAGIC from tmp_pack a # MAGIC where a.time + 100 <= (select max(time) from tmp_pack) # MAGIC and not exists ( # MAGIC select * from tmp_has_roc c # MAGIC where c.pack_code = a.pack_code # MAGIC and c.time = a.time # MAGIC ) # MAGIC ),tmp_pack_without_roc as ( # MAGIC select # MAGIC ifnull(a.year, b.year) as year, # MAGIC ifnull(a.yq, b.yq) as yq , # 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.geo_key, b.geo_key) as geo_key, # MAGIC ifnull(a.count_unit, b.count_unit) as count_unit, # MAGIC ifnull(a.average_price, b.average_price) as average_price, # 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.counting_units_obversion, b.counting_units_obversion) as counting_units_obversion, # 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_without_roc a # MAGIC full outer join tmp_pack_next_year_without_roc b # MAGIC on a.yyyymm = b.yyyymm # MAGIC and a.iqvia_pack_code = b.iqvia_pack_code # MAGIC and a.geo_key = b.geo_key # MAGIC -- and nvl(a.prescription_nature,'') = nvl(b.prescription_nature,'') # MAGIC -- and nvl(a.medicine_attribute,'') = nvl(b.medicine_attribute,'') # MAGIC -- and nvl(a.dosage_form,'') = nvl(b.dosage_form,'') # MAGIC -- and nvl(a.object,'') = nvl(b.object,'') # MAGIC -- and nvl(a.zk_classify1,'') = nvl(b.zk_classify1,'') # MAGIC -- and nvl(a.zk_classify2,'') = nvl(b.zk_classify2,'') # MAGIC -- and nvl(a.zk_classify3,'') = nvl(b.zk_classify3,'') # MAGIC and nvl(a.target_points,'') = nvl(b.target_points,'') # MAGIC -- and nvl(a.common_name,'') = nvl(b.common_name,'') # MAGIC -- and nvl(a.brand_name,'') = nvl(b.brand_name,'') # MAGIC -- and nvl(a.product_name,'') = nvl(b.product_name,'') # MAGIC -- and nvl(a.pack_des,'') = nvl(b.pack_des,'') # MAGIC -- and nvl(a.factory,'') = nvl(b.factory,'') # MAGIC -- and nvl(a.corp_des,'') = nvl(b.corp_des,'') # MAGIC ), tmp_final_sales as ( # MAGIC select * from tmp_pack_with_roc # MAGIC union all # MAGIC select * from tmp_pack_without_roc # MAGIC ) # MAGIC # MAGIC insert overwrite table tmp.tmp_retail_dtp_final_sales # MAGIC # MAGIC select # MAGIC year, # MAGIC yq, # MAGIC yyyymm, # MAGIC iqvia_pack_code, # MAGIC geo_key, # MAGIC count_unit, # MAGIC average_price, # MAGIC sales_value, # MAGIC sales_unit, # MAGIC counting_units_obversion, # MAGIC counting_unit, # MAGIC sales_value_ly, # MAGIC sales_unit_ly, # MAGIC counting_unit_ly, # MAGIC pack_flag, # MAGIC brand_flag # MAGIC from tmp_final_sales # MAGIC order by yyyymm # MAGIC # COMMAND ---------- # MAGIC %md # MAGIC ## STEP-2: calculate OTHERS data # COMMAND ---------- # DBTITLE 1,不再计算 # %sql # ------------------------------------------------------------------------------------- # -- STEP-2: calculate OTHERS data # -- 2.1 calaulate DTP_AZ_OTHERS data # ------------------------------------------------------------------------------------- # with tmp_az_total_now as ( # 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' # ), tmp_az_total_ly as ( # select # cast(yyyymm + 100 as int) as yyyymm, # corp_name, # sales_amount as sales_amount_ly, # CORP_COD # from tmp_az_total_now # ), tmp_az_total as ( # select # a.*, # ifnull(b.sales_amount_ly, 0) as sales_amount_ly # from tmp_az_total_now a # left join tmp_az_total_ly b # on a.yyyymm = b.yyyymm # and a.corp_name = b.corp_name # and a.CORP_COD = b.CORP_COD # ),tmp_az_pack_total as ( # select # a.year, # a.yq, # a.yyyymm, # sum(a.sales_value) as sales_value, # sum(a.sales_unit) as sales_unit, # sum(a.counting_unit) as counting_unit, # sum(a.sales_value_ly) as sales_value_ly, # sum(a.sales_unit_ly) as sales_unit_ly, # sum(a.counting_unit_ly) as counting_unit_ly # from tmp.tmp_retail_dtp_final_sales a # where a.iqvia_pack_code in ( # select distinct iqvia_pack_code # from tmp.tmp_zk_retail_dtp_market_corp # where corp_cod = 'A5Z' # ) # group by a.year,a.yq,a.yyyymm # order by a.yyyymm # ) # insert into table tmp.tmp_retail_dtp_final_sales # select # left(a.yyyymm, 4) as year, # concat(left(a.yyyymm, 4), 'Q', CEIL(CAST(RIGHT(a.yyyymm,2) AS INT)/3)) as yq, # a.yyyymm, # 'DTP_AZ_OTHERS' as iqvia_pack_code, # 'ROC' as geo_key, # 0 as count_unit, # 0 as average_price, # a.sales_amount - nvl(b.sales_value, 0) as sales_value, # 0 as sales_unit, # 0 as counting_units_obversion, # 0 as counting_unit, # a.sales_amount_ly - nvl(b.sales_value_ly, 0) as sales_value_ly, # 0 as sales_unit_ly, # 0 as counting_unit_ly, # 0 as pack_flag, # 0 as brand_flag # from tmp_az_total a # left join tmp_az_pack_total b # on a.yyyymm = b.yyyymm # order by a.yyyymm # COMMAND ---------- # DBTITLE 1,不再计算 # %sql # ------------------------------------------------------------------------------------- # -- STEP-2: calculate OTHERS data # -- 2.2 calaulate DTP_OTHERS data # ------------------------------------------------------------------------------------- # with tmp_total_now as ( # SELECT # dtp_name, # sales_quarter as yyyymm, # sales_amount * 1000000.0 as sales_amount # FROM dwd.dwd_gnd_ext_retail_dtp_overall_market # ), tmp_total_ly as ( # select # dtp_name, # cast(yyyymm + 100 as int) as yyyymm, # sales_amount as sales_amount_ly # from tmp_total_now # ), tmp_total as ( # select # a.*, # ifnull(b.sales_amount_ly, 0) as sales_amount_ly # from tmp_total_now a # left join tmp_total_ly b # on a.yyyymm = b.yyyymm # ),tmp_pack_total as ( # select # a.year, # a.yq, # a.yyyymm, # sum(a.sales_value) as sales_value, # sum(a.sales_unit) as sales_unit, # sum(a.counting_unit) as counting_unit, # sum(a.sales_value_ly) as sales_value_ly, # sum(a.sales_unit_ly) as sales_unit_ly, # sum(a.counting_unit_ly) as counting_unit_ly # from tmp.tmp_retail_dtp_final_sales a # group by a.year,a.yq,a.yyyymm # order by a.yyyymm # ) # insert into table tmp.tmp_retail_dtp_final_sales # select # left(a.yyyymm, 4) as year, # concat(left(a.yyyymm, 4), 'Q', CEIL(CAST(RIGHT(a.yyyymm,2) AS INT)/3)) as yq, # a.yyyymm, # 'DTP_OTHERS' as iqvia_pack_code, # 'ROC' as geo_key, # 0 as count_unit, # 0 as average_price, # a.sales_amount - nvl(b.sales_value, 0) as sales_value, # 0 as sales_unit, # 0 as counting_units_obversion, # 0 as counting_unit, # a.sales_amount_ly - nvl(b.sales_value_ly, 0) as sales_value_ly, # 0 as sales_unit_ly, # 0 as counting_unit_ly, # 0 as pack_flag, # 0 as brand_flag # from tmp_total a # left join tmp_pack_total b # on a.yyyymm = b.yyyymm # order by a.yyyymm # COMMAND ---------- ############################################################END################################################################