209 lines
8.9 KiB
Python
209 lines
8.9 KiB
Python
# 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################################################################ |