删除备份版本notebook
This commit is contained in:
@@ -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################################################################
|
||||
@@ -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
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user