Files
MarketAnalysis-ETL/CHPA/03 dm_ims_tf_sales.sql
chenwu 832c7bcd69 new file: 01 dm_tf_external_sales.sql
new file:   02 dm_td_external_keycompatitor.sql
	new file:   03 dm_td_external_brand_market.sql
	new file:   04 dm_td_external_calendar.sql
	new file:   05 dm_td_external_exchangerate.sql
	new file:   07 dm_td_external_packinfo.sql
	new file:   08 dm_td_external_corp.sql
	new file:   09 dm_td_external_geo_type.sql
	new file:   11 DM_TD_EXTERNAL_MARKET_NEW.sql
	new file:   12 dm_td_external_org.sql
	new file:   13 external auth.sql
	new file:   14 dm_tf_external_retail_special_bkp.sql
	new file:   AIA/01 dm_aia_pack_property.sql
	new file:   AIA/02 dm_ext_aia_sales.sql
	new file:   AIA/02 dm_ext_aia_sales_bakup_20230327.sql
	new file:   AIA/03 dm_aia_flag.sql
	new file:   AIA/04 dm_aia_provided_flag.sql
	new file:   AIA/06 DM_TD_EXT_AIA_PACK_PROPERTY.sql
	new file:   AIA/07 DM_TD_EXT_AIA_PACK2MARKET.sql
	new file:   AIA/08 DM_TD_EXT_AIA_MARKET_RATIO.sql
	new file:   AIA/09 DM_TD_EXT_AIA_MARKET_BRAND_RATIO.sql
	new file:   AIA/10 DM_TD_EXT_AIA_MARKET_PACK_MAPPING.sql
	new file:   AIA/11 DM_TD_EXT_AIA_TARGET_INST.sql
	new file:   AIA/11 dm_aia_targethp_flag.sql
	new file:   "AIA/z_03 dm_aia_flag_\345\244\207\344\273\275\347\224\250.sql"
	new file:   CHC/01 dm_chc_pack_property.sql
	new file:   CHC/02 DM_TF_EXT_CHC_SALES.sql
	new file:   CHC/03 DM_TD_EXT_CHC_PACK_PROPERTY.sql
	new file:   CHC/04 DM_TD_EXT_CHC_PACK2MARKET.sql
	new file:   CHC/05 DM_TD_EXT_CHC_MARKET_RATIO.sql
	new file:   CHC/06 DM_TD_EXT_CHC_MARKET_BRAND_RATIO.sql
	new file:   CHC/07 DM_TD_EXT_CHC_MARKET_PACK_MAPPING.sql
	new file:   CHPA/01 dwd_ims_atc_hierarchy.sql
	new file:   CHPA/01 dwd_ims_nfc_hierarchy.sql
	new file:   CHPA/01 dwd_ims_td_manufacturer_corp.sql
	new file:   CHPA/01 dwd_ims_td_pack_property.sql
	new file:   CHPA/01 dwd_update.sql
	new file:   CHPA/01_FB_BLOB_TO_DWD.sql
	new file:   CHPA/02 DWS_IMS_TD_GEO.sql
	new file:   CHPA/02 dws_ims_td_atc_cn.sql
	new file:   CHPA/02 dws_ims_td_corp_cn.sql
	new file:   CHPA/02 dws_ims_td_date.sql
	new file:   CHPA/02 dws_ims_td_manu_cn.sql
	new file:   CHPA/02 dws_ims_td_market.sql
	new file:   CHPA/02 dws_ims_td_market_ta.sql
	new file:   CHPA/02 dws_ims_td_nfc_cn.sql
	new file:   CHPA/02 dws_ims_td_prod_cn.sql
	new file:   CHPA/02 tmp_ims_td_prod_tmp.sql
	new file:   CHPA/02 tmp_ims_tf_fact_sales.sql
	new file:   CHPA/03 DM_TD_EXT_CHPA_MARKET_BRAND_RATIO.sql
	new file:   CHPA/03 DM_TD_EXT_CHPA_MARKET_PACK_MAPPING.sql
	new file:   CHPA/03 DM_TD_EXT_CHPA_MARKET_RATIO.sql
	new file:   CHPA/03 DM_TD_EXT_CHPA_PACK2MARKET.sql
	new file:   CHPA/03 DM_TD_EXT_CHPA_PACK_PROPERTY.sql
	new file:   CHPA/03 DM_TF_EXT_CHPA_SALES.sql
	new file:   CHPA/03 dm_ims_td_calendar.sql
	new file:   CHPA/03 dm_ims_td_geo.sql
	new file:   CHPA/03 dm_ims_td_market_property.sql
	new file:   CHPA/03 dm_ims_td_org.sql
	new file:   CHPA/03 dm_ims_td_org_hvh.sql
	new file:   CHPA/03 dm_ims_td_pack_property.sql
	new file:   CHPA/03 dm_ims_tf_sales.sql
	new file:   CHPA/03 dm_td_chpa_market_definition.sql
	new file:   CHPA/03 dm_td_ims_city_mapping.sql
	new file:   EC/03 ec_load_data.sql
	new file:   EC/04 DM_TD_EXT_EC_PACK_PROPERTY.sql
	new file:   EC/05 DM_TF_EXT_EC_SALES.sql
	new file:   EC/06 DM_TD_EXT_EC_PACK2MARKET.sql
	new file:   EC/07 DM_TD_EXT_EC_MARKET_RATIO.sql
	new file:   EC/08 DM_TD_EXT_EC_MARKET_BRAND_RATIO.sql
	new file:   EC/09 DM_TD_EXT_EC_MARKET_PACK_MAPPING.sql
	new file:   EC/1 (ec)blob_to_dwd.sql
	new file:   EC/2 dwd_inc_gnd_ext_ec_nationnal_pack_union_all.py
	new file:   Merged_Data/Merged_Data_Config_table_bkp.sql
	new file:   Merged_Data/Merged_Data_Config_table_bymonth.sql
	new file:   Merged_Data/dm_tf_exteranl_sales_merged_data_dtp_others_bkp.sql
	new file:   Merged_Data/dm_tf_exteranl_sales_merged_data_dtp_others_bymonth_bkp.sql
	new file:   ORG/DM_TD_EXT_AIA_ORG.sql
	new file:   ORG/DM_TD_EXT_CHC_ORG.sql
	new file:   ORG/DM_TD_EXT_CHPA_ORG.sql
	new file:   ORG/DM_TD_EXT_COUNTY_ORG.sql
	new file:   ORG/DM_TD_EXT_EC_ORG.sql
	new file:   ORG/DM_TD_EXT_RETAIL_ORG.sql
	new file:   ORG/DM_TD_EXT_THC_ORG.sql
	new file:   ORG/DM_TD_EXT_XIEHE_ORG.sql
	new file:   OTHERS/01 dm_td_report_url.sql
	new file:   OTHERS/02 dws_ext_email_warning.sql
	new file:   OTHERS/external_triggered_email.py
	new file:   Retail/01 load_tmp_data.py
	new file:   Retail/02 split_brand_data.py
	new file:   Retail/03 split_pack_data.py
	new file:   Retail/04 map_to_dws_table.py
	new file:   Retail/05 load_dtp_temp_data.py
	new file:   Retail/06 split_dtp_brand_data.py
	new file:   Retail/07 split_dtp_pack_data.py
	new file:   Retail/08 map_to_dtp_dws_table.py
	new file:   Retail/09 dwd_inc_gnd_ext_retail_nataional.py
	new file:   Retail/10 map_to_retail_dm_table.py
	new file:   Retail/11 map_to_overview_dm_table.py
	new file:   Retail/12 dws_tf_external_retail_dtp_special.sql
	new file:   Retail/13 DM_TF_EXT_RETAIL_SALES.sql
	new file:   Retail/14 DM_TF_EXT_RETAIL_DTP_SALES.sql
	new file:   Retail/15 DM_TD_EXT_RETAIL_PACK_PROPERTY.sql
	new file:   Retail/16 DM_TD_EXT_RETAIL_DTP_PACK_PROPERTY.sql
	new file:   Retail/17 DM_TD_EXT_DTP_PACK2MARKET.sql
	new file:   Retail/17 DM_TD_EXT_RETAIL_PACK2MARKET.sql
	new file:   Retail/18 DM_TD_EXT_DTP_MARKET_RATIO.sql
	new file:   Retail/18 DM_TD_EXT_RETAIL_MARKET_RATIO.sql
	new file:   Retail/19 DM_TD_EXT_DTP_MARKET_BRAND_RATIO.sql
	new file:   Retail/19 DM_TD_EXT_RETAIL_MARKET_BRAND_RATIO.sql
	new file:   Retail/20 DM_TD_EXT_DTP_MARKET_PACK_MAPPING.sql
	new file:   Retail/20 DM_TD_EXT_RETAIL_MARKET_PACK_MAPPING.sql
	new file:   "Retail/z1 dwd_inc_gnd_ext_retail_nataional_\344\275\234\345\272\237.py"
	new file:   "Retail/z2 retail_load_data_\344\275\234\345\272\237.sql"
	new file:   "Retail/z3 retail_overview_data_\344\275\234\345\272\237.sql"
	new file:   THC/01 dm_thc_pack_property.sql
	new file:   THC/02 dm_ext_thc_sales.sql
	new file:   THC/02 dm_ext_thc_sales_bakup_20260327.sql
	new file:   THC/03 DM_TF_EXT_THC_MARKET_SALES_CHT.sql
	new file:   THC/04 dm_tf_external_sales_thc.sql
	new file:   THC/05 DM_TD_EXT_THC_PACK_PROPERTY.sql
	new file:   THC/06 DM_TD_EXT_THC_PACK2MARKET.sql
	new file:   THC/07 DM_TD_EXT_THC_MARKET_RATIO.sql
	new file:   THC/08 DM_TD_EXT_THC_MARKET_BRAND_RATIO.sql
	new file:   THC/09 DM_TD_EXT_THC_MARKET_PACK_MAPPING.sql
	new file:   UNIONALL/DM_TD_EXT_UNIONALL_MARKET_PACK_MAPPING.sql
	new file:   UNIONALL/DM_TD_EXT_UNIONALL_PACKINFO.sql
	new file:   UNIONALL/DM_TF_EXT_UNIONALL_MARKET_SALES.sql
	new file:   UNIONALL/DM_TF_EXT_UNIONALL_MARKET_SALES_v1.0.sql
	new file:   UNIONALL/DM_TF_EXT_UNIONALL_SALES.sql
	new file:   UNIONALL/DM_TF_EXT_UNIONALL_SALES_MAPPING.sql
	new file:   UNIONALL/DM_TF_EXT_UNIONALL_SALES_MAPPING_NIAD.sql
	new file:   XIEHE/01 xiehe_blob_to_dwd.py
	new file:   XIEHE/02 dm_xiehe_pack_property.sql
	new file:   XIEHE/03 dm_ext_xiehe_geo.sql
	new file:   XIEHE/04 dm_ext_xiehe_sales.sql
	new file:   XIEHE/05 dm_td_xiehe_core_dept.sql
	new file:   XIEHE/06 DM_TF_EXT_XIEHE_SALES.sql
	new file:   XIEHE/07 DM_TD_EXT_XIEHE_PACK_PROPERTY.sql
	new file:   XIEHE/08 DM_TD_EXT_XIEHE_PACK2MARKET.sql
	new file:   XIEHE/09 DM_TD_EXT_XIEHE_MARKET_RATIO.sql
	new file:   XIEHE/10 DM_TD_EXT_XIEHE_MARKET_BRAND_RATIO.sql
	new file:   XIEHE/11 DM_TD_EXT_XIEHE_MARKET_PACK_MAPPING.sql
	new file:   XIEHE/bkp_01 xiehe_blob2dwd.py
	new file:   XIEHE/bkp_02 dm_ext_xiehe_sales.sql
	new file:   XIEHE/bkp_03 dm_ext_xiehe_pack_property.sql
	new file:   county/01 tmp_ims_county_fact_sales_sum.sql
	new file:   county/02 tmp_imscounty_Result.sql
	new file:   county/03 dm_ims_td_county_geo.sql
	new file:   county/04 dws_ext_county_tf_sales.sql
	new file:   county/05 dm_ext_county_td_pack_property.sql
	new file:   county/06 dm_td_county_pack_region.sql
	new file:   county/07 dm_ext_county_tf_sales_region.sql
	new file:   county/08 DM_TD_EXT_COUNTY_PACK_PROPERTY.sql
	new file:   county/09 DM_TF_EXT_COUNTY_SALES.sql
	new file:   county/10 DM_TD_EXT_COUNTY_PACK2MARKET.sql
	new file:   county/11 DM_TD_EXT_COUNTY_MARKET_RATIO.sql
	new file:   county/12 DM_TD_EXT_COUNTY_MARKET_BRAND_RATIO.sql
	new file:   county/13 DM_TD_EXT_COUNTY_MARKET_PACK_MAPPING.sql
	new file:   for_AIA_Dashboard/01 dm_td_aia_inst_mkt.sql
	new file:   for_AIA_Dashboard/02 dm_td_aia_auth_sales.sql
	new file:   for_AIA_Dashboard/03 dm_td_aia_original_col.sql
	new file:   for_AIA_Dashboard/04 dm_td_aia_nosales_inst.sql
	new file:   for_AIA_Dashboard/05 dm_td_aia_is_eagle_flag.sql
	new file:   for_AIA_Dashboard/06 dm_td_aia_rank.sql
	new file:   for_AIA_Dashboard/07 dm_ext_aia_data_remove_flag.sql
	new file:   for_AIA_Dashboard/07 dm_td_aia_remove_special_ins_bkp.py
	new file:   for_AIA_Dashboard/08 dm_ext_aia_data_quality_flag.sql
	new file:   z 01 dm_tf_external_sales.sql
	new file:   "z 10 dm_td_external_market_pack_mapping_\344\275\234\345\272\237.sql"
	new file:   "z 11 dm_td_external_market_\344\275\234\345\272\237.sql"
	new file:   "\344\270\212\347\272\277\350\204\232\346\234\254.sql"
	new file:   "\346\225\260\346\215\256\351\252\214\350\257\201.sql"
