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"
1187 lines
30 KiB
SQL
1187 lines
30 KiB
SQL
-- Databricks notebook source
|
||
-- MAGIC %md
|
||
-- MAGIC ## 清空表
|
||
|
||
-- COMMAND ----------
|
||
|
||
TRUNCATE TABLE dm.dm_tf_external_sales
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- MAGIC %md
|
||
-- MAGIC ## 各渠道逻辑
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,CHPA
|
||
-- DELETE FROM
|
||
-- dm.dm_tf_external_sales
|
||
-- WHERE
|
||
-- DATA_SOURCE = 'IQVIA-CHPA(Monthly)';
|
||
|
||
INSERT INTO dm.dm_tf_external_sales (
|
||
YYYYMM,
|
||
PACK_COD,
|
||
AUDIT_COD,
|
||
DATA_SOURCE,
|
||
SALES_UNIT_CAL,
|
||
SALES_UNIT_CAL_LY,
|
||
SALES_VALUE_CAL,
|
||
SALES_VALUE_CAL_LY,
|
||
CONUTING_UNIT,
|
||
CONUTING_UNIT_LY,
|
||
prescription,
|
||
prescription_ly,
|
||
PACK_FLAG,
|
||
PROD_FLAG,
|
||
DTP_FLAG,
|
||
cmps_flag,
|
||
PLATFORM_TYPE,
|
||
STORE_NAME,
|
||
STORE_TYPE,
|
||
REGION_TYPE,
|
||
inst_code,
|
||
dept_name,
|
||
new_code,
|
||
area,
|
||
h_level,
|
||
reimburse,
|
||
reimburse_type,
|
||
prescription_source,
|
||
CORP_COD
|
||
)
|
||
SELECT
|
||
A.YYYYMM,
|
||
A.PACK_COD,
|
||
A.AUDIT_COD,
|
||
'IQVIA-CHPA(Monthly)' DATA_SOURCE,
|
||
-------------------------------------
|
||
SUM(SALES_UNIT_CAL) SALES_UNIT_CAL,
|
||
SUM(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY,
|
||
SUM(SALES_VALUE_CAL) SALES_VALUE_CAL,
|
||
SUM(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
|
||
SUM(CONUTING_UNIT) CONUTING_UNIT,
|
||
SUM(CONUTING_UNIT_LY) as CONUTING_UNIT_LY,
|
||
null as prescription,
|
||
null as prescription_ly,
|
||
-------------------------------------
|
||
--Retail藏数逻辑标签------------------
|
||
1 PACK_FLAG,
|
||
1 PROD_FLAG,
|
||
0 DTP_FLAG,
|
||
null as cmps_flag,
|
||
-------------------------------------
|
||
--EC数据标签--------------------------
|
||
'' PLATFORM_TYPE,
|
||
'' STORE_NAME,
|
||
'' STORE_TYPE,
|
||
-------------------------------------
|
||
--COUNTY数据标签----------------------
|
||
'' REGION_TYPE,
|
||
-------------------------------------
|
||
--AIA数据标签-------------------------
|
||
null inst_code, --可以移除,报告没有使用
|
||
-------------------------------------
|
||
--XIE HE 数据标签---------------------
|
||
'' as dept_name,
|
||
'' as new_code,
|
||
'' as area,
|
||
'' as h_level,
|
||
'' as reimburse,
|
||
'' as reimburse_type,
|
||
'' as prescription_source,
|
||
-------------------------------------
|
||
'' as CORP_COD --可以移除,报告没有使用
|
||
-------------------------------------
|
||
FROM
|
||
dm.dm_ims_tf_sales A
|
||
where
|
||
A.YYYYMM >= '202001'
|
||
AND A.MARKET = 'IMS ALL Market'
|
||
GROUP BY
|
||
A.YYYYMM,
|
||
A.PACK_COD,
|
||
A.AUDIT_COD
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,COUNTY
|
||
-- DELETE FROM
|
||
-- dm.dm_tf_external_sales
|
||
-- WHERE
|
||
-- DATA_SOURCE = 'IQVIA-COUNTY(Quarterly)';
|
||
|
||
INSERT INTO dm.dm_tf_external_sales (
|
||
YYYYMM,
|
||
PACK_COD,
|
||
AUDIT_COD,
|
||
DATA_SOURCE,
|
||
SALES_UNIT_CAL,
|
||
SALES_UNIT_CAL_LY,
|
||
SALES_VALUE_CAL,
|
||
SALES_VALUE_CAL_LY,
|
||
CONUTING_UNIT,
|
||
CONUTING_UNIT_LY,
|
||
prescription,
|
||
prescription_ly,
|
||
PACK_FLAG,
|
||
PROD_FLAG,
|
||
DTP_FLAG,
|
||
cmps_flag,
|
||
PLATFORM_TYPE,
|
||
STORE_NAME,
|
||
STORE_TYPE,
|
||
REGION_TYPE,
|
||
inst_code,
|
||
dept_name,
|
||
new_code,
|
||
area,
|
||
h_level,
|
||
reimburse,
|
||
reimburse_type,
|
||
prescription_source,
|
||
CORP_COD
|
||
)
|
||
SELECT
|
||
A.YYYYMM,
|
||
A.PACK_COD,
|
||
A.AUDIT_COD,
|
||
'IQVIA-COUNTY(Quarterly)' DATA_SOURCE,
|
||
-------------------------------------
|
||
SUM(SALES_UNIT_CAL) SALES_UNIT_CAL,
|
||
SUM(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY,
|
||
SUM(SALES_VALUE_CAL) SALES_VALUE_CAL,
|
||
SUM(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
|
||
SUM(CONUTING_UNIT) CONUTING_UNIT,
|
||
SUM(CONUTING_UNIT_LY) as CONUTING_UNIT_LY,
|
||
null as prescription,
|
||
null as prescription_ly,
|
||
-------------------------------------
|
||
--Retail藏数逻辑标签------------------
|
||
1 PACK_FLAG,
|
||
1 PROD_FLAG,
|
||
0 DTP_FLAG,
|
||
null as cmps_flag,
|
||
-------------------------------------
|
||
--EC数据标签--------------------------
|
||
'' PLATFORM_TYPE,
|
||
'' STORE_NAME,
|
||
'' STORE_TYPE,
|
||
-------------------------------------
|
||
--COUNTY数据标签----------------------
|
||
NVL(A.REGION_TYPE, '') REGION_TYPE,
|
||
-------------------------------------
|
||
--AIA数据标签-------------------------
|
||
null inst_code, --可以移除,报告没有使用
|
||
-------------------------------------
|
||
--XIE HE 数据标签---------------------
|
||
'' as dept_name,
|
||
'' as new_code,
|
||
'' as area,
|
||
'' as h_level,
|
||
'' as reimburse,
|
||
'' as reimburse_type,
|
||
'' as prescription_source,
|
||
-------------------------------------
|
||
'' as CORP_COD --可以移除,报告没有使用
|
||
-------------------------------------
|
||
FROM
|
||
DM.dm_ext_county_tf_sales_region A
|
||
where
|
||
A.YYYYMM >= '202001'
|
||
AND A.MARKET = 'IMS ALL Market'
|
||
GROUP BY
|
||
A.YYYYMM,
|
||
A.PACK_COD,
|
||
A.AUDIT_COD,
|
||
A.REGION_TYPE
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,THC
|
||
-- DELETE FROM
|
||
-- dm.dm_tf_external_sales
|
||
-- WHERE
|
||
-- DATA_SOURCE = 'THC(Quarterly)';
|
||
|
||
INSERT INTO dm.dm_tf_external_sales (
|
||
YYYYMM,
|
||
PACK_COD,
|
||
AUDIT_COD,
|
||
DATA_SOURCE,
|
||
SALES_UNIT_CAL,
|
||
SALES_UNIT_CAL_LY,
|
||
SALES_VALUE_CAL,
|
||
SALES_VALUE_CAL_LY,
|
||
CONUTING_UNIT,
|
||
CONUTING_UNIT_LY,
|
||
prescription,
|
||
prescription_ly,
|
||
PACK_FLAG,
|
||
PROD_FLAG,
|
||
DTP_FLAG,
|
||
cmps_flag,
|
||
PLATFORM_TYPE,
|
||
STORE_NAME,
|
||
STORE_TYPE,
|
||
REGION_TYPE,
|
||
inst_code,
|
||
dept_name,
|
||
new_code,
|
||
area,
|
||
h_level,
|
||
reimburse,
|
||
reimburse_type,
|
||
prescription_source,
|
||
CORP_COD
|
||
)
|
||
SELECT
|
||
A.YYYYMM,
|
||
A.PACK_COD,
|
||
A.AUDIT_COD,
|
||
'THC(Quarterly)' DATA_SOURCE,
|
||
-------------------------------------
|
||
SALES_UNIT_CAL,
|
||
SALES_UNIT_CAL_LY,
|
||
SALES_VALUE_CAL,
|
||
SALES_VALUE_CAL_LY,
|
||
COUNTING_UNIT,
|
||
COUNTING_UNIT_LY ,
|
||
null as prescription,
|
||
null as prescription_ly,
|
||
-------------------------------------
|
||
--Retail藏数逻辑标签------------------
|
||
1 PACK_FLAG,
|
||
1 PROD_FLAG,
|
||
0 DTP_FLAG,
|
||
null as cmps_flag,
|
||
-------------------------------------
|
||
--EC数据标签--------------------------
|
||
'' PLATFORM_TYPE,
|
||
'' STORE_NAME,
|
||
'' STORE_TYPE,
|
||
-------------------------------------
|
||
--COUNTY数据标签----------------------
|
||
'' REGION_TYPE,
|
||
-------------------------------------
|
||
--AIA数据标签-------------------------
|
||
null inst_code, --可以移除,报告没有使用
|
||
-------------------------------------
|
||
--XIE HE 数据标签---------------------
|
||
'' as dept_name,
|
||
'' as new_code,
|
||
'' as area,
|
||
'' as h_level,
|
||
'' as reimburse,
|
||
'' as reimburse_type,
|
||
'' as prescription_source,
|
||
-------------------------------------
|
||
'' as CORP_COD --可以移除,报告没有使用
|
||
-------------------------------------
|
||
FROM
|
||
dm.dm_ext_thc_sales A
|
||
where
|
||
AUDIT_COD = 'CHT'
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,CHC_RAW_DATA
|
||
------------------------------------------------------------------------------CHC-------------------------------------------------------------------
|
||
CREATE OR REPLACE TEMPORARY VIEW V_CHC_RAW_DATA AS
|
||
with exchc_rawdata_temp as ( --主表数据
|
||
select
|
||
CASE
|
||
WHEN SUBSTRING(yq, 5,2) = 'Q1' THEN CONCAT(SUBSTRING(yq, 1, 4), '03')
|
||
WHEN SUBSTRING(yq, 5, 2) = 'Q2' THEN CONCAT(SUBSTRING(yq, 1, 4), '06')
|
||
WHEN SUBSTRING(yq, 5, 2) = 'Q3' THEN CONCAT(SUBSTRING(yq, 1, 4), '09')
|
||
WHEN SUBSTRING(yq, 5, 2) = 'Q4' THEN CONCAT(SUBSTRING(yq, 1, 4), '12')
|
||
ELSE yq
|
||
END AS yq,
|
||
city_c,
|
||
pack_cod,
|
||
prod_cod,
|
||
value,
|
||
totalunit,
|
||
countingunit
|
||
from dwd.dwd_gnd_chc_rawdata
|
||
where year >= '2021'
|
||
),
|
||
temp_corp as (
|
||
select
|
||
PACK_COD,
|
||
CORP_COD,
|
||
CORP_DES
|
||
from dm.dm_ims_td_pack_property
|
||
group by PACK_COD,CORP_COD,CORP_DES
|
||
),
|
||
chc_pack_property_clean as (
|
||
select distinct
|
||
iqvia_pack_code ,
|
||
iqvia_prod_code ,
|
||
pack_cod ,
|
||
app1_cod ,
|
||
app1_des ,
|
||
app1_des_c ,
|
||
app2_cod ,
|
||
app2_des ,
|
||
app2_des_c ,
|
||
app3_cod ,
|
||
app3_des ,
|
||
app3_des_c ,
|
||
atc1_cod ,
|
||
atc1_des ,
|
||
atc1_des_c ,
|
||
atc2_cod ,
|
||
atc2_des ,
|
||
atc2_des_c ,
|
||
atc3_cod ,
|
||
atc3_des ,
|
||
atc3_des_c ,
|
||
atc4_cod ,
|
||
atc4_des ,
|
||
atc4_des_c ,
|
||
bio_desc ,
|
||
cmps_cod ,
|
||
cmps_des ,
|
||
cmps_des_c ,
|
||
corp_cod ,
|
||
corp_des ,
|
||
corp_des_c ,
|
||
edl_desc ,
|
||
eth_otc_desc ,
|
||
gene_orig_desc ,
|
||
gqce_desc ,
|
||
manu_cod ,
|
||
manu_des ,
|
||
manu_des_c ,
|
||
mnfl_cod ,
|
||
mnfl_des ,
|
||
nrdl_desc ,
|
||
pack_des ,
|
||
stgh_des ,
|
||
pack_lch ,
|
||
paed_desc ,
|
||
prod_des ,
|
||
prod_des_c ,
|
||
tcm_desc ,
|
||
vbp_desc ,
|
||
builtinarchivedate ,
|
||
source_file_path ,
|
||
source_file_name ,
|
||
etl_insert_dt ,
|
||
countingunit
|
||
from dwd.dwd_gnd_ext_chc_pack_property
|
||
),
|
||
temp_corp_temp as ( -- chc本身唯独表 存在corp_cod以及 CORP_DES 与手工数据不一致的问题
|
||
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 chc_pack_property_clean t1
|
||
left join temp_corp t2
|
||
on t1.iqvia_pack_code = t2.PACK_COD
|
||
),
|
||
dim_exchc_geo as (
|
||
-- geo相关信息 在CHPA中没有 苏州、无锡、福州等数据,根据业务反馈,按照 city_c当作AUDIT_COD
|
||
select
|
||
t1.city_e,
|
||
concat( t1.city_c , '市') as city_c,
|
||
upper( nvl(t2.AUDIT_COD ,t1.city_e) ) as AUDIT_COD
|
||
from dwd.dwd_gnd_ext_chc_geo t1
|
||
left join dm.dm_ims_td_geo t2
|
||
on t1.city_e= t2.city
|
||
-- --20260122 按照新的业务逻辑,直接用城市名称匹配获得省份code
|
||
-- SELECT DISTINCT city_name,province_code,province_name FROM DM.dm_td_geography
|
||
-- WHERE nvl(city_code,'')!='' and nvl(province_code,'')!='' and nvl(province_name,'')!=''
|
||
),
|
||
exchc_rawdata_iqvia as (
|
||
select
|
||
t1.yq ,
|
||
t1.city_c,
|
||
-- t1.pack_cod pack_cod_old,
|
||
t3.iqvia_pack_code as pack_cod,
|
||
t3.CORP_COD as CORP_COD,
|
||
C.AUDIT_COD as AUDIT_COD,
|
||
t1.prod_cod,
|
||
t1.value,
|
||
t1.totalunit,
|
||
-- t1.countingunit,
|
||
t1.totalunit * nvl(t3.countingunit,1 ) as countingunit
|
||
-- t2.countingunit as tt
|
||
from exchc_rawdata_temp t1
|
||
left join temp_corp_temp t3
|
||
on t1.pack_cod = t3.pack_cod
|
||
LEFT JOIN dim_exchc_geo C
|
||
ON t1.CITY_C = C.city_c
|
||
),
|
||
value_temp as(
|
||
select
|
||
a.yq,
|
||
a.AUDIT_COD,
|
||
a.pack_cod,
|
||
--t1.prod_cod,
|
||
a.CORP_COD,
|
||
sum(a.value) value,
|
||
sum(a.totalunit) totalunit,
|
||
sum(a.countingunit) countingunit,
|
||
sum(value_ly) value_ly,
|
||
sum(totalunit_ly) totalunit_ly,
|
||
sum(countingunit_ly) countingunit_ly
|
||
from (
|
||
select
|
||
t1.yq,
|
||
t1.AUDIT_COD,
|
||
t1.pack_cod,
|
||
--t1.prod_cod,
|
||
t1.CORP_COD,
|
||
t1.value,
|
||
t1.totalunit,
|
||
t1.countingunit,
|
||
0 value_ly,
|
||
0 totalunit_ly,
|
||
0 countingunit_ly
|
||
from exchc_rawdata_iqvia t1
|
||
union all
|
||
select
|
||
cast(t1.yq+100 as int),
|
||
t1.AUDIT_COD,
|
||
t1.pack_cod,
|
||
--t1.prod_cod,
|
||
t1.CORP_COD,
|
||
0 value,
|
||
0 totalunit,
|
||
0 countingunit,
|
||
t1.value value_ly,
|
||
t1.totalunit totalunit_ly,
|
||
t1.countingunit countingunit_ly
|
||
from exchc_rawdata_iqvia t1
|
||
WHERE T1.YQ+100 <= (SELECT MAX(YQ) FROM exchc_rawdata_iqvia)
|
||
) a
|
||
group by a.yq,
|
||
a.AUDIT_COD,
|
||
a.pack_cod,
|
||
--t1.prod_cod,
|
||
a.CORP_COD
|
||
)
|
||
|
||
select
|
||
t1.yq as YYYYMM ,
|
||
t1.AUDIT_COD,
|
||
t1.pack_cod,
|
||
t1.CORP_COD
|
||
--t1.prod_cod
|
||
,sum(totalunit) AS SALES_UNIT_CAL -- totalunit
|
||
,sum(totalunit_ly) AS SALES_UNIT_CAL_LY --totalunit_ly
|
||
,sum(value) AS SALES_VALUE_CAL --value
|
||
,sum(value_ly ) AS SALES_VALUE_CAL_LY
|
||
,sum(countingunit) AS CONUTING_UNIT --countingunit
|
||
,sum(countingunit_ly) AS CONUTING_UNIT_LY
|
||
,'CHC(Quarterly)' as DATA_SOURCE
|
||
from value_temp t1
|
||
group by 1,2,3,4
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,CHC
|
||
-- DELETE FROM
|
||
-- dm.dm_tf_external_sales
|
||
-- WHERE
|
||
-- DATA_SOURCE = 'CHC(Quarterly)';
|
||
|
||
INSERT INTO dm.dm_tf_external_sales (
|
||
YYYYMM,
|
||
PACK_COD,
|
||
AUDIT_COD,
|
||
DATA_SOURCE,
|
||
SALES_UNIT_CAL,
|
||
SALES_UNIT_CAL_LY,
|
||
SALES_VALUE_CAL,
|
||
SALES_VALUE_CAL_LY,
|
||
CONUTING_UNIT,
|
||
CONUTING_UNIT_LY,
|
||
prescription,
|
||
prescription_ly,
|
||
PACK_FLAG,
|
||
PROD_FLAG,
|
||
DTP_FLAG,
|
||
cmps_flag,
|
||
PLATFORM_TYPE,
|
||
STORE_NAME,
|
||
STORE_TYPE,
|
||
REGION_TYPE,
|
||
inst_code,
|
||
dept_name,
|
||
new_code,
|
||
area,
|
||
h_level,
|
||
reimburse,
|
||
reimburse_type,
|
||
prescription_source,
|
||
CORP_COD
|
||
)
|
||
SELECT
|
||
A.YYYYMM,
|
||
A.PACK_COD,
|
||
A.AUDIT_COD,
|
||
'CHC(Quarterly)' DATA_SOURCE,
|
||
-------------------------------------
|
||
SALES_UNIT_CAL,
|
||
SALES_UNIT_CAL_LY,
|
||
SALES_VALUE_CAL,
|
||
SALES_VALUE_CAL_LY,
|
||
CONUTING_UNIT,
|
||
CONUTING_UNIT_LY,
|
||
null as prescription,
|
||
null as prescription_ly,
|
||
-------------------------------------
|
||
--Retail藏数逻辑标签------------------
|
||
1 PACK_FLAG,
|
||
1 PROD_FLAG,
|
||
0 DTP_FLAG,
|
||
null as cmps_flag,
|
||
-------------------------------------
|
||
--EC数据标签--------------------------
|
||
'' PLATFORM_TYPE,
|
||
'' STORE_NAME,
|
||
'' STORE_TYPE,
|
||
-------------------------------------
|
||
--COUNTY数据标签----------------------
|
||
'' REGION_TYPE,
|
||
-------------------------------------
|
||
--AIA数据标签-------------------------
|
||
null inst_code, --可以移除,报告没有使用
|
||
-------------------------------------
|
||
--XIE HE 数据标签---------------------
|
||
'' as dept_name,
|
||
'' as new_code,
|
||
'' as area,
|
||
'' as h_level,
|
||
'' as reimburse,
|
||
'' as reimburse_type,
|
||
'' as prescription_source,
|
||
-------------------------------------
|
||
'' as CORP_COD --可以移除,报告没有使用
|
||
-------------------------------------
|
||
FROM
|
||
V_CHC_RAW_DATA A
|
||
WHERE
|
||
YYYYMM >= (
|
||
SELECT
|
||
CAST((LEFT(MAX(YYYYMM), 4) - 3) * 100 + 1 AS INT)
|
||
FROM
|
||
V_CHC_RAW_DATA
|
||
)
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,Retail
|
||
-- DELETE FROM
|
||
-- dm.dm_tf_external_sales
|
||
-- WHERE
|
||
-- DATA_SOURCE = 'Retail(Quarterly)';
|
||
|
||
INSERT INTO dm.dm_tf_external_sales (
|
||
YYYYMM,
|
||
PACK_COD,
|
||
AUDIT_COD,
|
||
DATA_SOURCE,
|
||
SALES_UNIT_CAL,
|
||
SALES_UNIT_CAL_LY,
|
||
SALES_VALUE_CAL,
|
||
SALES_VALUE_CAL_LY,
|
||
CONUTING_UNIT,
|
||
CONUTING_UNIT_LY,
|
||
prescription,
|
||
prescription_ly,
|
||
PACK_FLAG,
|
||
PROD_FLAG,
|
||
DTP_FLAG,
|
||
cmps_flag,
|
||
PLATFORM_TYPE,
|
||
STORE_NAME,
|
||
STORE_TYPE,
|
||
REGION_TYPE,
|
||
inst_code,
|
||
dept_name,
|
||
new_code,
|
||
area,
|
||
h_level,
|
||
reimburse,
|
||
reimburse_type,
|
||
prescription_source,
|
||
CORP_COD
|
||
)
|
||
SELECT
|
||
A.YYYYMM,
|
||
A.iqvia_pack_code,
|
||
trim(A.AUDIT_COD) AS AUDIT_COD,
|
||
'Retail(Quarterly)' DATA_SOURCE,
|
||
-------------------------------------
|
||
SUM(sales_unit) SALES_UNIT_CAL,
|
||
SUM(sales_unit_ly) as SALES_UNIT_CAL_LY,
|
||
SUM(sales_value) SALES_VALUE_CAL,
|
||
SUM(sales_value_LY) as SALES_VALUE_CAL_LY,
|
||
SUM(counting_unit) CONUTING_UNIT,
|
||
SUM(counting_unit_LY) as CONUTING_UNIT_LY,
|
||
null as prescription,
|
||
null as prescription_ly,
|
||
-------------------------------------
|
||
--Retail藏数逻辑标签------------------
|
||
A.PACK_FLAG,
|
||
A.brand_flag PROD_FLAG,
|
||
0 DTP_FLAG,
|
||
case
|
||
when
|
||
max(lower(C.CMPS_DES)) in ('atorvastatin', 'rosuvastatin', 'esomeprazole', 'omeprazole')
|
||
then
|
||
1
|
||
else 0
|
||
end as cmps_flag,
|
||
-------------------------------------
|
||
--EC数据标签--------------------------
|
||
'' PLATFORM_TYPE,
|
||
'' STORE_NAME,
|
||
'' STORE_TYPE,
|
||
-------------------------------------
|
||
--COUNTY数据标签----------------------
|
||
'' REGION_TYPE,
|
||
-------------------------------------
|
||
--AIA数据标签-------------------------
|
||
null inst_code, --可以移除,报告没有使用
|
||
-------------------------------------
|
||
--XIE HE 数据标签---------------------
|
||
'' as dept_name,
|
||
'' as new_code,
|
||
'' as area,
|
||
'' as h_level,
|
||
'' as reimburse,
|
||
'' as reimburse_type,
|
||
'' as prescription_source,
|
||
-------------------------------------
|
||
'' as CORP_COD --可以移除,报告没有使用
|
||
-------------------------------------
|
||
FROM
|
||
dm.dm_zk_retail_sales A
|
||
LEFT JOIN (
|
||
select DISTINCT
|
||
iqvia_pack_code,
|
||
CMPS_DES
|
||
from
|
||
dm.dm_zk_retail_pack_property
|
||
) C
|
||
ON A.iqvia_pack_code = C.iqvia_pack_code
|
||
-------------------------------------
|
||
LEFT JOIN (SELECT OTHERS_MONTH FROM tmp.tmp_dm_td_ext_retail_niad_month) D
|
||
-------------------------------------
|
||
where
|
||
A.YYYYMM >= '202001' AND A.YYYYMM <= D.OTHERS_MONTH
|
||
GROUP BY
|
||
A.YYYYMM,
|
||
A.iqvia_pack_code,
|
||
A.AUDIT_COD,
|
||
A.PACK_FLAG,
|
||
A.brand_flag
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,DTP
|
||
-- DELETE FROM
|
||
-- dm.dm_tf_external_sales
|
||
-- WHERE
|
||
-- DATA_SOURCE = 'DTP(Quarterly)';
|
||
|
||
INSERT INTO dm.dm_tf_external_sales (
|
||
YYYYMM,
|
||
PACK_COD,
|
||
AUDIT_COD,
|
||
DATA_SOURCE,
|
||
SALES_UNIT_CAL,
|
||
SALES_UNIT_CAL_LY,
|
||
SALES_VALUE_CAL,
|
||
SALES_VALUE_CAL_LY,
|
||
CONUTING_UNIT,
|
||
CONUTING_UNIT_LY,
|
||
prescription,
|
||
prescription_ly,
|
||
PACK_FLAG,
|
||
PROD_FLAG,
|
||
DTP_FLAG,
|
||
cmps_flag,
|
||
PLATFORM_TYPE,
|
||
STORE_NAME,
|
||
STORE_TYPE,
|
||
REGION_TYPE,
|
||
inst_code,
|
||
dept_name,
|
||
new_code,
|
||
area,
|
||
h_level,
|
||
reimburse,
|
||
reimburse_type,
|
||
prescription_source,
|
||
CORP_COD
|
||
)
|
||
SELECT
|
||
A.YYYYMM,
|
||
A.iqvia_pack_code,
|
||
trim(A.AUDIT_COD) AS AUDIT_COD,
|
||
'DTP(Quarterly)' DATA_SOURCE,
|
||
-------------------------------------
|
||
SUM(sales_unit) SALES_UNIT_CAL,
|
||
SUM(sales_unit_ly) as SALES_UNIT_CAL_LY,
|
||
SUM(sales_value) SALES_VALUE_CAL,
|
||
SUM(sales_value_LY) as SALES_VALUE_CAL_LY,
|
||
SUM(counting_unit) CONUTING_UNIT,
|
||
SUM(counting_unit_LY) as CONUTING_UNIT_LY,
|
||
null as prescription,
|
||
null as prescription_ly,
|
||
-------------------------------------
|
||
--Retail藏数逻辑标签------------------
|
||
1 PACK_FLAG,
|
||
brand_flag AS PROD_FLAG,
|
||
1 DTP_FLAG,
|
||
null as cmps_flag,
|
||
-------------------------------------
|
||
--EC数据标签--------------------------
|
||
'' PLATFORM_TYPE,
|
||
'' STORE_NAME,
|
||
'' STORE_TYPE,
|
||
-------------------------------------
|
||
--COUNTY数据标签----------------------
|
||
'' REGION_TYPE,
|
||
-------------------------------------
|
||
--AIA数据标签-------------------------
|
||
null inst_code, --可以移除,报告没有使用
|
||
-------------------------------------
|
||
--XIE HE 数据标签---------------------
|
||
'' as dept_name,
|
||
'' as new_code,
|
||
'' as area,
|
||
'' as h_level,
|
||
'' as reimburse,
|
||
'' as reimburse_type,
|
||
'' as prescription_source,
|
||
-------------------------------------
|
||
'' as CORP_COD --可以移除,报告没有使用
|
||
-------------------------------------
|
||
FROM
|
||
dm.dm_zk_retail_dtp_sales A
|
||
where
|
||
A.YYYYMM >= '202001'
|
||
GROUP BY
|
||
A.YYYYMM,
|
||
A.iqvia_pack_code,
|
||
A.AUDIT_COD,
|
||
A.brand_flag,
|
||
A.pack_flag
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,EC
|
||
-- DELETE FROM
|
||
-- dm.dm_tf_external_sales
|
||
-- WHERE
|
||
-- DATA_SOURCE = 'EC(Monthly)';
|
||
|
||
INSERT INTO dm.dm_tf_external_sales (
|
||
YYYYMM,
|
||
PACK_COD,
|
||
AUDIT_COD,
|
||
DATA_SOURCE,
|
||
SALES_UNIT_CAL,
|
||
SALES_UNIT_CAL_LY,
|
||
SALES_VALUE_CAL,
|
||
SALES_VALUE_CAL_LY,
|
||
CONUTING_UNIT,
|
||
CONUTING_UNIT_LY,
|
||
prescription,
|
||
prescription_ly,
|
||
PACK_FLAG,
|
||
PROD_FLAG,
|
||
DTP_FLAG,
|
||
cmps_flag,
|
||
PLATFORM_TYPE,
|
||
STORE_NAME,
|
||
STORE_TYPE,
|
||
REGION_TYPE,
|
||
inst_code,
|
||
dept_name,
|
||
new_code,
|
||
area,
|
||
h_level,
|
||
reimburse,
|
||
reimburse_type,
|
||
prescription_source,
|
||
CORP_COD
|
||
)
|
||
SELECT
|
||
A.YYYYMM,
|
||
A.iqvia_pack_code,
|
||
A.AUDIT_COD,
|
||
'EC(Monthly)' DATA_SOURCE,
|
||
-------------------------------------
|
||
SUM(sales_unit) SALES_UNIT_CAL,
|
||
SUM(sales_unit_ly) SALES_UNIT_CAL_LY,
|
||
SUM(sales_value) SALES_VALUE_CAL,
|
||
SUM(sales_value_LY) SALES_VALUE_CAL_LY,
|
||
SUM(counting_unit) CONUTING_UNIT,
|
||
SUM(counting_unit_LY) CONUTING_UNIT_LY,
|
||
null as prescription,
|
||
null as prescription_ly,
|
||
-------------------------------------
|
||
--Retail藏数逻辑标签------------------
|
||
1 PACK_FLAG,
|
||
1 PROD_FLAG,
|
||
0 DTP_FLAG,
|
||
null as cmps_flag,
|
||
-------------------------------------
|
||
--EC数据标签--------------------------
|
||
A.PLATFORM,
|
||
A.STORE_NAME,
|
||
A.STORE_TYPE,
|
||
-------------------------------------
|
||
--COUNTY数据标签----------------------
|
||
'' REGION_TYPE,
|
||
-------------------------------------
|
||
--AIA数据标签-------------------------
|
||
null inst_code, --可以移除,报告没有使用
|
||
-------------------------------------
|
||
--XIE HE 数据标签---------------------
|
||
'' as dept_name,
|
||
'' as new_code,
|
||
'' as area,
|
||
'' as h_level,
|
||
'' as reimburse,
|
||
'' as reimburse_type,
|
||
'' as prescription_source,
|
||
-------------------------------------
|
||
'' as CORP_COD --可以移除,报告没有使用
|
||
-------------------------------------
|
||
FROM
|
||
dm.dm_zk_ec_sales A
|
||
LEFT JOIN (SELECT OTHERS_MONTH FROM tmp.tmp_dm_td_ext_ec_niad_month) D
|
||
where
|
||
A.YYYYMM >= '202001' and A.YYYYMM<=D.others_month
|
||
GROUP BY
|
||
A.YYYYMM,
|
||
A.iqvia_pack_code,
|
||
A.AUDIT_COD,
|
||
A.PLATFORM,
|
||
A.STORE_NAME,
|
||
A.STORE_TYPE,
|
||
A.PACK_FLAG,
|
||
A.PROD_FLAG
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,AIA
|
||
-- DELETE FROM
|
||
-- dm.dm_tf_external_sales
|
||
-- WHERE
|
||
-- DATA_SOURCE = 'AIA(Monthly)';
|
||
|
||
INSERT INTO dm.dm_tf_external_sales (
|
||
YYYYMM,
|
||
PACK_COD,
|
||
AUDIT_COD,
|
||
DATA_SOURCE,
|
||
SALES_UNIT_CAL,
|
||
SALES_UNIT_CAL_LY,
|
||
SALES_VALUE_CAL,
|
||
SALES_VALUE_CAL_LY,
|
||
CONUTING_UNIT,
|
||
CONUTING_UNIT_LY,
|
||
prescription,
|
||
prescription_ly,
|
||
PACK_FLAG,
|
||
PROD_FLAG,
|
||
DTP_FLAG,
|
||
cmps_flag,
|
||
PLATFORM_TYPE,
|
||
STORE_NAME,
|
||
STORE_TYPE,
|
||
REGION_TYPE,
|
||
inst_code,
|
||
dept_name,
|
||
new_code,
|
||
area,
|
||
h_level,
|
||
reimburse,
|
||
reimburse_type,
|
||
prescription_source,
|
||
CORP_COD
|
||
)
|
||
SELECT
|
||
A.ym,
|
||
A.PACK_COD,
|
||
A.AUDIT_COD,
|
||
'AIA(Monthly)' DATA_SOURCE,
|
||
-------------------------------------
|
||
sum(volume) as SALES_UNIT_CAL,
|
||
sum(volume_ly) as SALES_UNIT_CAL_LY,
|
||
sum(value) as SALES_VALUE_CAL,
|
||
sum(value_ly) as SALES_VALUE_CAL_LY,
|
||
sum(counting_unit) as CONUTING_UNIT,
|
||
sum(counting_unit_ly) as CONUTING_UNIT_LY,
|
||
null as prescription,
|
||
null as prescription_ly,
|
||
-------------------------------------
|
||
--Retail藏数逻辑标签------------------
|
||
1 PACK_FLAG,
|
||
1 PROD_FLAG,
|
||
0 DTP_FLAG,
|
||
null as cmps_flag,
|
||
-------------------------------------
|
||
--EC数据标签--------------------------
|
||
'' PLATFORM_TYPE,
|
||
'' STORE_NAME,
|
||
'' STORE_TYPE,
|
||
-------------------------------------
|
||
--COUNTY数据标签----------------------
|
||
'' REGION_TYPE,
|
||
-------------------------------------
|
||
--AIA数据标签-------------------------
|
||
inst_code AS inst_code, --可以移除,报告没有使用
|
||
-------------------------------------
|
||
--XIE HE 数据标签---------------------
|
||
'' as dept_name,
|
||
'' as new_code,
|
||
'' as area,
|
||
'' as h_level,
|
||
'' as reimburse,
|
||
'' as reimburse_type,
|
||
'' as prescription_source,
|
||
-------------------------------------
|
||
'' as CORP_COD --可以移除,报告没有使用
|
||
-------------------------------------
|
||
from
|
||
dm.dm_ext_aia_sales A
|
||
--取202401年之后的数
|
||
where
|
||
ym >= '202401'
|
||
group by
|
||
ym,
|
||
pack_cod,
|
||
corp_cod,
|
||
AUDIT_COD,
|
||
inst_code
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,XIEHE
|
||
-- DELETE FROM
|
||
-- dm.dm_tf_external_sales
|
||
-- WHERE
|
||
-- DATA_SOURCE = 'XH Data(Quarterly)';
|
||
|
||
INSERT INTO dm.dm_tf_external_sales (
|
||
YYYYMM,
|
||
PACK_COD,
|
||
AUDIT_COD,
|
||
DATA_SOURCE,
|
||
SALES_UNIT_CAL,
|
||
SALES_UNIT_CAL_LY,
|
||
SALES_VALUE_CAL,
|
||
SALES_VALUE_CAL_LY,
|
||
CONUTING_UNIT,
|
||
CONUTING_UNIT_LY,
|
||
prescription,
|
||
prescription_ly,
|
||
PACK_FLAG,
|
||
PROD_FLAG,
|
||
DTP_FLAG,
|
||
cmps_flag,
|
||
PLATFORM_TYPE,
|
||
STORE_NAME,
|
||
STORE_TYPE,
|
||
REGION_TYPE,
|
||
inst_code,
|
||
dept_name,
|
||
new_code,
|
||
area,
|
||
h_level,
|
||
reimburse,
|
||
reimburse_type,
|
||
prescription_source,
|
||
CORP_COD
|
||
)
|
||
SELECT
|
||
A.YYYYMM,
|
||
A.iqvia_pack_code,
|
||
A.AUDIT_COD,
|
||
'XH Data(Quarterly)' DATA_SOURCE,
|
||
-------------------------------------
|
||
0 SALES_UNIT_CAL,
|
||
0 SALES_UNIT_CAL_LY,
|
||
SUM(sales_value) SALES_VALUE_CAL,
|
||
SUM(sales_value_LY) SALES_VALUE_CAL_LY,
|
||
SUM(A.counting_unit) CONUTING_UNIT,
|
||
SUM(A.counting_unit_ly) CONUTING_UNIT_LY,
|
||
null as prescription,
|
||
null as prescription_ly,
|
||
-------------------------------------
|
||
--Retail藏数逻辑标签------------------
|
||
1 PACK_FLAG,
|
||
1 PROD_FLAG,
|
||
0 DTP_FLAG,
|
||
null as cmps_flag,
|
||
-------------------------------------
|
||
--EC数据标签--------------------------
|
||
'' PLATFORM_TYPE,
|
||
'' STORE_NAME,
|
||
'' STORE_TYPE,
|
||
-------------------------------------
|
||
--COUNTY数据标签----------------------
|
||
'' REGION_TYPE,
|
||
-------------------------------------
|
||
--AIA数据标签-------------------------
|
||
null inst_code, --可以移除,报告没有使用
|
||
-------------------------------------
|
||
--XIE HE 数据标签---------------------
|
||
NVL(A.dept_name, '') dept_name,
|
||
NVL(A.new_code, '') new_code,
|
||
NVL(A.province, '') area,
|
||
NVL(A.h_level, '') h_level,
|
||
NVL(A.reimburse, '') reimburse,
|
||
NVL(A.reimburse_type, '') reimburse_type,
|
||
NVL(A.prescription_source, '') prescription_source,
|
||
-------------------------------------
|
||
'' as CORP_COD --可以移除,报告没有使用
|
||
-------------------------------------
|
||
FROM
|
||
dm.dm_ext_xiehe_sales A
|
||
WHERE
|
||
A.YYYYMM >= '202001'
|
||
GROUP BY
|
||
A.YYYYMM,
|
||
A.iqvia_pack_code,
|
||
A.AUDIT_COD,
|
||
A.dept_name,
|
||
A.new_code,
|
||
A.province,
|
||
A.h_level,
|
||
A.reimburse,
|
||
A.reimburse_type,
|
||
A.prescription_source
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- MAGIC %md
|
||
-- MAGIC ## 特殊逻辑
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,更新pack code
|
||
update dm.dm_tf_external_sales set PACK_COD = concat('PACK_COD_',DATA_SOURCE)
|
||
where PACK_COD is null or PACK_COD = '';
|
||
update dm.dm_tf_external_sales set AUDIT_COD = 'ROC'
|
||
where AUDIT_COD is null or AUDIT_COD = '';
|
||
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- MAGIC %md
|
||
-- MAGIC ## NIAD单独页面数据
|
||
|
||
-- COMMAND ----------
|
||
|
||
update dm.dm_zk_retail_sales set DATA_SOURCE = 'Retail(Quarterly)'
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- DBTITLE 1,NIAD
|
||
-- =============================================================================
|
||
-- 脚本名称: dm.dm_tf_external_sales_niad 数据装载脚本
|
||
-- 功能描述: 将零售和电商的销售数据(仅限NIAD市场)插入目标表
|
||
-- 数据来源: dm.dm_zk_retail_sales, dm.dm_zk_ec_sales, dm.dm_td_external_market_pack_mapping_temp
|
||
-- 目标表: dm.dm_tf_external_sales_niad
|
||
-- 更新方式: INSERT OVERWRITE(全量覆盖)
|
||
-- 创建时间: 2026-03-09
|
||
-- =============================================================================
|
||
|
||
INSERT OVERWRITE TABLE dm.dm_tf_external_sales_niad
|
||
(
|
||
YYYYMM, -- 年月分区键
|
||
PACK_COD, -- 药品包装代码
|
||
AUDIT_COD, -- 审计代码
|
||
SALES_UNIT_CAL, -- 本期销量(单位)
|
||
SALES_UNIT_CAL_LY, -- 去年同期销量(单位)
|
||
SALES_VALUE_CAL, -- 本期销售额
|
||
SALES_VALUE_CAL_LY, -- 去年同期销售额
|
||
CONUTING_UNIT, -- 计数单位(本期)
|
||
CONUTING_UNIT_LY, -- 计数单位(去年同期)
|
||
DATA_SOURCE, -- 数据来源(零售/电商)
|
||
DATE_KEY, -- 日期键(转换为日期类型)
|
||
PACK_SOURCE, -- 包装来源唯一标识
|
||
AUDIT_SOURCE -- 审计来源唯一标识
|
||
)
|
||
|
||
WITH mkt_pack AS (
|
||
SELECT DISTINCT
|
||
PACK_COD,
|
||
DATA_SOURCE
|
||
FROM dm.dm_td_external_market_pack_mapping_temp
|
||
WHERE DATA_SOURCE IN ('Retail(Quarterly)', 'EC(Monthly)') -- 仅限零售和电商
|
||
AND MARKET = 'NIAD (Non-Insulin Anti-Diabetic) Market' -- NIAD市场过滤
|
||
),
|
||
|
||
sales AS (
|
||
-- 零售数据(季度)
|
||
SELECT
|
||
YYYYMM,
|
||
iqvia_pack_code AS PACK_COD,
|
||
AUDIT_COD,
|
||
sales_unit AS SALES_UNIT_CAL,
|
||
sales_unit_ly AS SALES_UNIT_CAL_LY,
|
||
sales_value AS SALES_VALUE_CAL,
|
||
sales_value_ly AS SALES_VALUE_CAL_LY,
|
||
counting_unit AS CONUTING_UNIT,
|
||
counting_unit_ly AS CONUTING_UNIT_LY,
|
||
DATA_SOURCE,
|
||
-- 将日期转换放在CTE中统一处理,避免重复解析
|
||
TO_DATE(YYYYMM, 'yyyyMM') AS DATE_KEY,
|
||
CONCAT(iqvia_pack_code, DATA_SOURCE) AS PACK_SOURCE,
|
||
CONCAT(AUDIT_COD, DATA_SOURCE) AS AUDIT_SOURCE
|
||
FROM dm.dm_zk_retail_sales
|
||
WHERE YYYYMM >= '202001' -- 仅保留2020年及以后数据
|
||
|
||
UNION ALL
|
||
|
||
-- 电商数据(月度)
|
||
SELECT
|
||
YYYYMM,
|
||
iqvia_pack_code AS PACK_COD,
|
||
AUDIT_COD,
|
||
sales_unit AS SALES_UNIT_CAL,
|
||
sales_unit_ly AS SALES_UNIT_CAL_LY,
|
||
sales_value AS SALES_VALUE_CAL,
|
||
sales_value_ly AS SALES_VALUE_CAL_LY,
|
||
counting_unit AS CONUTING_UNIT,
|
||
counting_unit_ly AS CONUTING_UNIT_LY,
|
||
DATA_SOURCE,
|
||
TO_DATE(YYYYMM, 'yyyyMM') AS DATE_KEY,
|
||
CONCAT(iqvia_pack_code, DATA_SOURCE) AS PACK_SOURCE,
|
||
CONCAT(AUDIT_COD, DATA_SOURCE) AS AUDIT_SOURCE
|
||
FROM dm.dm_zk_ec_sales
|
||
WHERE YYYYMM >= '202001' -- 仅保留2020年及以后数据
|
||
)
|
||
SELECT
|
||
a.YYYYMM,
|
||
a.PACK_COD,
|
||
a.AUDIT_COD,
|
||
a.SALES_UNIT_CAL,
|
||
a.SALES_UNIT_CAL_LY,
|
||
a.SALES_VALUE_CAL,
|
||
a.SALES_VALUE_CAL_LY,
|
||
a.CONUTING_UNIT,
|
||
a.CONUTING_UNIT_LY,
|
||
a.DATA_SOURCE,
|
||
a.DATE_KEY,
|
||
a.PACK_SOURCE,
|
||
a.AUDIT_SOURCE
|
||
FROM sales a
|
||
INNER JOIN mkt_pack b
|
||
ON a.PACK_COD = b.PACK_COD
|
||
AND a.DATA_SOURCE = b.DATA_SOURCE; |