update20260427

This commit is contained in:
2026-04-27 11:04:09 +08:00
commit 3e14e78af3
173 changed files with 62579 additions and 0 deletions

View File

@@ -0,0 +1,178 @@
-- Databricks notebook source
-- create table dm.dm_ext_thc_sales
-- (
-- yyyymm STRING,
-- audit_cod STRING,
-- pack_cod STRING,
-- corp_cod STRING,
-- sales_value_cal decimal(38,10),
-- sales_unit_cal decimal(38,10),
-- counting_unit decimal(38,10),
-- sales_value_cal_ly decimal(38,10),
-- sales_unit_cal_ly decimal(38,10),
-- counting_unit_ly decimal(38,10)
-- )
-- using delta
-- COMMAND ----------
--------------------------------------------------------------------------
--修改时间20241106
--Fanxujia
--
--THC原本只有全国数据14
--
--PBI单独展示
--where条件筛选
---------------------------------------------------------------------------
--rawdata年月处理
with thc_rawdata1 as (
select case when right(yq,2) = 'Q1' then concat(left(yq,4),'03')
when right(yq,2) = 'Q2' then concat(left(yq,4),'06')
when right(yq,2) = 'Q3' then concat(left(yq,4),'09')
when right(yq,2) = 'Q4' then concat(left(yq,4),'12')
else yq
end as yyyymm,
city_c,
pack_cod,
prod_cod,
value,
totalunit,
countingunit
from dwd.dwd_gnd_ext_thc_rawdata
)
--CHPA渠道对应的公司编码以及名称
,chpa_corp as (
select distinct
PACK_COD,
CORP_COD,
CORP_DES
from dm.dm_ims_td_pack_property
)
--thc渠道的pack_property里取到对应的iqvia_pack_codecountingunit
--corp_cod优先取CHPA渠道的
,thc_corp as (
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 dwd.dwd_gnd_ext_thc_pack_property t1
left join chpa_corp t2
on t1.iqvia_pack_code = t2.PACK_COD
)
--thc渠道的audit_cod
--CHT
--使audit_cod
,thc_geo as (
select
t1.city_c,
t1.city_e,
t1.province_c,
t1.province_e,
case when t1.province_c = '全国' then 'CHT'
when t2.geo_key is null then t1.province_e
else t2.geo_key
end as AUDIT_COD
from dwd.dwd_gnd_ext_thc_geo t1
left join dm.dm_td_geography t2
--THC最细到省
on t1.province_c = t2.province_name
and t2.geo_key = t2.province_code
)
--rawdatacorpaudit信息合到一起
,thc_rawdata2 as (
select t1.yyyymm,
t1.city_c,
t1.pack_cod as pack_cod_old,
t2.iqvia_pack_code as pack_cod,
t2.CORP_COD,
t3.AUDIT_COD,
t1.prod_cod,
t1.value,
t1.totalunit,
t1.totalunit * t2.countingunit as countingunit
from thc_rawdata1 t1
left join thc_corp t2
on t1.pack_cod = t2.pack_cod
left join thc_geo t3
on t1.city_c = t3.city_c
)
--ly时会需要
,max_thc_rawdata as (
select max(cast(yyyymm as int)) as max_ym
from thc_rawdata2
)
--ly
,thc_rawdata3 as (
select yyyymm,
AUDIT_COD,
pack_cod,
CORP_COD,
value,
totalunit,
countingunit,
0 as value_ly,
0 as totalunit_ly,
0 as countingunit_ly
from thc_rawdata2
union all
select cast(t1.yyyymm as int) + 100 as yyyymm,
t1.AUDIT_COD,
t1.pack_cod,
t1.CORP_COD,
0 as value,
0 as totalunit,
0 as countingunit,
t1.value as value_ly,
t1.totalunit as totalunit_ly,
t1.countingunit as countingunit_ly
from thc_rawdata2 t1
cross join max_thc_rawdata t2
where cast(t1.yyyymm as int) + 100 <= t2.max_ym
)
--audit_codpack_codCorp_cod汇总
,thc_rawdata4 as (
select yyyymm,
AUDIT_COD,
pack_cod,
CORP_COD,
sum(value) as SALES_VALUE_CAL,
sum(totalunit) as SALES_UNIT_CAL,
sum(countingunit) as COUNTING_UNIT,
sum(value_ly) as SALES_VALUE_CAL_LY,
sum(totalunit_ly) as SALES_UNIT_CAL_LY,
sum(countingunit_ly) as COUNTING_UNIT_LY
from thc_rawdata3
group by yyyymm,
AUDIT_COD,
pack_cod,
CORP_COD
)
insert overwrite table dm.dm_ext_thc_sales
(
yyyymm,
AUDIT_COD,
pack_cod,
CORP_COD,
SALES_VALUE_CAL,
SALES_UNIT_CAL,
COUNTING_UNIT,
SALES_VALUE_CAL_LY,
SALES_UNIT_CAL_LY,
COUNTING_UNIT_LY
)
select yyyymm,
AUDIT_COD,
pack_cod,
CORP_COD,
SALES_VALUE_CAL,
SALES_UNIT_CAL,
COUNTING_UNIT,
SALES_VALUE_CAL_LY,
SALES_UNIT_CAL_LY,
COUNTING_UNIT_LY
from thc_rawdata4
--2018
where yyyymm >= '201801'