2026-04-27 15:48:38 +08:00

759 lines
22 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Databricks notebook source
-- DBTITLE 1,建表SQL
-- CREATE OR REPLACE TABLE dm.dm_ims_tf_sales (
-- YYYYMM STRING,
-- DAY_DATE STRING,
-- PACK_COD STRING,
-- AUDIT_COD STRING,
-- Related_flag STRING,
-- ORG_KEY STRING,
-- Market STRING,
-- MARKET_PACK_KEY STRING,
-- MARKET_AUDIT_KEY STRING,
-- SALES_VALUE DECIMAL(30,10),
-- SALES_VALUE_LY DECIMAL(30,10),
-- SALES_VALUE_L2Y DECIMAL(30,10),
-- SALES_UNIT DECIMAL(30,10),
-- SALES_UNIT_LY DECIMAL(30,10),
-- SALES_UNIT_L2Y DECIMAL(30,10),
-- CONUTING_UNIT DECIMAL(30,10),
-- CONUTING_UNIT_LY DECIMAL(30,10),
-- CONUTING_UNIT_L2Y DECIMAL(30,10),
-- PDOT DECIMAL(30,10),
-- PDOT_LY DECIMAL(30,10),
-- PDOT_L2Y DECIMAL(30,10),
-- SALES_VALUE_CAL DECIMAL(30,10),
-- SALES_VALUE_CAL_LY DECIMAL(30,10),
-- SALES_UNIT_CAL DECIMAL(30,10),
-- SALES_UNIT_CAL_LY DECIMAL(30,10),
-- ETL_INSERT_DT TIMESTAMP,
-- ETL_UPDATE_DT TIMESTAMP)
-- USING delta
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_ims_tf_sales';
-- COMMAND ----------
-- DBTITLE 1,tmp_ims_tf_fact_sales_l2y
--提前算好前端需要两年前的数据
-- tmp.tmp_ims_tf_fact_sales 整张表对应的notebook path:/Workspace/Users/kthc245@azc.partner.onmschina.cn/20251125Rebuild MA/rebuildMA/DWS/dws_ims_tbl
CREATE
OR REPLACE TEMPORARY VIEW tmp_ims_tf_fact_sales_l2y AS
select
YM,
AUDIT_COD,
PACK_COD,
sum(MTH00LC) as MTH00LC,
sum(MTH00LCLY) as MTH00LCLY,
sum(MTH00LCL2Y) as MTH00LCL2Y,
sum(MTH00CN) as MTH00CN,
sum(MTH00CNLY) as MTH00CNLY,
sum(MTH00CNL2Y) as MTH00CNL2Y,
sum(MTH00UN) as MTH00UN,
sum(MTH00UNLY) as MTH00UNLY,
sum(MTH00UNL2Y) as MTH00UNL2Y
from
(
select
YM,
AUDIT_COD,
PACK_COD,
MTH00LC,
MTH00LCLY,
0 as MTH00LCL2Y,
MTH00CN,
MTH00CNLY,
0 as MTH00CNL2Y,
MTH00UN,
MTH00UNLY,
0 as MTH00UNL2Y
from
tmp.tmp_ims_tf_fact_sales
union all
select
YM + 200 as YM,
AUDIT_COD,
PACK_COD,
0 as MTH00LC,
0 as MTH00LCLY,
MTH00LC as MTH00LCL2Y,
0 as MTH00CN,
0 as MTH00CNLY,
MTH00CN as MTH00CNL2Y,
0 as MTH00UN,
0 as MTH00UNLY,
MTH00UN as MTH00UNL2Y
from
tmp.tmp_ims_tf_fact_sales
where
YM + 200 in (
select distinct
YM
from
tmp.tmp_ims_tf_fact_sales
)
)
group by
YM,
AUDIT_COD,
PACK_COD
-- COMMAND ----------
-- DBTITLE 1,tmp_ims_tf_sales_total
--提前算好需要IMS 折算的value和不折算的value
CREATE
OR REPLACE TEMPORARY VIEW tmp_ims_tf_sales_total AS
-- select distinct
-- t1.YM as YYYYMM,
-- t1.PACK_COD,
-- t1.AUDIT_COD,
-- 'ALL' AS RELATE_FLAG,
-- t1.AUDIT_COD as ORG_KEY,
-- case
-- when t2.market is null then 'Non Market'
-- else t2.market
-- end Market,
-- case
-- when t2.market is null then concat ('Non Market_', t1.pack_cod)
-- else concat (t2.market, '_', t2.pack_cod)
-- end as MARKET_PACK_KEY,
-- case
-- when t2.market is null then concat ('Non Market_', t1.audit_cod)
-- else concat (t2.market, '_', t1.audit_cod)
-- end as MARKET_AUDIT_KEY,
-- t1.MTH00LC * (
-- case
-- when t2.Value_Market_Ratio is null then 1
-- else t2.Value_Market_Ratio
-- end
-- ) as SALES_VALUE,
-- t1.MTH00LCLY * (
-- case
-- when t2.Value_Market_Ratio is null then 1
-- else t2.Value_Market_Ratio
-- end
-- ) as SALES_VALUE_LY,
-- t1.MTH00LCL2Y * (
-- case
-- when t2.Value_Market_Ratio is null then 1
-- else t2.Value_Market_Ratio
-- end
-- ) as SALES_VALUE_L2Y,
-- t1.MTH00UN * (
-- case
-- when t2.Unit_Market_Ratio is null then 1
-- else t2.Unit_Market_Ratio
-- end
-- ) as SALES_UNIT,
-- t1.MTH00UNLY * (
-- case
-- when t2.Unit_Market_Ratio is null then 1
-- else t2.Unit_Market_Ratio
-- end
-- ) as SALES_UNIT_LY,
-- t1.MTH00UNL2Y * (
-- case
-- when t2.Unit_Market_Ratio is null then 1
-- else t2.Unit_Market_Ratio
-- end
-- ) as SALES_UNIT_L2Y,
-- t1.MTH00CN as CONUTING_UNIT,
-- t1.MTH00CNLY as CONUTING_UNIT_LY,
-- t1.MTH00CNL2Y as CONUTING_UNIT_L2Y,
-- t1.MTH00CN * (
-- case
-- when t2.CountingUnit_Market_Ratio is null then 1
-- else t2.CountingUnit_Market_Ratio
-- end
-- ) as PDOT,
-- t1.MTH00CNLY * (
-- case
-- when t2.CountingUnit_Market_Ratio is null then 1
-- else t2.CountingUnit_Market_Ratio
-- end
-- ) as PDOT_LY,
-- t1.MTH00CNL2Y * (
-- case
-- when t2.CountingUnit_Market_Ratio is null then 1
-- else t2.CountingUnit_Market_Ratio
-- end
-- ) as PDOT_L2Y,
-- t1.MTH00LC as SALES_VALUE_CAL,
-- t1.MTH00LCLY as SALES_VALUE_CAL_LY,
-- t1.MTH00LCL2Y as SALES_VALUE_CAL_L2Y,
-- t1.MTH00UN as SALES_UNIT_CAL,
-- t1.MTH00UNLY as SALES_UNIT_CAL_LY,
-- t1.MTH00UNL2Y as SALES_UNIT_CAL_L2Y,
-- from_utc_timestamp (current_timestamp(), 'UTC+8') as ETL_INSERT_DT,
-- from_utc_timestamp (current_timestamp(), 'UTC+8') as ETL_UPDATE_DT
-- from
-- tmp_ims_tf_fact_sales_l2y t1
-- left join (
-- select distinct
-- YM,
-- market,
-- pack_cod,
-- Value_Market_Ratio,
-- Unit_Market_Ratio,
-- CountingUnit_Market_Ratio
-- from
-- dws.dws_ims_td_market_ratio
-- ) t2 on t1.pack_cod = t2.pack_cod
-- and t1.YM = t2.YM
-- where
-- -- t1.YM in (select YYYYMM from dm.dm_ims_td_calendar)and
-- t2.market is not null
-- union all
-- -- IMS ALL Market 为所有的IMS市场 不折算的数据的汇总即不折算的所有市场的city粒度数据
select distinct
t1.YM as YYYYMM,
t1.PACK_COD,
t1.AUDIT_COD,
-- 'ALL' AS RELATE_FLAG,
t1.AUDIT_COD as ORG_KEY,
-- 'IMS ALL Market' Market,
-- concat ('IMS ALL Market_', t1.pack_cod) as MARKET_PACK_KEY,
-- concat ('IMS ALL Market_', t1.audit_cod) as MARKET_AUDIT_KEY,
t1.MTH00LC SALES_VALUE,
t1.MTH00LCLY SALES_VALUE_LY,
t1.MTH00LCL2Y SALES_VALUE_L2Y,
t1.MTH00UN SALES_UNIT,
t1.MTH00UNLY SALES_UNIT_LY,
t1.MTH00UNL2Y SALES_UNIT_L2Y,
t1.MTH00CN CONUTING_UNIT,
t1.MTH00CNLY CONUTING_UNIT_LY,
t1.MTH00CNL2Y CONUTING_UNIT_L2Y,
t1.MTH00CN PDOT,
t1.MTH00CNLY PDOT_LY,
t1.MTH00CNL2Y PDOT_L2Y,
t1.MTH00LC SALES_VALUE_CAL,
t1.MTH00LCLY SALES_VALUE_CAL_LY,
t1.MTH00LCL2Y SALES_VALUE_CAL_L2Y,
t1.MTH00UN SALES_UNIT_CAL,
t1.MTH00UNLY SALES_UNIT_CAL_LY,
t1.MTH00UNL2Y SALES_UNIT_CAL_L2Y,
from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
from
tmp_ims_tf_fact_sales_l2y t1
-- where t1.YM in (select YYYYMM from dm.dm_ims_td_calendar)
;
-- COMMAND ----------
-- DBTITLE 1,tmp_ims_tf_sales_tamarket
-- --在原本的数据上加上AZ Related Market此为AZ 相关的六大市场数据汇总
-- --以及年月日的日期格式标签
-- CREATE
-- OR REPLACE TEMPORARY VIEW tmp_ims_tf_sales_tamarket AS
-- select distinct
-- YYYYMM,
-- concat (left (YYYYMM, 4), '-', right (YYYYMM, 2), '-01') as DAY_DATE,
-- PACK_COD,
-- AUDIT_COD,
-- RELATE_FLAG,
-- ORG_KEY,
-- Market,
-- MARKET_PACK_KEY,
-- MARKET_AUDIT_KEY,
-- SALES_VALUE,
-- SALES_VALUE_LY,
-- SALES_VALUE_L2Y,
-- SALES_UNIT,
-- SALES_UNIT_LY,
-- SALES_UNIT_L2Y,
-- CONUTING_UNIT,
-- CONUTING_UNIT_LY,
-- CONUTING_UNIT_L2Y,
-- PDOT,
-- PDOT_LY,
-- PDOT_L2Y,
-- SALES_VALUE_CAL,
-- SALES_VALUE_CAL_LY,
-- SALES_VALUE_CAL_L2Y,
-- SALES_UNIT_CAL,
-- SALES_UNIT_CAL_LY,
-- SALES_UNIT_CAL_L2Y,
-- from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
-- from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
-- from
-- tmp_ims_tf_sales_total
-- COMMAND ----------
-- DBTITLE 1,插入逻辑1-需保留
-- dm.dm_ims_tf_sales_china 和 dm.dm_ims_tf_sales 区别就在于 audit_cod = 'CHT' 或者 t1.audit_cod <> 'CHT'
-- insert overwrite table dm.dm_ims_tf_sales_china
-- from tmp_ims_tf_sales_tamarket t1
-- where t1.audit_cod = 'CHT' and (sales_value<>0 or sales_value_ly<>0) and t1.market not in ('Primary Prevention Market-XZK','Iressa 1L TKI Market' )
-- union all
-- from tmp_ims_tf_sales_tamarket t1
-- where t1.audit_cod = 'CHT' and (sales_value<>0 or sales_value_ly<>0) and t1.market in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')
-- insert overwrite table dm.dm_ims_tf_sales
-- from tmp_ims_tf_sales_tamarket t1
-- where t1.audit_cod <> 'CHT' and t1.market not in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')and (sales_value<>0 or sales_value_ly<>0)
-- union all
-- from tmp_ims_tf_sales_tamarket t1
-- where t1.audit_cod <> 'CHT' and t1.market in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')and (sales_value<>0 or sales_value_ly<>0)
-- COMMAND ----------
-- CREATE TABLE catalog_e2_ngbi_qas.dm.dm_ims_tf_sales_china (
-- YYYYMM INT,
-- PACK_COD STRING,
-- AUDIT_COD STRING,
-- SALES_VALUE DECIMAL(38,10),
-- SALES_VALUE_LY DECIMAL(38,10),
-- SALES_VALUE_L2Y DECIMAL(38,10),
-- SALES_UNIT DECIMAL(38,10),
-- SALES_UNIT_LY DECIMAL(38,10),
-- SALES_UNIT_L2Y DECIMAL(38,10),
-- CONUTING_UNIT DECIMAL(38,10),
-- CONUTING_UNIT_LY DECIMAL(38,10),
-- CONUTING_UNIT_L2Y DECIMAL(38,10),
-- PDOT DECIMAL(38,10),
-- PDOT_LY DECIMAL(38,10),
-- PDOT_L2Y DECIMAL(38,10),
-- SALES_VALUE_CAL DECIMAL(38,10),
-- SALES_VALUE_CAL_LY DECIMAL(38,10),
-- SALES_UNIT_CAL DECIMAL(38,10),
-- SALES_UNIT_CAL_LY DECIMAL(38,10),
-- ETL_INSERT_DT TIMESTAMP,
-- ETL_UPDATE_DT TIMESTAMP)
-- USING delta
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_ims_tf_sales_china'
-- COMMAND ----------
-- DBTITLE 1,overwrite dm.dm_ims_tf_sales_china
--SALES
--CHINA CHPA数据
-- t1.market not in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')
-- ,CONUTING_UNIT
-- ,CONUTING_UNIT_LY
-- ,CONUTING_UNIT_L2Y
-- ,SALES_VALUE_CAL
-- ,SALES_VALUE_CAL_LY
-- ,SALES_UNIT_CAL
-- ,SALES_UNIT_CAL_LY
-- from tmp_ims_tf_sales_tamarket t1
-- where t1.audit_cod = 'CHT' and (sales_value<>0 or sales_value_ly<>0) and t1.market not in ('Primary Prevention Market-XZK','Iressa 1L TKI Market' )
-- t1.market in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')
-- PDOT as CONUTING_UNIT
-- ,PDOT_LY as CONUTING_UNIT_LY
-- ,PDOT_L2Y as CONUTING_UNIT_L2Y
-- ,SALES_VALUE as SALES_VALUE_CAL
-- ,SALES_VALUE_LY as SALES_VALUE_CAL_LY
-- ,SALES_UNIT as SALES_UNIT_CAL
-- ,SALES_UNIT_LY as SALES_UNIT_CAL_LY
-- from tmp_ims_tf_sales_tamarket t1
-- where t1.audit_cod = 'CHT' and (sales_value<>0 or sales_value_ly<>0) and t1.market in ('Primary Prevention Market-XZK','Iressa 1L TKI Market')
-- create or replace table dm.dm_ims_tf_sales_china
insert overwrite table dm.dm_ims_tf_sales_china
select distinct
YYYYMM,
-- DAY_DATE,
PACK_COD,
AUDIT_COD,
-- RELATE_FLAG,
-- ORG_KEY,
-- Market,
-- MARKET_PACK_KEY,
-- MARKET_AUDIT_KEY,
SALES_VALUE,
SALES_VALUE_LY,
SALES_VALUE_L2Y,
SALES_UNIT,
SALES_UNIT_LY,
SALES_UNIT_L2Y,
CONUTING_UNIT,
CONUTING_UNIT_LY,
CONUTING_UNIT_L2Y,
PDOT,
PDOT_LY,
PDOT_L2Y,
SALES_VALUE_CAL,
SALES_VALUE_CAL_LY,
SALES_UNIT_CAL,
SALES_UNIT_CAL_LY,
from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
from
tmp_ims_tf_sales_total t1
where
t1.audit_cod = 'CHT'
and (
sales_value <> 0
or sales_value_ly <> 0
)
-- and t1.market not in (
-- 'Primary Prevention Market-XZK',
-- 'Iressa 1L TKI Market'
-- )
-- union all
-- select distinct
-- YYYYMM,
-- DAY_DATE,
-- PACK_COD,
-- AUDIT_COD,
-- RELATE_FLAG,
-- ORG_KEY,
-- Market,
-- MARKET_PACK_KEY,
-- MARKET_AUDIT_KEY,
-- SALES_VALUE,
-- SALES_VALUE_LY,
-- SALES_VALUE_L2Y,
-- SALES_UNIT,
-- SALES_UNIT_LY,
-- SALES_UNIT_L2Y,
-- PDOT as CONUTING_UNIT,
-- PDOT_LY as CONUTING_UNIT_LY,
-- PDOT_L2Y as CONUTING_UNIT_L2Y,
-- PDOT,
-- PDOT_LY,
-- PDOT_L2Y,
-- SALES_VALUE as SALES_VALUE_CAL,
-- SALES_VALUE_LY as SALES_VALUE_CAL_LY,
-- SALES_UNIT as SALES_UNIT_CAL,
-- SALES_UNIT_LY as SALES_UNIT_CAL_LY,
-- from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
-- from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
-- from
-- tmp_ims_tf_sales_tamarket t1
-- where
-- t1.audit_cod = 'CHT'
-- and (
-- sales_value <> 0
-- or sales_value_ly <> 0
-- )
-- and t1.market in (
-- 'Primary Prevention Market-XZK',
-- 'Iressa 1L TKI Market'
-- )
-- COMMAND ----------
-- CREATE or REPLACE TABLE dm.dm_ims_tf_sales (
-- YYYYMM INT,
-- PACK_COD STRING,
-- AUDIT_COD STRING,
-- SALES_VALUE DECIMAL(38,10),
-- SALES_VALUE_LY DECIMAL(38,10),
-- SALES_VALUE_L2Y DECIMAL(38,10),
-- SALES_UNIT DECIMAL(38,10),
-- SALES_UNIT_LY DECIMAL(38,10),
-- SALES_UNIT_L2Y DECIMAL(38,10),
-- CONUTING_UNIT DECIMAL(38,10),
-- CONUTING_UNIT_LY DECIMAL(38,10),
-- CONUTING_UNIT_L2Y DECIMAL(38,10),
-- PDOT DECIMAL(38,10),
-- PDOT_LY DECIMAL(38,10),
-- PDOT_L2Y DECIMAL(38,10),
-- SALES_VALUE_CAL DECIMAL(38,10),
-- SALES_VALUE_CAL_LY DECIMAL(38,10),
-- SALES_UNIT_CAL DECIMAL(38,10),
-- SALES_UNIT_CAL_LY DECIMAL(38,10),
-- ETL_INSERT_DT TIMESTAMP,
-- ETL_UPDATE_DT TIMESTAMP)
-- USING delta
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_ims_tf_sales'
-- COMMAND ----------
-- DBTITLE 1,overwrite dm.dm_ims_tf_sales
--SALES
--CITY
insert overwrite table dm.dm_ims_tf_sales
select distinct
YYYYMM,
-- DAY_DATE,
PACK_COD,
AUDIT_COD,
-- RELATE_FLAG,
-- ORG_KEY,
-- Market,
-- MARKET_PACK_KEY,
-- MARKET_AUDIT_KEY,
SALES_VALUE,
SALES_VALUE_LY,
SALES_VALUE_L2Y,
SALES_UNIT,
SALES_UNIT_LY,
SALES_UNIT_L2Y,
CONUTING_UNIT,
CONUTING_UNIT_LY,
CONUTING_UNIT_L2Y,
PDOT,
PDOT_LY,
PDOT_L2Y,
SALES_VALUE_CAL,
SALES_VALUE_CAL_LY,
SALES_UNIT_CAL,
SALES_UNIT_CAL_LY,
from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
from
tmp_ims_tf_sales_total t1
where
t1.audit_cod <> 'CHT'
-- and t1.market not in (
-- 'Primary Prevention Market-XZK',
-- 'Iressa 1L TKI Market'
-- )
and (
sales_value <> 0
or sales_value_ly <> 0
)
-- union all
-- --'Primary Prevention Market-XZK','Iressa 1L TKI Market' 两个市场特殊处理所有的value都是折算过的
-- select distinct
-- YYYYMM,
-- DAY_DATE,
-- PACK_COD,
-- AUDIT_COD,
-- RELATE_FLAG,
-- ORG_KEY,
-- Market,
-- MARKET_PACK_KEY,
-- MARKET_AUDIT_KEY,
-- SALES_VALUE,
-- SALES_VALUE_LY,
-- SALES_VALUE_L2Y,
-- SALES_UNIT,
-- SALES_UNIT_LY,
-- SALES_UNIT_L2Y,
-- PDOT as CONUTING_UNIT,
-- PDOT_LY as CONUTING_UNIT_LY,
-- PDOT_L2Y as CONUTING_UNIT_L2Y,
-- PDOT,
-- PDOT_LY,
-- PDOT_L2Y,
-- SALES_VALUE as SALES_VALUE_CAL,
-- SALES_VALUE_LY as SALES_VALUE_CAL_LY,
-- SALES_UNIT as SALES_UNIT_CAL,
-- SALES_UNIT_LY as SALES_UNIT_CAL_LY,
-- from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
-- from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
-- from
-- tmp_ims_tf_sales_tamarket t1
-- where
-- t1.audit_cod <> 'CHT'
-- -- and t1.market in (
-- -- 'Primary Prevention Market-XZK',
-- -- 'Iressa 1L TKI Market'
-- -- )
-- -- and (
-- -- sales_value <> 0
-- -- or sales_value_ly <> 0
-- -- )
-- COMMAND ----------
-- DBTITLE 1,插入逻辑2-需保留
-- -- 关键词
-- audit_cod <> 'CHT' -》 dm.dm_ims_tf_sales
-- audit_cod = 'CHT' -》dm.dm_ims_tf_sales_china
-- KCT 所有城市的IMS数据 基于 dm.dm_ims_tf_sales
-- ROC 其他城市的IMS数据计算 ROC = CHT-KCT
-- COMMAND ----------
-- DBTITLE 1,overwrite tmp.tmp_ims_tf_sales_kct
-- 所有城市汇总的IMS数据计算 KCT=sum(all city)
-- create or replace table tmp.tmp_ims_tf_sales_kct
insert overwrite table tmp.tmp_ims_tf_sales_kct
select
YYYYMM,
PACK_COD,
'KCT' as AUDIT_COD,
-- Related_flag,
-- 'KCT' as ORG_KEY,
-- Market,
-- MARKET_PACK_KEY,
-- concat (Market, '_KCT') as MARKET_AUDIT_KEY,
sum(SALES_VALUE) as SALES_VALUE,
sum(SALES_VALUE_LY) as SALES_VALUE_LY,
sum(SALES_VALUE_L2Y) as SALES_VALUE_L2Y,
sum(SALES_UNIT) as SALES_UNIT,
sum(SALES_UNIT_LY) as SALES_UNIT_LY,
sum(SALES_UNIT_L2Y) as SALES_UNIT_L2Y,
sum(CONUTING_UNIT) as CONUTING_UNIT,
sum(CONUTING_UNIT_LY) as CONUTING_UNIT_LY,
sum(CONUTING_UNIT_L2Y) as CONUTING_UNIT_L2Y,
sum(PDOT) as PDOT,
sum(PDOT_LY) as PDOT_LY,
sum(PDOT_LY) as PDOT_L2Y,
sum(SALES_VALUE_CAL) as SALES_VALUE_CAL,
sum(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
sum(SALES_UNIT_CAL) as SALES_UNIT_CAL,
sum(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY
from
dm.dm_ims_tf_sales
group by
YYYYMM,
PACK_COD
-- Related_flag,
-- Market,
-- MARKET_PACK_KEY;
-- COMMAND ----------
-- DBTITLE 1,overwrite tmp.tmp_ims_tf_sales_roc
-- 其他城市的IMS数据计算 ROC=CHT-KCT
insert overwrite table tmp.tmp_ims_tf_sales_roc
WITH
DIM_YM_PACK_MARKT (
SELECT
YYYYMM as CHINA_YYYYMM,
PACK_COD as CHINA_PACK_COD
-- ,CHINA_Market
from
dm.dm_ims_tf_sales_china
group by
YYYYMM,
PACK_COD
-- ,CHINA_Market
UNION
SELECT
YYYYMM,
PACK_COD
-- ,Market
from
tmp.tmp_ims_tf_sales_kct
group by
YYYYMM,
PACK_COD
-- ,Market
),
CHPA_SALES (
select
YYYYMM,
PACK_COD,
-- CHINA_Market,
sum(SALES_VALUE) as CHINA_SALES_VALUE,
sum(SALES_VALUE_LY) as CHINA_SALES_VALUE_LY,
sum(SALES_VALUE_L2Y) as CHINA_SALES_VALUE_L2Y,
sum(SALES_UNIT) as CHINA_SALES_UNIT,
sum(SALES_UNIT_LY) as CHINA_SALES_UNIT_LY,
sum(SALES_UNIT_L2Y) as CHINA_SALES_UNIT_L2Y,
sum(CONUTING_UNIT) as CHINA_CONUTING_UNIT,
sum(CONUTING_UNIT_LY) as CHINA_CONUTING_UNIT_LY,
sum(CONUTING_UNIT_L2Y) as CHINA_CONUTING_UNIT_L2Y,
sum(PDOT) as CHINA_PDOT,
sum(PDOT_LY) as CHINA_PDOT_LY,
sum(PDOT_L2Y) as CHINA_PDOT_L2Y,
sum(SALES_VALUE_CAL) as SALES_VALUE_CAL,
sum(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
sum(SALES_UNIT_CAL) as SALES_UNIT_CAL,
sum(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY
from
dm.dm_ims_tf_sales_china
group by
YYYYMM,
PACK_COD
-- ,CHINA_Market
),
FABO_SALES (
select
YYYYMM,
PACK_COD,
-- Market,
sum(SALES_VALUE) as SALES_VALUE,
sum(SALES_VALUE_LY) as SALES_VALUE_LY,
sum(SALES_VALUE_L2Y) as SALES_VALUE_L2Y,
sum(SALES_UNIT) as SALES_UNIT,
sum(SALES_UNIT_LY) as SALES_UNIT_LY,
sum(SALES_UNIT_L2Y) as SALES_UNIT_L2Y,
sum(CONUTING_UNIT) as CONUTING_UNIT,
sum(CONUTING_UNIT_LY) as CONUTING_UNIT_LY,
sum(CONUTING_UNIT_L2Y) as CONUTING_UNIT_L2Y,
sum(PDOT) as PDOT,
sum(PDOT_LY) as PDOT_LY,
sum(PDOT_L2Y) as PDOT_L2Y,
sum(SALES_VALUE_CAL) as SALES_VALUE_CAL,
sum(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
sum(SALES_UNIT_CAL) as SALES_UNIT_CAL,
sum(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY
from
tmp.tmp_ims_tf_sales_kct
group by
YYYYMM,
PACK_COD
-- ,Market
)
SELECT
t1.CHINA_YYYYMM as YYYYMM,
t1.CHINA_PACK_COD as PACK_COD,
'ROC' as AUDIT_COD,
-- null as Related_flag,
-- 'ROC' as ORG_KEY,
-- t1.CHINA_Market as Market,
-- concat (t1.CHINA_Market, '_', t1.china_pack_cod) as MARKET_PACK_KEY,
-- concat (t1.CHINA_Market, '_ROC') as MARKET_AUDIT_KEY,
nvl (t2.CHINA_SALES_VALUE, 0) - nvl (t3.SALES_VALUE, 0) as SALES_VALUE,
nvl (t2.CHINA_SALES_VALUE_LY, 0) - nvl (t3.SALES_VALUE_LY, 0) as SALES_VALUE_LY,
nvl (t2.CHINA_SALES_VALUE_L2Y, 0) - nvl (t3.SALES_VALUE_L2Y, 0) as SALES_VALUE_L2Y,
nvl (t2.CHINA_SALES_UNIT, 0) - nvl (t3.SALES_UNIT, 0) as SALES_UNIT,
nvl (t2.CHINA_SALES_UNIT_L2Y, 0) - nvl (t3.SALES_UNIT_L2Y, 0) as SALES_UNIT_LY,
nvl (t2.CHINA_SALES_UNIT_LY, 0) - nvl (t3.SALES_UNIT_LY, 0) as SALES_UNIT_L2Y,
nvl (t2.CHINA_CONUTING_UNIT, 0) - nvl (t3.CONUTING_UNIT, 0) as CONUTING_UNIT,
nvl (t2.CHINA_CONUTING_UNIT_LY, 0) - nvl (t3.CONUTING_UNIT_LY, 0) as CONUTING_UNIT_LY,
nvl (t2.CHINA_CONUTING_UNIT_L2Y, 0) - nvl (t3.CONUTING_UNIT_L2Y, 0) as CONUTING_UNIT_L2Y,
nvl (t2.CHINA_PDOT, 0) - nvl (t3.PDOT, 0) as PDOT,
nvl (t2.CHINA_PDOT_LY, 0) - nvl (t3.PDOT_LY, 0) as PDOT_LY,
nvl (t2.CHINA_PDOT_L2Y, 0) - nvl (t3.PDOT_L2Y, 0) as PDOT_L2Y,
nvl (t2.SALES_VALUE_CAL, 0) - nvl (t3.SALES_VALUE_CAL, 0) as SALES_VALUE_CAL,
nvl (t2.SALES_VALUE_CAL_LY, 0) - nvl (t3.SALES_VALUE_CAL_LY, 0) as SALES_VALUE_CAL_LY,
nvl (t2.SALES_UNIT_CAL, 0) - nvl (t3.SALES_UNIT_CAL, 0) as SALES_UNIT_CAL,
nvl (t2.SALES_UNIT_CAL_LY, 0) - nvl (t3.SALES_UNIT_CAL_LY, 0) as SALES_UNIT_CAL_LY
FROM
dim_ym_pack_markt t1
LEFT JOIN chpa_sales t2 on
-- t1.china_market = t2.china_market and
t1.china_yyyymm = t2.yyyymm
and t1.china_pack_cod = t2.pack_cod
LEFT JOIN fabo_sales t3 on
-- t1.china_market = t3.market and
t1.china_yyyymm = t3.yyyymm
and t1.china_pack_cod = t3.pack_cod
-- COMMAND ----------
-- DBTITLE 1,insert into dm.dm_ims_tf_sales
--将ROC的数据写入dm.dm_ims_tf_sales
insert into
dm.dm_ims_tf_sales
select distinct
YYYYMM,
-- concat (left (YYYYMM, 4), '-', right (YYYYMM, 2), '-01') as DAY_DATE,
PACK_COD,
AUDIT_COD,
-- RELATED_FLAG,
-- ORG_KEY,
-- Market,
-- MARKET_PACK_KEY,
-- MARKET_AUDIT_KEY,
SALES_VALUE,
SALES_VALUE_LY,
SALES_VALUE_L2Y,
SALES_UNIT,
SALES_UNIT_LY,
SALES_UNIT_L2Y,
CONUTING_UNIT,
CONUTING_UNIT_LY,
CONUTING_UNIT_L2Y,
PDOT,
PDOT_LY,
PDOT_L2Y,
SALES_VALUE_CAL,
SALES_VALUE_CAL_LY,
SALES_UNIT_CAL,
SALES_UNIT_CAL_LY,
from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
from
tmp.tmp_ims_tf_sales_roc
where
sales_value <> 0
or sales_value_ly <> 0;