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"
This commit is contained in:
355
AIA/01 dm_aia_pack_property.sql
Normal file
355
AIA/01 dm_aia_pack_property.sql
Normal file
@@ -0,0 +1,355 @@
|
||||
-- Databricks notebook source
|
||||
-- CREATE OR REPLACE TABLE dm.dm_aia_pack_property (
|
||||
-- PACK_COD STRING,
|
||||
-- pack_des STRING,
|
||||
-- stgh_des STRING,
|
||||
-- pack_lch STRING,
|
||||
-- PROD_COD STRING,
|
||||
-- prod_des STRING,
|
||||
-- prod_des_c STRING,
|
||||
-- Family_Code STRING,
|
||||
-- Family_Name STRING,
|
||||
-- cmps_cod STRING,
|
||||
-- cmps_des STRING,
|
||||
-- cmps_des_c STRING,
|
||||
-- atc1_cod STRING,
|
||||
-- atc1_des STRING,
|
||||
-- atc1_des_c STRING,
|
||||
-- atc2_cod STRING,
|
||||
-- atc2_des STRING,
|
||||
-- atc2_des_c STRING,
|
||||
-- atc3_cod STRING,
|
||||
-- atc3_des STRING,
|
||||
-- atc3_des_c STRING,
|
||||
-- atc4_cod STRING,
|
||||
-- atc4_des STRING,
|
||||
-- atc4_des_c STRING,
|
||||
-- app1_cod STRING,
|
||||
-- app1_des STRING,
|
||||
-- app1_des_c STRING,
|
||||
-- app2_cod STRING,
|
||||
-- app2_des STRING,
|
||||
-- app2_des_c STRING,
|
||||
-- app3_cod STRING,
|
||||
-- app3_des STRING,
|
||||
-- app3_des_c STRING,
|
||||
-- bio_desc STRING,
|
||||
-- gene_orig_desc STRING,
|
||||
-- eth_otc_desc STRING,
|
||||
-- nrdl_desc STRING,
|
||||
-- NRDL_Entry_Date STRING,
|
||||
-- edl_desc STRING,
|
||||
-- tcm_desc STRING,
|
||||
-- paed_desc STRING,
|
||||
-- gqce_desc STRING,
|
||||
-- vbp_desc STRING,
|
||||
-- manu_cod STRING,
|
||||
-- manu_des STRING,
|
||||
-- manu_des_c STRING,
|
||||
-- mnfl_cod STRING,
|
||||
-- mnfl_des STRING,
|
||||
-- corp_cod STRING,
|
||||
-- corp_des STRING,
|
||||
-- corp_des_c STRING,
|
||||
-- BrandType STRING,
|
||||
-- IS_AZ STRING,
|
||||
-- AZ_MAIN STRING,
|
||||
-- AZ_Related STRING,
|
||||
-- COUNTING_UNIT STRING,
|
||||
-- ETL_INSERT_DT TIMESTAMP,
|
||||
-- ETL_UPDATE_DT TIMESTAMP)
|
||||
-- USING delta
|
||||
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_aia_pack_property';
|
||||
-- -- 上面是生产环境location,下面是测试环境location
|
||||
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_aia_pack_property';
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
--对用户上传的表进行去重处理
|
||||
--对部分编码字段进行补0
|
||||
create or replace temporary view aia_pack_property_distinct as
|
||||
select distinct
|
||||
iqvia_lineno,
|
||||
case
|
||||
when
|
||||
length(trim(iqvia_pack_code)) < 12
|
||||
and trim(iqvia_pack_code) REGEXP '^[0-9]'
|
||||
then
|
||||
right(concat('000000000000', trim(iqvia_pack_code)), 12)
|
||||
else trim(iqvia_pack_code)
|
||||
end as iqvia_pack_code,
|
||||
case
|
||||
when length(iqvia_prod_code) < 9 then right(concat('000000000', iqvia_prod_code), 9)
|
||||
else iqvia_prod_code
|
||||
end as iqvia_prod_code,
|
||||
iqvia_notes,
|
||||
IS_NEW_PRD,
|
||||
IS_IN_CHPA,
|
||||
ATC1,
|
||||
ATC2,
|
||||
ATC3,
|
||||
ATC4,
|
||||
org_prd_cd,
|
||||
org_mole_nm_c,
|
||||
org_manu_prd_nm_c,
|
||||
org_prd_nm_c,
|
||||
org_pk,
|
||||
org_prd_str,
|
||||
org_pth,
|
||||
min_pk_unit,
|
||||
org_pk_unit,
|
||||
org_manu_nm,
|
||||
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,
|
||||
case
|
||||
when length(cmps_cod) < 6 then right(concat('000000', cmps_cod), 6)
|
||||
else cmps_cod
|
||||
end as 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,
|
||||
unit,
|
||||
case
|
||||
when
|
||||
counting_unit is null
|
||||
and org_pk_unit is null
|
||||
then
|
||||
1
|
||||
when counting_unit is null then cast(org_pk_unit as decimal(30, 10))
|
||||
else cast(counting_unit / coalesce(unit, 1) as decimal(30, 10))
|
||||
end as counting_unit,
|
||||
dosage_unit
|
||||
from
|
||||
dwd.dwd_gnd_ext_aia_cpt_data;
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- create or replace table dm.dm_aia_pack_property
|
||||
insert overwrite table dm.dm_aia_pack_property
|
||||
WITH dedup_l AS (
|
||||
SELECT *,
|
||||
ROW_NUMBER() OVER (PARTITION BY iqvia_pack_code ORDER BY iqvia_pack_code) AS rn
|
||||
FROM aia_pack_property_distinct
|
||||
QUALIFY rn = 1
|
||||
),
|
||||
dedup_r AS (
|
||||
SELECT *,
|
||||
ROW_NUMBER() OVER (PARTITION BY PACK_COD ORDER BY PACK_COD) AS rn
|
||||
FROM tmp.tmp_ext_chpa_pack_propetry
|
||||
QUALIFY rn = 1
|
||||
)
|
||||
SELECT
|
||||
l.iqvia_pack_code AS PACK_COD,
|
||||
|
||||
-- Pack / Product 描述
|
||||
COALESCE(r.PACK_DES, l.pack_des) AS pack_des,
|
||||
COALESCE(r.STGH_DES, l.stgh_des) AS stgh_des,
|
||||
COALESCE(r.PACK_LCH, l.pack_lch) AS pack_lch,
|
||||
COALESCE(r.PROD_COD, l.iqvia_prod_code) AS PROD_COD,
|
||||
COALESCE(r.PROD_DES, l.prod_des) AS prod_des,
|
||||
COALESCE(r.PROD_DES_C, l.prod_des_c) AS prod_des_c,
|
||||
|
||||
-- 右表独有:Family
|
||||
r.Family_Code,
|
||||
r.Family_Name,
|
||||
|
||||
-- 成分
|
||||
COALESCE(r.CMPS_COD, l.cmps_cod) AS cmps_cod,
|
||||
COALESCE(r.CMPS_DES, l.cmps_des) AS cmps_des,
|
||||
COALESCE(r.CMPS_DES_C, l.cmps_des_c) AS cmps_des_c,
|
||||
|
||||
-- ATC
|
||||
COALESCE(r.ATC1_COD, l.atc1_cod) AS atc1_cod,
|
||||
COALESCE(r.atc1_des, l.atc1_des) AS atc1_des,
|
||||
COALESCE(r.atc1_des_c, l.atc1_des_c) AS atc1_des_c,
|
||||
COALESCE(r.ATC2_COD, l.atc2_cod) AS atc2_cod,
|
||||
COALESCE(r.atc2_des, l.atc2_des) AS atc2_des,
|
||||
COALESCE(r.atc2_des_c, l.atc2_des_c) AS atc2_des_c,
|
||||
COALESCE(r.ATC3_COD, l.atc3_cod) AS atc3_cod,
|
||||
COALESCE(r.atc3_des, l.atc3_des) AS atc3_des,
|
||||
COALESCE(r.atc3_des_c, l.atc3_des_c) AS atc3_des_c,
|
||||
COALESCE(r.ATC4_COD, l.atc4_cod) AS atc4_cod,
|
||||
COALESCE(r.atc4_des, l.atc4_des) AS atc4_des,
|
||||
COALESCE(r.atc4_des_c, l.atc4_des_c) AS atc4_des_c,
|
||||
|
||||
-- APP
|
||||
COALESCE(r.APP1_COD, l.app1_cod) AS app1_cod,
|
||||
COALESCE(r.app1_des, l.app1_des) AS app1_des,
|
||||
COALESCE(r.app1_des_c, l.app1_des_c) AS app1_des_c,
|
||||
COALESCE(r.APP2_COD, l.app2_cod) AS app2_cod,
|
||||
COALESCE(r.app2_des, l.app2_des) AS app2_des,
|
||||
COALESCE(r.app2_des_c, l.app2_des_c) AS app2_des_c,
|
||||
COALESCE(r.APP3_COD, l.app3_cod) AS app3_cod,
|
||||
COALESCE(r.app3_des, l.app3_des) AS app3_des,
|
||||
COALESCE(r.app3_des_c, l.app3_des_c) AS app3_des_c,
|
||||
|
||||
-- 标志位
|
||||
COALESCE(r.BIO_DESC, l.bio_desc) AS bio_desc,
|
||||
COALESCE(r.GENE_ORIG_DESC, l.gene_orig_desc) AS gene_orig_desc,
|
||||
COALESCE(r.ETH_OTC_DESC, l.eth_otc_desc) AS eth_otc_desc,
|
||||
COALESCE(r.NRDL_DESC, l.nrdl_desc) AS nrdl_desc,
|
||||
r.NRDL_Entry_Date,
|
||||
COALESCE(r.EDL_DESC, l.edl_desc) AS edl_desc,
|
||||
COALESCE(r.TCM_DESC, l.tcm_desc) AS tcm_desc,
|
||||
COALESCE(r.PAED_DESC, l.paed_desc) AS paed_desc,
|
||||
COALESCE(r.GQCE_DESC, l.gqce_desc) AS gqce_desc,
|
||||
COALESCE(r.VBP_DESC, l.vbp_desc) AS vbp_desc,
|
||||
|
||||
-- 厂商
|
||||
COALESCE(r.MANU_COD, l.manu_cod) AS manu_cod,
|
||||
COALESCE(r.MANU_DES, l.manu_des) AS manu_des,
|
||||
COALESCE(r.MANU_DES_C, l.manu_des_c) AS manu_des_c,
|
||||
COALESCE(r.MNFL_COD, l.mnfl_cod) AS mnfl_cod,
|
||||
COALESCE(r.MNFL_DES, l.mnfl_des) AS mnfl_des,
|
||||
|
||||
-- 公司
|
||||
COALESCE(r.CORP_COD, l.corp_cod) AS corp_cod,
|
||||
COALESCE(r.CORP_DES, l.corp_des) AS corp_des,
|
||||
COALESCE(r.CORP_DES_C, l.corp_des_c) AS corp_des_c,
|
||||
|
||||
-- 右表独有:品牌 / AZ 标志
|
||||
r.BrandType,
|
||||
r.IS_AZ,
|
||||
r.AZ_MAIN,
|
||||
r.AZ_Related,
|
||||
-- 左表独有
|
||||
l.COUNTING_UNIT,
|
||||
from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_INSERT_DT,
|
||||
from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_UPDATE_DT
|
||||
FROM dedup_l l
|
||||
LEFT JOIN dedup_r r
|
||||
ON l.iqvia_pack_code = r.PACK_COD
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- -----------------------------------------------------------
|
||||
-- --修改时间:20241118
|
||||
-- --修改人:Fanxujia
|
||||
-- --修改内容:
|
||||
-- --使用手工文件排除掉每个渠道的不需要显示的市场
|
||||
-- --由于AIA涉及后面的Flag逻辑、NEW AIA Dashboard,因此不能直接在最后的DM表进行限制,需要在这里限制。
|
||||
-- -----------------------------------------------------------
|
||||
-- with hidden_market as (
|
||||
-- select distinct
|
||||
-- upper(hidden_market) as hidden_market
|
||||
-- from dwd.dwd_gnd_ims_hidden_market
|
||||
-- where upper(data_source) = 'AIA'
|
||||
-- )
|
||||
-- ,tmp as (
|
||||
-- select
|
||||
-- MARKET_PACK_KEY,
|
||||
-- PACK_COD,
|
||||
-- PACK_DES,
|
||||
-- STGH_DES,
|
||||
-- PACK_LCH,
|
||||
-- Family_Code,
|
||||
-- Family_Name,
|
||||
-- PROD_COD,
|
||||
-- PROD_DES,
|
||||
-- PROD_DES_C,
|
||||
-- CMPS_COD,
|
||||
-- CMPS_DES,
|
||||
-- CMPS_DES_C,
|
||||
-- ATC1_COD,
|
||||
-- ATC2_COD,
|
||||
-- ATC3_COD,
|
||||
-- ATC4_COD,
|
||||
-- APP1_COD,
|
||||
-- APP2_COD,
|
||||
-- APP3_COD,
|
||||
-- BIO_DESC,
|
||||
-- GENE_ORIG_DESC,
|
||||
-- ETH_OTC_DESC,
|
||||
-- NRDL_DESC,
|
||||
-- NRDL_Entry_Date,
|
||||
-- EDL_DESC,
|
||||
-- TCM_DESC,
|
||||
-- PAED_DESC,
|
||||
-- GQCE_DESC,
|
||||
-- VBP_DESC,
|
||||
-- MANU_COD,
|
||||
-- MANU_DES,
|
||||
-- MANU_DES_C,
|
||||
-- MNFL_COD,
|
||||
-- MNFL_DES,
|
||||
-- CORP_COD,
|
||||
-- CORP_DES,
|
||||
-- CORP_DES_C,
|
||||
-- BrandType,
|
||||
-- t1.MARKET,
|
||||
-- t1.MARKET_CHPA,
|
||||
-- KEY_COMPETITOR,
|
||||
-- IS_AZ,
|
||||
-- AZ_MAIN,
|
||||
-- AZ_Related,
|
||||
-- atc1_des,
|
||||
-- atc1_des_c,
|
||||
-- atc2_des,
|
||||
-- atc2_des_c,
|
||||
-- atc3_des,
|
||||
-- atc3_des_c,
|
||||
-- atc4_des,
|
||||
-- atc4_des_c,
|
||||
-- app1_des,
|
||||
-- app1_des_c,
|
||||
-- app2_des,
|
||||
-- app2_des_c,
|
||||
-- app3_des,
|
||||
-- app3_des_c,
|
||||
-- Class,
|
||||
-- ETL_INSERT_DT,
|
||||
-- ETL_UPDATE_DT,
|
||||
-- market_ratio,
|
||||
-- counting_unit
|
||||
-- from dm.dm_aia_pack_property t1
|
||||
-- left join hidden_market t3
|
||||
-- on upper(t1.MARKET) = t3.hidden_market
|
||||
-- where t3.hidden_market is null
|
||||
-- union
|
||||
-- select *
|
||||
-- from dm.dm_aia_pack_property
|
||||
-- where MARKET = 'AIA ALL Market'
|
||||
-- )
|
||||
|
||||
-- insert overwrite dm.dm_aia_pack_property
|
||||
-- select * from tmp
|
||||
710
AIA/02 dm_ext_aia_sales.sql
Normal file
710
AIA/02 dm_ext_aia_sales.sql
Normal file
@@ -0,0 +1,710 @@
|
||||
-- Databricks notebook source
|
||||
-- CREATE OR REPLACE TABLE DM.DM_TF_EXT_AIA_SALES (
|
||||
-- YYYYMM STRING,
|
||||
-- PACK_CODE STRING,
|
||||
-- CORP_CODE STRING,
|
||||
-- AUDIT_CODE STRING,
|
||||
-- PLATFORM_TYPE STRING,
|
||||
-- STORE_NAME STRING,
|
||||
-- STORE_TYPE STRING,
|
||||
-- REGION_TYPE STRING,
|
||||
-- PACK_FLAG INT,
|
||||
-- PROD_FLAG INT,
|
||||
-- DTP_FLAG INT,
|
||||
-- SALES_UNIT_CAL DECIMAL(38,10),
|
||||
-- SALES_UNIT_CAL_LY DECIMAL(38,10),
|
||||
-- SALES_VALUE_CAL DECIMAL(38,10),
|
||||
-- SALES_VALUE_CAL_LY DECIMAL(38,10),
|
||||
-- CONUTING_UNIT DECIMAL(38,10),
|
||||
-- CONUTING_UNIT_LY DECIMAL(38,10),
|
||||
-- DATA_SOURCE STRING,
|
||||
-- INST_CODE STRING COMMENT '内部机构编码',
|
||||
-- CMPS_FLAG STRING COMMENT '分子式标签',
|
||||
-- DEPT_NAME STRING COMMENT '科室名称',
|
||||
-- PRESCRIPTION DECIMAL(38,10) COMMENT '处方张数',
|
||||
-- PRESCRIPTION_LY DECIMAL(38,10) COMMENT '去年同期处方张数',
|
||||
-- NEW_CODE STRING COMMENT '主数据关联CODE',
|
||||
-- AREA STRING COMMENT '城市',
|
||||
-- H_LEVEL STRING COMMENT '医院类型',
|
||||
-- REIMBURSE STRING COMMENT '报销情况',
|
||||
-- REIMBURSE_TYPE STRING COMMENT '报销类型',
|
||||
-- PRESCRIPTION_SOURCE STRING COMMENT '处方来源',
|
||||
-- ETL_INSERT_DT TIMESTAMP,
|
||||
-- ETL_UPDATE_DT TIMESTAMP
|
||||
-- )
|
||||
-- USING delta
|
||||
-- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_tf_ext_aia_sales';
|
||||
-- 上面是生产环境location,下面是测试环境location
|
||||
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_tf_ext_aia_sales';
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- MAGIC %run ../../../Common/config
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- MAGIC %python
|
||||
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dim_product_wide`').createOrReplaceTempView('cdw_dwd_dim_product_wide')
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- 原始数据
|
||||
-- 字段值的初步处理
|
||||
CREATE OR REPLACE TEMPORARY VIEW AIA_RAWDATA
|
||||
AS
|
||||
SELECT
|
||||
YEAR,
|
||||
QTR,
|
||||
CAST(YEAR * 100 + YM AS INT) YM,
|
||||
ORG_PROV,
|
||||
ORG_CITY,
|
||||
INS_LEVEL,
|
||||
CASE
|
||||
WHEN
|
||||
INS_CD IS NOT NULL
|
||||
AND
|
||||
LENGTH (INS_CD) < 7
|
||||
THEN RIGHT (CONCAT ('0000000', INS_CD), 7)
|
||||
ELSE INS_CD
|
||||
END AS ORG_INS_CD,
|
||||
ATC1,
|
||||
ATC2,
|
||||
ATC3,
|
||||
ATC4,
|
||||
ATC,
|
||||
MOLE_NM,
|
||||
PROD_NM,
|
||||
ORG_PRD_NM_C,
|
||||
ORG_PK,
|
||||
ORG_PRD_STR,
|
||||
ORG_PTH,
|
||||
MIN_UNIT,
|
||||
VALUE,
|
||||
UNIT,
|
||||
CAST(PACK_DESC AS INT) AS PACK_DESC,
|
||||
ORG_PK_UN,
|
||||
MANU
|
||||
FROM
|
||||
DWD.DWD_GND_AIA_RAWDATA
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
--关联出AUDIT_COD、INST_CODE
|
||||
CREATE OR REPLACE TEMPORARY VIEW AIA_RAWDATA_WITH_AUDIT_INST
|
||||
AS
|
||||
SELECT
|
||||
UPPER(NVL (T2.INST_CODE, T1.ORG_INS_CD)) AS AUDIT_COD,
|
||||
T1.YEAR,
|
||||
T1.QTR,
|
||||
T1.YM,
|
||||
T1.ORG_PROV,
|
||||
T1.ORG_CITY,
|
||||
T1.INS_LEVEL,
|
||||
T1.ORG_INS_CD,
|
||||
T1.ATC1,
|
||||
T1.ATC2,
|
||||
T1.ATC3,
|
||||
T1.ATC4,
|
||||
T1.ATC,
|
||||
T1.MOLE_NM,
|
||||
T1.PROD_NM,
|
||||
T1.ORG_PRD_NM_C,
|
||||
T1.ORG_PK,
|
||||
T1.ORG_PRD_STR,
|
||||
T1.ORG_PTH,
|
||||
T1.MIN_UNIT,
|
||||
T1.VALUE,
|
||||
T1.UNIT,
|
||||
T1.PACK_DESC,
|
||||
T1.ORG_PK_UN,
|
||||
T1.MANU,
|
||||
NVL (T2.INST_CODE, T1.ORG_INS_CD) AS INST_CODE
|
||||
FROM
|
||||
AIA_RAWDATA AS T1
|
||||
LEFT JOIN
|
||||
(
|
||||
SELECT
|
||||
DISTINCT
|
||||
CASE
|
||||
WHEN LENGTH (CPA_HOSPITAL_CODE) < 7
|
||||
THEN RIGHT (CONCAT ('0000000', CPA_HOSPITAL_CODE), 7)
|
||||
ELSE CPA_HOSPITAL_CODE
|
||||
END AS CPA_HOSPITAL_CODE,
|
||||
INS_CD_NL AS INST_CODE
|
||||
FROM
|
||||
DWD.DWD_GND_HOSPITAL_NOT_PROVIDED
|
||||
) AS T2
|
||||
ON
|
||||
T1.ORG_INS_CD = T2.CPA_HOSPITAL_CODE
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
/*
|
||||
修改人 CHENWU
|
||||
修改时间 20250513
|
||||
修改内容 打包分子的数据,不论 打通表 和 事实表,都没有系数,需要用 事实表里的 最小制剂单位数量 作为COUNTING_UNIT
|
||||
*/
|
||||
--关联出PACK_COD、CORP_COD、COUNTING_UNIT
|
||||
CREATE OR REPLACE TEMPORARY VIEW AIA_RAWDATA_WIHT_COUNTING_UNIT
|
||||
AS
|
||||
SELECT
|
||||
CASE
|
||||
WHEN
|
||||
LENGTH (TRIM(T2.IQVIA_PACK_CODE)) < 12
|
||||
AND
|
||||
TRIM(T2.IQVIA_PACK_CODE) REGEXP '^[0-9]'
|
||||
THEN RIGHT (CONCAT ('000000000000', TRIM(T2.IQVIA_PACK_CODE)),12)
|
||||
ELSE TRIM(T2.IQVIA_PACK_CODE)
|
||||
END AS PACK_COD,
|
||||
T3.CORP_COD,
|
||||
CASE
|
||||
WHEN LEFT (T2.IQVIA_PACK_CODE, 4) = 'AZP_'
|
||||
THEN CAST(T1.MIN_UNIT / T1.ORG_PK_UN AS DECIMAL(38, 10))
|
||||
ELSE T3.COUNTING_UNIT
|
||||
END COUNTING_UNIT,
|
||||
T1.AUDIT_COD,
|
||||
T1.YEAR,
|
||||
T1.QTR,
|
||||
T1.YM,
|
||||
T1.ORG_PROV,
|
||||
T1.ORG_CITY,
|
||||
T1.INS_LEVEL,
|
||||
T1.ORG_INS_CD,
|
||||
T1.ATC1,
|
||||
T1.ATC2,
|
||||
T1.ATC3,
|
||||
T1.ATC4,
|
||||
T1.ATC,
|
||||
T1.MOLE_NM,
|
||||
T1.PROD_NM,
|
||||
T1.ORG_PRD_NM_C,
|
||||
T1.ORG_PK,
|
||||
T1.ORG_PRD_STR,
|
||||
T1.ORG_PTH,
|
||||
T1.MIN_UNIT,
|
||||
T1.VALUE,
|
||||
T1.UNIT,
|
||||
T1.PACK_DESC,
|
||||
T1.ORG_PK_UN,
|
||||
T1.MANU,
|
||||
T1.INST_CODE
|
||||
FROM
|
||||
AIA_RAWDATA_WITH_AUDIT_INST T1
|
||||
LEFT JOIN
|
||||
(
|
||||
SELECT
|
||||
DISTINCT
|
||||
TRIM(IQVIA_PACK_CODE) AS IQVIA_PACK_CODE,
|
||||
ATC1,
|
||||
ATC2,
|
||||
ATC3,
|
||||
ATC4,
|
||||
ORG_PRD_CD,
|
||||
ORG_MOLE_NM_C,
|
||||
ORG_MANU_PRD_NM_C,
|
||||
ORG_PRD_NM_C,
|
||||
ORG_PK,
|
||||
ORG_PRD_STR,
|
||||
ORG_PTH,
|
||||
MIN_PK_UNIT,
|
||||
ORG_PK_UNIT,
|
||||
ORG_MANU_NM
|
||||
FROM
|
||||
DWD.DWD_GND_EXT_AIA_CPT_DATA
|
||||
) AS T2
|
||||
--ATC1编码+ATC2编码+ATC3编码+ATC4编码+药品编码(ATC)+药品通用名+药品产品名+药品商品名+规格+剂型+给药途径+最小销售包装单位+包装规格+生产企业
|
||||
ON
|
||||
NVL (T1.ATC1, '') = NVL (T2.ATC1, '')
|
||||
AND NVL (T1.ATC2, '') = NVL (T2.ATC2, '')
|
||||
AND NVL (T1.ATC3, '') = NVL (T2.ATC3, '')
|
||||
AND NVL (T1.ATC4, '') = NVL (T2.ATC4, '')
|
||||
AND NVL (T1.ATC, '') = NVL (T2.ORG_PRD_CD, '')
|
||||
AND NVL (T1.MOLE_NM, '') = NVL (T2.ORG_MOLE_NM_C, '')
|
||||
AND NVL (T1.PROD_NM, '') = NVL (T2.ORG_MANU_PRD_NM_C, '')
|
||||
AND NVL (T1.ORG_PRD_NM_C, '') = NVL (T2.ORG_PRD_NM_C, '')
|
||||
AND NVL (T1.ORG_PK, '') = NVL (T2.ORG_PK, '')
|
||||
AND NVL (T1.ORG_PRD_STR, '') = NVL (T2.ORG_PRD_STR, '')
|
||||
AND NVL (T1.ORG_PTH, '') = NVL (T2.ORG_PTH, '')
|
||||
AND NVL (T1.UNIT, '') = NVL (T2.MIN_PK_UNIT, '')
|
||||
AND NVL (T1.PACK_DESC, '') = NVL (T2.ORG_PK_UNIT, '')
|
||||
AND NVL (T1.MANU, '') = NVL (T2.ORG_MANU_NM, '')
|
||||
LEFT JOIN
|
||||
(
|
||||
SELECT
|
||||
PACK_COD,
|
||||
CORP_COD,
|
||||
COUNTING_UNIT
|
||||
FROM
|
||||
DM.DM_AIA_PACK_PROPERTY
|
||||
GROUP BY
|
||||
PACK_COD,
|
||||
CORP_COD,
|
||||
COUNTING_UNIT
|
||||
) T3 ON T2.IQVIA_PACK_CODE = T3.PACK_COD
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
CREATE OR REPLACE TEMPORARY VIEW FILTER_YM
|
||||
AS
|
||||
SELECT
|
||||
MAX(YM) AS MAX_YYYYMM,
|
||||
CONCAT(CAST(FLOOR(MAX(YM)/100-2) AS STRING),'01') AS MIN_YYYYMM
|
||||
FROM
|
||||
AIA_RAWDATA_WIHT_COUNTING_UNIT
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
--计算出LY销量
|
||||
CREATE OR REPLACE TEMPORARY VIEW AIA_RAWDATA_FINAL AS
|
||||
SELECT
|
||||
YM,
|
||||
ORG_INS_CD,
|
||||
INST_CODE,
|
||||
AUDIT_COD,
|
||||
PACK_COD,
|
||||
CORP_COD,
|
||||
SUM(VALUE) AS VALUE,
|
||||
SUM(VOLUME) AS VOLUME,
|
||||
SUM(COUNTING_UNIT) AS COUNTING_UNIT,
|
||||
SUM(VALUE_LY) AS VALUE_LY,
|
||||
SUM(VOLUME_LY) AS VOLUME_LY,
|
||||
SUM(COUNTING_UNIT_LY) AS COUNTING_UNIT_LY
|
||||
FROM
|
||||
(
|
||||
SELECT
|
||||
YM,
|
||||
ORG_INS_CD,
|
||||
INST_CODE,
|
||||
AUDIT_COD,
|
||||
PACK_COD,
|
||||
CORP_COD,
|
||||
CAST(VALUE AS DECIMAL(38, 10)) AS VALUE,
|
||||
CAST(ORG_PK_UN AS DECIMAL(38, 10)) AS VOLUME,
|
||||
CAST(ORG_PK_UN AS DECIMAL(38, 10)) * COALESCE(CAST(COUNTING_UNIT AS DECIMAL(38, 10)), 1) AS COUNTING_UNIT,
|
||||
0 AS VALUE_LY,
|
||||
0 AS VOLUME_LY,
|
||||
0 AS COUNTING_UNIT_LY
|
||||
FROM
|
||||
AIA_RAWDATA_WIHT_COUNTING_UNIT
|
||||
UNION ALL
|
||||
SELECT
|
||||
CAST(CAST(YM AS INT) + 100 AS STRING) AS YM,
|
||||
ORG_INS_CD,
|
||||
INST_CODE,
|
||||
AUDIT_COD,
|
||||
PACK_COD,
|
||||
CORP_COD,
|
||||
0 AS VALUE,
|
||||
0 AS VOLUME,
|
||||
0 AS COUNTING_UNIT,
|
||||
CAST(VALUE AS DECIMAL(38, 10)) AS VALUE_LY,
|
||||
CAST(ORG_PK_UN AS DECIMAL(38, 10)) AS VOLUME_LY,
|
||||
CAST(ORG_PK_UN AS DECIMAL(38, 10)) * COALESCE(CAST(COUNTING_UNIT AS DECIMAL(38, 10)), 1) AS COUNTING_UNIT_LY
|
||||
FROM
|
||||
AIA_RAWDATA_WIHT_COUNTING_UNIT T1
|
||||
JOIN
|
||||
FILTER_YM T2
|
||||
ON
|
||||
1 = 1
|
||||
WHERE
|
||||
CAST(CAST(YM AS INT) + 100 AS STRING) <= T2.MAX_YYYYMM
|
||||
)
|
||||
GROUP BY
|
||||
YM,
|
||||
ORG_INS_CD,
|
||||
INST_CODE,
|
||||
AUDIT_COD,
|
||||
PACK_COD,
|
||||
CORP_COD
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
CREATE OR REPLACE TEMPORARY VIEW FINAL_DM_TF_EXT_AIA_SALES
|
||||
AS
|
||||
SELECT
|
||||
YM,
|
||||
ORG_INS_CD,
|
||||
INST_CODE,
|
||||
AUDIT_COD,
|
||||
PACK_COD,
|
||||
CORP_COD,
|
||||
SUM(VALUE) AS VALUE,
|
||||
SUM(VOLUME) AS VOLUME,
|
||||
SUM(COUNTING_UNIT) AS COUNTING_UNIT,
|
||||
SUM(VALUE_LY) AS VALUE_LY,
|
||||
SUM(VOLUME_LY) AS VOLUME_LY,
|
||||
SUM(COUNTING_UNIT_LY) AS COUNTING_UNIT_LY
|
||||
FROM
|
||||
AIA_RAWDATA_FINAL
|
||||
GROUP BY
|
||||
YM,
|
||||
ORG_INS_CD,
|
||||
INST_CODE,
|
||||
AUDIT_COD,
|
||||
PACK_COD,
|
||||
CORP_COD
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
INSERT OVERWRITE TABLE DM.DM_TF_EXT_AIA_SALES (
|
||||
YYYYMM,
|
||||
PACK_CODE,
|
||||
AUDIT_CODE,
|
||||
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_CODE,
|
||||
ETL_INSERT_DT,
|
||||
ETL_UPDATE_DT
|
||||
)
|
||||
SELECT
|
||||
A.ym,
|
||||
A.PACK_COD as PACK_CODE,
|
||||
A.AUDIT_COD AS AUDIT_CODE,
|
||||
'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_CODE, --可以移除,报告没有使用
|
||||
-------------------------------------
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||
FROM
|
||||
FINAL_DM_TF_EXT_AIA_SALES A
|
||||
WHERE
|
||||
ym >= '202401'
|
||||
GROUP BY
|
||||
YM,
|
||||
PACK_COD,
|
||||
CORP_COD,
|
||||
AUDIT_COD,
|
||||
INST_CODE
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- 20260109 dm.dm_ext_aia_sales 是老的表,现在已经用DM.DM_TF_EXT_AIA_SALES 替换,但是为了下游,老表暂时不下线,仍然写入
|
||||
insert overwrite table dm.dm_ext_aia_sales (
|
||||
ym,
|
||||
org_ins_cd,
|
||||
inst_code,
|
||||
AUDIT_COD,
|
||||
pack_cod,
|
||||
corp_cod,
|
||||
value,
|
||||
volume,
|
||||
counting_unit,
|
||||
value_ly,
|
||||
volume_ly,
|
||||
counting_unit_ly
|
||||
)
|
||||
select
|
||||
ym,
|
||||
org_ins_cd,
|
||||
inst_code,
|
||||
AUDIT_COD,
|
||||
pack_cod,
|
||||
corp_cod,
|
||||
sum(value) as value,
|
||||
sum(volume) as volume,
|
||||
sum(counting_unit) as counting_unit,
|
||||
sum(value_ly) as value_ly,
|
||||
sum(volume_ly) as volume_ly,
|
||||
sum(counting_unit_ly) as counting_unit_ly
|
||||
from
|
||||
aia_rawdata_FINAL
|
||||
CROSS JOIN
|
||||
filter_ym
|
||||
WHERE
|
||||
YM BETWEEN filter_ym.min_yyyymm AND filter_ym.max_yyyymm
|
||||
group by
|
||||
ym,
|
||||
org_ins_cd,
|
||||
inst_code,
|
||||
AUDIT_COD,
|
||||
pack_cod,
|
||||
corp_cod
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- MAGIC %md
|
||||
-- MAGIC 下面是重构验证,chenwu可删
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- %sql
|
||||
-- inst_code
|
||||
-- ym
|
||||
-- MARKET
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- SELECT ym,AUDIT_COD,inst_code,SUM(value) value,SUM(volume) volume,SUM(counting_unit) counting_unit,SUM(value_ly) value_ly,SUM(volume_ly) volume_ly,SUM(counting_unit_ly) counting_unit_ly
|
||||
-- FROM dm.dm_ext_aia_sales
|
||||
-- GROUP BY ym,AUDIT_COD,inst_code
|
||||
-- ORDER BY ym,AUDIT_COD,inst_code
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- SELECT ym,AUDIT_COD,SUM(value) value,SUM(volume) volume,SUM(counting_unit) counting_unit,SUM(value_ly) value_ly,SUM(volume_ly) volume_ly,SUM(counting_unit_ly) counting_unit_ly
|
||||
-- FROM dm.dm_ext_aia_sales
|
||||
-- GROUP BY ym,AUDIT_COD
|
||||
-- ORDER BY ym,AUDIT_COD
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
|
||||
-- SELECT
|
||||
-- old.ym,
|
||||
-- OLD.AUDIT_COD,
|
||||
-- OLD.inst_code,
|
||||
-- OLD.value,
|
||||
-- NEW.value,
|
||||
-- OLD.value-NEW.value as dif_value,
|
||||
-- OLD.value_ly,
|
||||
-- NEW.value_ly,
|
||||
-- OLD.value_ly-NEW.value_ly as dif_value_ly,
|
||||
-- OLD.volume,
|
||||
-- NEW.volume,
|
||||
-- OLD.volume-NEW.volume as dif_volume,
|
||||
-- OLD.volume_ly,
|
||||
-- NEW.volume_ly,
|
||||
-- OLD.volume_ly-NEW.volume_ly as dif_volume_ly,
|
||||
-- OLD.counting_unit,
|
||||
-- NEW.counting_unit,
|
||||
-- OLD.counting_unit-NEW.counting_unit as dif_counting_unit,
|
||||
-- OLD.counting_unit_ly,
|
||||
-- NEW.counting_unit_ly,
|
||||
-- OLD.counting_unit_ly-NEW.counting_unit_ly as dif_counting_unit_ly
|
||||
-- FROM
|
||||
-- (
|
||||
-- SELECT ym,AUDIT_COD,inst_code,SUM(value) value,SUM(volume) volume,SUM(counting_unit) counting_unit,SUM(value_ly) value_ly,SUM(volume_ly) volume_ly,SUM(counting_unit_ly) counting_unit_ly
|
||||
-- FROM dm.dm_ext_aia_sales
|
||||
-- GROUP BY ym,AUDIT_COD,inst_code
|
||||
-- -- ORDER BY ym
|
||||
-- ) AS OLD
|
||||
-- LEFT JOIN
|
||||
-- (
|
||||
-- SELECT YYYYMM,AUDIT_CODE,inst_code,SUM(SALES_VALUE_CAL) value,SUM(SALES_UNIT_CAL) volume,SUM(CONUTING_UNIT) counting_unit,SUM(SALES_VALUE_CAL_LY) value_ly,SUM(SALES_UNIT_CAL_LY) volume_ly,SUM(CONUTING_UNIT_LY) counting_unit_ly
|
||||
-- FROM DM.DM_TF_EXT_AIA_SALES
|
||||
-- GROUP BY YYYYMM,AUDIT_CODE,inst_code
|
||||
-- -- ORDER BY YYYYMM
|
||||
-- ) AS NEW
|
||||
-- ON OLD.YM =NEW.YYYYMM AND old.AUDIT_COD = NEW.AUDIT_CODE AND old.inst_code = new.inst_code
|
||||
-- where
|
||||
-- ABS(OLD.value - NEW.value) > 0.0000000001 OR
|
||||
-- ABS(OLD.volume - NEW.volume) > 0.0000000001 OR
|
||||
-- ABS(OLD.value_ly - NEW.value_ly) > 0.0000000001 OR
|
||||
-- ABS(OLD.volume_ly - NEW.volume_ly) > 0.0000000001 OR
|
||||
-- ABS(OLD.counting_unit - NEW.counting_unit) > 0.0000000001 OR
|
||||
-- ABS(OLD.counting_unit_ly - NEW.counting_unit_ly) > 0.0000000001;
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
|
||||
-- SELECT
|
||||
-- old.ym,
|
||||
-- OLD.AUDIT_COD,
|
||||
-- OLD.value,
|
||||
-- NEW.value,
|
||||
-- OLD.value-NEW.value as dif_value,
|
||||
-- OLD.value_ly,
|
||||
-- NEW.value_ly,
|
||||
-- OLD.value_ly-NEW.value_ly as dif_value_ly,
|
||||
-- OLD.volume,
|
||||
-- NEW.volume,
|
||||
-- OLD.volume-NEW.volume as dif_volume,
|
||||
-- OLD.volume_ly,
|
||||
-- NEW.volume_ly,
|
||||
-- OLD.volume_ly-NEW.volume_ly as dif_volume_ly,
|
||||
-- OLD.counting_unit,
|
||||
-- NEW.counting_unit,
|
||||
-- OLD.counting_unit-NEW.counting_unit as dif_counting_unit,
|
||||
-- OLD.counting_unit_ly,
|
||||
-- NEW.counting_unit_ly,
|
||||
-- OLD.counting_unit_ly-NEW.counting_unit_ly as dif_counting_unit_ly
|
||||
-- FROM
|
||||
-- (
|
||||
-- SELECT ym,AUDIT_COD,SUM(value) value,SUM(volume) volume,SUM(counting_unit) counting_unit,SUM(value_ly) value_ly,SUM(volume_ly) volume_ly,SUM(counting_unit_ly) counting_unit_ly
|
||||
-- FROM dm.dm_ext_aia_sales
|
||||
-- GROUP BY ym,AUDIT_COD
|
||||
-- -- ORDER BY ym
|
||||
-- ) AS OLD
|
||||
-- LEFT JOIN
|
||||
-- (
|
||||
-- SELECT YYYYMM,AUDIT_CODE,SUM(SALES_VALUE_CAL) value,SUM(SALES_UNIT_CAL) volume,SUM(CONUTING_UNIT) counting_unit,SUM(SALES_VALUE_CAL_LY) value_ly,SUM(SALES_UNIT_CAL_LY) volume_ly,SUM(CONUTING_UNIT_LY) counting_unit_ly
|
||||
-- FROM DM.DM_TF_EXT_AIA_SALES
|
||||
-- GROUP BY YYYYMM,AUDIT_CODE
|
||||
-- -- ORDER BY YYYYMM
|
||||
-- ) AS NEW
|
||||
-- ON OLD.YM =NEW.YYYYMM AND old.AUDIT_COD = NEW.AUDIT_CODE
|
||||
-- where
|
||||
-- ABS(OLD.value - NEW.value) > 0.0000000001 OR
|
||||
-- ABS(OLD.volume - NEW.volume) > 0.0000000001 OR
|
||||
-- ABS(OLD.value_ly - NEW.value_ly) > 0.0000000001 OR
|
||||
-- ABS(OLD.volume_ly - NEW.volume_ly) > 0.0000000001 OR
|
||||
-- ABS(OLD.counting_unit - NEW.counting_unit) > 0.0000000001 OR
|
||||
-- ABS(OLD.counting_unit_ly - NEW.counting_unit_ly) > 0.0000000001;
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
|
||||
-- SELECT
|
||||
-- old.ym,
|
||||
-- OLD.value,
|
||||
-- NEW.value,
|
||||
-- OLD.value-NEW.value as dif_value,
|
||||
-- OLD.value_ly,
|
||||
-- NEW.value_ly,
|
||||
-- OLD.value_ly-NEW.value_ly as dif_value_ly,
|
||||
-- OLD.volume,
|
||||
-- NEW.volume,
|
||||
-- OLD.volume-NEW.volume as dif_volume,
|
||||
-- OLD.volume_ly,
|
||||
-- NEW.volume_ly,
|
||||
-- OLD.volume_ly-NEW.volume_ly as dif_volume_ly,
|
||||
-- OLD.counting_unit,
|
||||
-- NEW.counting_unit,
|
||||
-- OLD.counting_unit-NEW.counting_unit as dif_counting_unit,
|
||||
-- OLD.counting_unit_ly,
|
||||
-- NEW.counting_unit_ly,
|
||||
-- OLD.counting_unit_ly-NEW.counting_unit_ly as dif_counting_unit_ly
|
||||
-- FROM
|
||||
-- (
|
||||
-- SELECT ym,SUM(value) value,SUM(volume) volume,SUM(counting_unit) counting_unit,SUM(value_ly) value_ly,SUM(volume_ly) volume_ly,SUM(counting_unit_ly) counting_unit_ly
|
||||
-- FROM dm.dm_ext_aia_sales
|
||||
-- GROUP BY ym
|
||||
-- ORDER BY ym
|
||||
-- ) AS OLD
|
||||
-- LEFT JOIN
|
||||
-- (
|
||||
-- SELECT YYYYMM,SUM(SALES_VALUE_CAL) value,SUM(SALES_UNIT_CAL) volume,SUM(CONUTING_UNIT) counting_unit,SUM(SALES_VALUE_CAL_LY) value_ly,SUM(SALES_UNIT_CAL_LY) volume_ly,SUM(CONUTING_UNIT_LY) counting_unit_ly
|
||||
-- FROM DM.DM_TF_EXT_AIA_SALES
|
||||
-- GROUP BY YYYYMM
|
||||
-- ORDER BY YYYYMM
|
||||
-- ) AS NEW
|
||||
-- ON OLD.YM =NEW.YYYYMM
|
||||
-- where
|
||||
-- ABS(OLD.value - NEW.value) > 0.0000000001 OR
|
||||
-- ABS(OLD.volume - NEW.volume) > 0.0000000001 OR
|
||||
-- ABS(OLD.value_ly - NEW.value_ly) > 0.0000000001 OR
|
||||
-- ABS(OLD.volume_ly - NEW.volume_ly) > 0.0000000001 OR
|
||||
-- ABS(OLD.counting_unit - NEW.counting_unit) > 0.0000000001 OR
|
||||
-- ABS(OLD.counting_unit_ly - NEW.counting_unit_ly) > 0.0000000001;
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- -- 这个sql证明,两张表的inst_code,值也相同
|
||||
-- SELECT
|
||||
-- *
|
||||
-- FROM
|
||||
-- (
|
||||
-- SELECT inst_code,COUNT(*) as c1 FROM DM.DM_TF_EXT_AIA_SALES
|
||||
-- GROUP BY inst_code
|
||||
-- ) AS NEW
|
||||
-- FULL JOIN
|
||||
-- (
|
||||
-- SELECT inst_code,COUNT(*) as c1 FROM DM.dm_ext_aia_sales
|
||||
-- GROUP BY inst_code
|
||||
-- ) AS OLD
|
||||
-- ON NEW.inst_code = old.inst_code
|
||||
-- WHERE new.c1 != old.c1
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- -- 这个sql证明,两张表的inst_code相同
|
||||
-- SELECT
|
||||
-- *
|
||||
-- FROM
|
||||
-- (
|
||||
-- SELECT inst_code,COUNT(*) as c1 FROM DM.DM_TF_EXT_AIA_SALES
|
||||
-- GROUP BY inst_code
|
||||
-- ) AS NEW
|
||||
-- FULL JOIN
|
||||
-- (
|
||||
-- SELECT inst_code,COUNT(*) as c1 FROM DM.DM_TF_EXT_AIA_SALES
|
||||
-- GROUP BY inst_code
|
||||
-- ) AS OLD
|
||||
-- ON NEW.inst_code = old.inst_code
|
||||
-- WHERE NEW.inst_code IS NULL OR old.inst_code IS NULL
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- -- 这个sql证明,两张表的inst_code,值也相同
|
||||
-- SELECT
|
||||
-- *
|
||||
-- FROM
|
||||
-- (
|
||||
-- SELECT inst_code,COUNT(*) as c1 FROM DM.DM_TF_EXT_AIA_SALES
|
||||
-- GROUP BY inst_code
|
||||
-- ) AS NEW
|
||||
-- FULL JOIN
|
||||
-- (
|
||||
-- SELECT inst_code,COUNT(*) as c1 FROM DM.DM_TF_EXT_AIA_SALES
|
||||
-- GROUP BY inst_code
|
||||
-- ) AS OLD
|
||||
-- ON NEW.inst_code = old.inst_code
|
||||
-- WHERE new.c1 != old.c1
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- SELECT COUNT(*) FROM DM.DM_TF_EXT_AIA_SALES
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- SELECT COUNT(*) FROM DM.dm_ext_aia_sales
|
||||
195
AIA/02 dm_ext_aia_sales_bakup_20230327.sql
Normal file
195
AIA/02 dm_ext_aia_sales_bakup_20230327.sql
Normal file
@@ -0,0 +1,195 @@
|
||||
-- Databricks notebook source
|
||||
--字段值的初步处理
|
||||
create or replace temporary view aia_rawdata_1 as
|
||||
select
|
||||
year,
|
||||
qtr,
|
||||
cast(year * 100 + ym as int) ym,
|
||||
org_prov,
|
||||
org_city,
|
||||
ins_level,
|
||||
case when ins_cd is not null and length(ins_cd) < 7 then right(concat('0000000',ins_cd),7) else ins_cd end as org_ins_cd,
|
||||
atc1,
|
||||
atc2,
|
||||
atc3,
|
||||
atc4,
|
||||
atc,
|
||||
mole_nm,
|
||||
prod_nm,
|
||||
org_prd_nm_c,
|
||||
org_pk,
|
||||
org_prd_str,
|
||||
org_pth,
|
||||
min_unit,
|
||||
value,
|
||||
unit,
|
||||
cast(pack_desc as int) as pack_desc,
|
||||
org_pk_un,
|
||||
manu
|
||||
from dwd.dwd_gnd_aia_rawdata
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
|
||||
--关联出AUDIT_COD、inst_code
|
||||
create or replace temporary view aia_rawdata_2 as
|
||||
with hospital_mapping as (
|
||||
select distinct
|
||||
case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code,
|
||||
ins_cd_nl as inst_code
|
||||
from dwd.dwd_gnd_hospital_not_provided
|
||||
)
|
||||
|
||||
select upper(nvl(t2.inst_code,t1.org_ins_cd)) AUDIT_COD,
|
||||
t1.*,
|
||||
nvl(t2.inst_code,t1.org_ins_cd) as inst_code
|
||||
from aia_rawdata_1 t1
|
||||
left join hospital_mapping t2
|
||||
on t1.org_ins_cd = t2.cpa_hospital_code
|
||||
-- left join dm.dm_td_institution t3
|
||||
-- on t2.ins_code = t3.inst_code
|
||||
-- left join dm.dm_ims_td_geo t4
|
||||
-- on t3.city_name_en = t4.city
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
/*
|
||||
修改人 chenwu
|
||||
修改时间 20250513
|
||||
修改内容 打包分子的数据,不论 打通表 和 事实表,都没有系数,需要用 事实表里的 最小制剂单位数量 作为counting_unit
|
||||
*/
|
||||
--关联出pack_cod、corp_cod、counting_unit
|
||||
create or replace temporary view aia_rawdata_3 as
|
||||
select case when length(trim(t2.iqvia_pack_code)) < 12 and trim(t2.iqvia_pack_code) REGEXP '^[0-9]' then right(concat('000000000000',trim(t2.iqvia_pack_code)),12)
|
||||
else trim(t2.iqvia_pack_code)
|
||||
end as pack_cod,
|
||||
t3.corp_cod,
|
||||
case when left(t2.iqvia_pack_code,4) = 'AZP_' then cast(t1.min_unit/t1.org_pk_un as decimal(38,10)) else t3.counting_unit end counting_unit,
|
||||
-- t3.counting_unit,20250513
|
||||
t1.*
|
||||
from aia_rawdata_2 t1
|
||||
left join (select distinct trim(iqvia_pack_code) as iqvia_pack_code,
|
||||
ATC1,ATC2,ATC3,ATC4,org_prd_cd,org_mole_nm_c,
|
||||
org_manu_prd_nm_c,org_prd_nm_c,org_pk,org_prd_str,
|
||||
org_pth,min_pk_unit,org_pk_unit,org_manu_nm from dwd.dwd_gnd_ext_aia_cpt_data) t2
|
||||
--ATC1编码+ATC2编码+ATC3编码+ATC4编码+药品编码(ATC)+药品通用名+药品产品名+药品商品名+规格+剂型+给药途径+最小销售包装单位+包装规格+生产企业
|
||||
on nvl(t1.ATC1,'') = nvl(t2.ATC1,'')
|
||||
and nvl(t1.ATC2,'') = nvl(t2.ATC2,'')
|
||||
and nvl(t1.ATC3,'') = nvl(t2.ATC3,'')
|
||||
and nvl(t1.ATC4,'') = nvl(t2.ATC4,'')
|
||||
and nvl(t1.ATC,'') = nvl(t2.org_prd_cd,'')
|
||||
and nvl(t1.MOLE_NM,'') = nvl(t2.org_mole_nm_c,'')
|
||||
and nvl(t1.PROD_NM,'') = nvl(t2.org_manu_prd_nm_c,'')
|
||||
and nvl(t1.ORG_PRD_NM_C,'') = nvl(t2.org_prd_nm_c,'')
|
||||
and nvl(t1.ORG_PK,'') = nvl(t2.org_pk,'')
|
||||
and nvl(t1.ORG_PRD_STR,'') = nvl(t2.org_prd_str,'')
|
||||
and nvl(t1.ORG_PTH,'') = nvl(t2.org_pth,'')
|
||||
and nvl(t1.UNIT,'') = nvl(t2.min_pk_unit,'')
|
||||
and nvl(t1.PACK_DESC,'') = nvl(t2.org_pk_unit,'')
|
||||
and nvl(t1.MANU,'') = nvl(t2.org_manu_nm,'')
|
||||
left join (select distinct PACK_COD,corp_cod,counting_unit from dm.dm_aia_pack_property) t3
|
||||
on t2.iqvia_pack_code = t3.PACK_COD
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
|
||||
--计算出ly销量
|
||||
create or replace temporary view aia_rawdata_4 as
|
||||
with max_ym as (
|
||||
select max(ym) as max_ym from aia_rawdata_3
|
||||
)
|
||||
|
||||
select
|
||||
YM,
|
||||
org_ins_cd,
|
||||
inst_code,
|
||||
AUDIT_COD,
|
||||
pack_cod,
|
||||
corp_cod,
|
||||
sum(value) as value,
|
||||
sum(volume) as volume,
|
||||
sum(counting_unit) as counting_unit,
|
||||
sum(value_ly) as value_ly,
|
||||
sum(volume_ly) as volume_ly,
|
||||
sum(counting_unit_ly) as counting_unit_ly
|
||||
from (
|
||||
select
|
||||
ym,
|
||||
org_ins_cd,
|
||||
inst_code,
|
||||
AUDIT_COD,
|
||||
pack_cod,
|
||||
corp_cod,
|
||||
cast(value as decimal(38,10)) as value,
|
||||
cast(ORG_PK_UN as decimal(38,10)) as volume,
|
||||
cast(ORG_PK_UN as decimal(38,10)) * coalesce(cast(counting_unit as decimal(38,10)),1) as counting_unit,
|
||||
0 as value_ly,
|
||||
0 as volume_ly,
|
||||
0 as counting_unit_ly
|
||||
from aia_rawdata_3
|
||||
union all
|
||||
select
|
||||
cast(cast(ym as int) + 100 as string) as ym,
|
||||
org_ins_cd,
|
||||
inst_code,
|
||||
AUDIT_COD,
|
||||
pack_cod,
|
||||
corp_cod,
|
||||
0 as value,
|
||||
0 as volume,
|
||||
0 as counting_unit,
|
||||
cast(value as decimal(38,10)) as value_ly,
|
||||
cast(ORG_PK_UN as decimal(38,10)) as volume_ly,
|
||||
cast(ORG_PK_UN as decimal(38,10)) * coalesce(cast(counting_unit as decimal(38,10)),1) as counting_unit_ly
|
||||
from aia_rawdata_3 t1
|
||||
join max_ym t2
|
||||
on 1=1
|
||||
where cast(cast(ym as int) + 100 as string) <= t2.max_ym
|
||||
)
|
||||
group by ym,
|
||||
org_ins_cd,
|
||||
inst_code,
|
||||
AUDIT_COD,
|
||||
pack_cod,
|
||||
corp_cod
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
insert overwrite table dm.dm_ext_aia_sales(
|
||||
ym,
|
||||
org_ins_cd,
|
||||
inst_code,
|
||||
AUDIT_COD,
|
||||
pack_cod,
|
||||
corp_cod,
|
||||
value,
|
||||
volume,
|
||||
counting_unit,
|
||||
value_ly,
|
||||
volume_ly,
|
||||
counting_unit_ly
|
||||
)
|
||||
select
|
||||
ym,
|
||||
org_ins_cd,
|
||||
inst_code,
|
||||
AUDIT_COD,
|
||||
pack_cod,
|
||||
corp_cod,
|
||||
sum(value) as value,
|
||||
sum(volume) as volume,
|
||||
sum(counting_unit) as counting_unit,
|
||||
sum(value_ly) as value_ly,
|
||||
sum(volume_ly) as volume_ly,
|
||||
sum(counting_unit_ly) as counting_unit_ly
|
||||
from aia_rawdata_4
|
||||
group by
|
||||
ym,
|
||||
org_ins_cd,
|
||||
inst_code,
|
||||
AUDIT_COD,
|
||||
pack_cod,
|
||||
corp_cod
|
||||
84
AIA/03 dm_aia_flag.sql
Normal file
84
AIA/03 dm_aia_flag.sql
Normal file
@@ -0,0 +1,84 @@
|
||||
-- Databricks notebook source
|
||||
-----------------------------------------------------------------------------------
|
||||
--修改时间:20241021
|
||||
--修改人:FanXujia
|
||||
--修改内容:
|
||||
--只取最新年-1 ~ 最新年范围内的,称为“2年共有”。增加
|
||||
-----------------------------------------------------------------------------------
|
||||
--共有机构
|
||||
insert overwrite table dm.dm_aia_hp_flag
|
||||
with max_year as (
|
||||
select
|
||||
max(cast(year as int)) as max_year,
|
||||
max(cast(year as int) - 1) as max_year_ly
|
||||
from dwd.dwd_gnd_hospital_not_provided
|
||||
)
|
||||
,hp_flag as (
|
||||
select
|
||||
distinct concat(t1.year, '-Y') as hp_his_flag,
|
||||
case when length(t1.cpa_hospital_code) < 7 then right(concat('0000000',t1.cpa_hospital_code),7) else t1.cpa_hospital_code end as cpa_hospital_code
|
||||
from
|
||||
dwd.dwd_gnd_hospital_not_provided t1
|
||||
cross join max_year t2
|
||||
where cast(t1.year as int) >= t2.max_year_ly
|
||||
)
|
||||
,hp_flag2 as (
|
||||
SELECT
|
||||
cpa_hospital_code,
|
||||
CONCAT_WS(' ∩ ', SORT_ARRAY(COLLECT_LIST(hp_his_flag))) AS FLAG
|
||||
FROM
|
||||
hp_flag
|
||||
GROUP BY
|
||||
cpa_hospital_code
|
||||
)
|
||||
,inst_mapping as (
|
||||
-- select
|
||||
-- distinct right(concat('0000000', org_cd), 7) org_cd,
|
||||
-- ins_cd
|
||||
-- from
|
||||
-- dwd.dwd_gnd_hospitalmapping
|
||||
select distinct
|
||||
case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code,
|
||||
ins_cd_nl as inst_code
|
||||
from dwd.dwd_gnd_hospital_not_provided
|
||||
)
|
||||
|
||||
,ALL_INS as (
|
||||
select distinct inst_code
|
||||
from dm.dm_ext_aia_sales
|
||||
)
|
||||
|
||||
,flag as (
|
||||
select distinct
|
||||
coalesce(t2.inst_code,t1.cpa_hospital_code) as inst_code,
|
||||
t1.flag as aia_hp_flag,
|
||||
'Y' as FLAG,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from hp_flag2 t1
|
||||
left join inst_mapping t2 on t1.cpa_hospital_code = t2.cpa_hospital_code
|
||||
where
|
||||
length(t1.flag) > 6
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-CHPA(Monthly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'XH Data(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'EC(Monthly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'Retail(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'CHC(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'THC(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-COUNTY(Quarterly)' as DATA_SOURCE
|
||||
)
|
||||
|
||||
select t1.inst_code,
|
||||
NVL(t2.aia_hp_flag,'') as aia_hp_flag,
|
||||
NVL(t2.FLAG,'N') as FLAG,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from ALL_INS t1
|
||||
left join flag t2
|
||||
on t1.inst_code = t2.inst_code
|
||||
|
||||
112
AIA/04 dm_aia_provided_flag.sql
Normal file
112
AIA/04 dm_aia_provided_flag.sql
Normal file
@@ -0,0 +1,112 @@
|
||||
-- Databricks notebook source
|
||||
-- MAGIC %run ../../../Common/config
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- MAGIC %python
|
||||
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dim_product_wide`').createOrReplaceTempView('cdw_dwd_dim_product_wide')
|
||||
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dwd_hospital_classification`').createOrReplaceTempView('cdw_dwd_dwd_hospital_classification')
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
--连续取数机构
|
||||
----------------------------------------------------------------------------------------------------------------------
|
||||
--修改时间:20241029
|
||||
--修改人:FanXujia
|
||||
--修改内容:
|
||||
--根据事实表最新月,往前推12个月(含最新月),按机构 + 年月粒度汇总,如果每个月销量都不等于0,这家机构称为:滚动一年有数
|
||||
--根据事实表最新月,往前推24个月(含最新月),按机构 + 年月粒度汇总,如果每个月销量都不等于0,这家机构称为:滚动两年有数
|
||||
--2022年没有销量为正常情况,判断时仅考虑2023年01月起销量是否等于0
|
||||
----------------------------------------------------------------------------------------------------------------------
|
||||
insert overwrite table dm.dm_aia_provided_flag
|
||||
with max_ym as (
|
||||
select max(ym) as max_ym
|
||||
from dm.dm_ext_aia_sales
|
||||
)
|
||||
,ym_range as (
|
||||
select max_ym,
|
||||
case when max_ym <= '202412' then cast(right(max_ym,2) as int)
|
||||
else cast(12 as int)
|
||||
end ym_12m,
|
||||
case when max_ym <= '202312' then '202401'
|
||||
when right(max_ym,2) = '12' then concat(left(max_ym,4),'01')
|
||||
else concat(cast(left(max_ym, 4) - 1 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2))
|
||||
end as ym_12m_before,
|
||||
case when max_ym <= '202312' then cast(right(max_ym,2) as int)
|
||||
when max_ym <= '202512' then cast(right(max_ym,2) as int) + 12
|
||||
else cast(24 as int)
|
||||
end as ym_24m,
|
||||
case when max_ym <= '202512' then '202401'
|
||||
when right(max_ym,2) = '12' then concat(cast(left(max_ym,4) - 1 as int),'01')
|
||||
else concat(cast(left(max_ym, 4) -2 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2))
|
||||
end as ym_24m_before
|
||||
from max_ym
|
||||
)
|
||||
,sales_1 as (
|
||||
select
|
||||
inst_code,
|
||||
ym
|
||||
from
|
||||
dm.dm_ext_aia_sales
|
||||
group by
|
||||
ym,
|
||||
inst_code
|
||||
having
|
||||
sum(value) <> 0
|
||||
)
|
||||
,sales_2 as (
|
||||
select
|
||||
t1.inst_code,
|
||||
t1.ym,
|
||||
t2.max_ym,
|
||||
t2.ym_12m,
|
||||
t2.ym_12m_before,
|
||||
t2.ym_24m,
|
||||
t2.ym_24m_before
|
||||
from sales_1 t1
|
||||
cross join ym_range t2
|
||||
)
|
||||
,flag_12m as (
|
||||
select inst_code,
|
||||
'滚动一年有数' as aia_provided_flag,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from sales_2
|
||||
where ym <= max_ym
|
||||
and ym >= ym_12m_before
|
||||
group by inst_code
|
||||
having count(1) = (select ym_12m from ym_range)
|
||||
)
|
||||
,flag_24m as (
|
||||
select inst_code,
|
||||
'滚动两年有数' as aia_provided_flag,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from sales_2
|
||||
where ym <= max_ym
|
||||
and ym >= ym_24m_before
|
||||
group by inst_code
|
||||
having count(1) = (select ym_24m from ym_range)
|
||||
)
|
||||
|
||||
select inst_code,
|
||||
aia_provided_flag,
|
||||
DATA_SOURCE
|
||||
from flag_12m
|
||||
union all
|
||||
select inst_code,
|
||||
aia_provided_flag,
|
||||
DATA_SOURCE
|
||||
from flag_24m
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'IQVIA-CHPA(Monthly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'XH Data(Quarterly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'EC(Monthly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'Retail(Quarterly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'CHC(Quarterly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'THC(Quarterly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'IQVIA-COUNTY(Quarterly)' as data_source
|
||||
146
AIA/06 DM_TD_EXT_AIA_PACK_PROPERTY.sql
Normal file
146
AIA/06 DM_TD_EXT_AIA_PACK_PROPERTY.sql
Normal file
@@ -0,0 +1,146 @@
|
||||
-- Databricks notebook source
|
||||
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_PACK_PROPERTY
|
||||
-- (
|
||||
-- -- MARKET_PACK_KEY STRING,
|
||||
-- PACK_CODE STRING,
|
||||
-- PACK_DESC STRING,
|
||||
-- STGH_DESC STRING,
|
||||
-- PACK_LCH STRING,
|
||||
-- FAMILY_CODE STRING,
|
||||
-- FAMILY_NAME STRING,
|
||||
-- PROD_CODE STRING,
|
||||
-- PROD_DESC STRING,
|
||||
-- PROD_DESC_C STRING,
|
||||
-- CMPS_CODE STRING,
|
||||
-- CMPS_DESC STRING,
|
||||
-- CMPS_DESC_C STRING,
|
||||
-- ATC1_CODE STRING,
|
||||
-- ATC2_CODE STRING,
|
||||
-- ATC3_CODE STRING,
|
||||
-- ATC4_CODE STRING,
|
||||
-- APP1_CODE STRING,
|
||||
-- APP2_CODE STRING,
|
||||
-- APP3_CODE STRING,
|
||||
-- BIO_DESC STRING,
|
||||
-- GENE_ORIG_DESC STRING,
|
||||
-- ETH_OTC_DESC STRING,
|
||||
-- NRDL_DESC STRING,
|
||||
-- NRDL_ENTRY_DATE STRING,
|
||||
-- EDL_DESC STRING,
|
||||
-- TCM_DESC STRING,
|
||||
-- PAED_DESC STRING,
|
||||
-- GQCE_DESC STRING,
|
||||
-- VBP_DESC_V STRING,
|
||||
-- VBP_DESC STRING,
|
||||
-- MANU_CODE STRING,
|
||||
-- MANU_DESC STRING,
|
||||
-- MANU_DESC_C STRING,
|
||||
-- MNFL_CODE STRING,
|
||||
-- MNFL_DESC STRING,
|
||||
-- CORP_CODE STRING,
|
||||
-- CORP_DESC STRING,
|
||||
-- CORP_DESC_C STRING,
|
||||
-- BRANDTYPE STRING,
|
||||
-- -- MARKET STRING,
|
||||
-- -- KEY_COMPETITOR STRING,
|
||||
-- IS_AZ STRING,
|
||||
-- AZ_MAIN STRING,
|
||||
-- -- AZ_RELATED STRING,
|
||||
-- ATC1_DESC STRING,
|
||||
-- ATC1_DESC_C STRING,
|
||||
-- ATC2_DESC STRING,
|
||||
-- ATC2_DESC_C STRING,
|
||||
-- ATC3_DESC STRING,
|
||||
-- ATC3_DESC_C STRING,
|
||||
-- ATC4_DESC STRING,
|
||||
-- ATC4_DESC_C STRING,
|
||||
-- APP1_DESC STRING,
|
||||
-- APP1_DESC_C STRING,
|
||||
-- APP2_DESC STRING,
|
||||
-- APP2_DESC_C STRING,
|
||||
-- APP3_DESC STRING,
|
||||
-- APP3_DESC_C STRING,
|
||||
-- -- CLASS STRING,
|
||||
-- -- MARKET_RATIO STRING,
|
||||
-- COUNTINGUNIT STRING,
|
||||
-- VBP_BRAND STRING,
|
||||
-- REPLENISH_FALG STRING,
|
||||
-- ETL_INSERT_DT TIMESTAMP,
|
||||
-- ETL_UPDATE_DT TIMESTAMP
|
||||
-- )
|
||||
-- USING delta
|
||||
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack_property';
|
||||
-- -- 上面是生产环境location,下面是测试环境location
|
||||
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack_property';
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_PACK_PROPERTY
|
||||
SELECT
|
||||
-- MARKET_PACK_KEY,
|
||||
PACK_COD AS PACK_CODE,
|
||||
PACK_DES AS PACK_DESC,
|
||||
STGH_DES AS STGH_DESC,
|
||||
PACK_LCH,
|
||||
FAMILY_CODE,
|
||||
FAMILY_NAME,
|
||||
PROD_COD AS PROD_CODE,
|
||||
PROD_DES AS PROD_DESC,
|
||||
PROD_DES_C AS PROD_DESC_C,
|
||||
CMPS_COD AS CMPS_CODE,
|
||||
CMPS_DES AS CMPS_DESC,
|
||||
CMPS_DES_C AS CMPS_DESC_C,
|
||||
ATC1_COD AS ATC1_CODE,
|
||||
ATC2_COD AS ATC2_CODE,
|
||||
ATC3_COD AS ATC3_CODE,
|
||||
ATC4_COD AS ATC4_CODE,
|
||||
APP1_COD AS APP1_CODE,
|
||||
APP2_COD AS APP2_CODE,
|
||||
APP3_COD AS APP3_CODE,
|
||||
BIO_DESC,
|
||||
GENE_ORIG_DESC,
|
||||
ETH_OTC_DESC,
|
||||
NRDL_DESC,
|
||||
NRDL_ENTRY_DATE,
|
||||
EDL_DESC,
|
||||
TCM_DESC,
|
||||
PAED_DESC,
|
||||
GQCE_DESC,
|
||||
null as VBP_DESC_V,
|
||||
VBP_DESC,
|
||||
MANU_COD AS MANU_CODE,
|
||||
MANU_DES AS MANU_DESC,
|
||||
MANU_DES_C AS MANU_DESC_C,
|
||||
MNFL_COD AS MNFL_CODE,
|
||||
MNFL_DES AS MNFL_DESC,
|
||||
CORP_COD AS CORP_CODE,
|
||||
CORP_DES AS CORP_DESC,
|
||||
CORP_DES_C AS CORP_DESC_C,
|
||||
BRANDTYPE,
|
||||
-- MARKET,
|
||||
-- KEY_COMPETITOR,
|
||||
IS_AZ,
|
||||
AZ_MAIN,
|
||||
-- AZ_RELATED,
|
||||
ATC1_DES AS ATC1_DESC,
|
||||
ATC1_DES_C AS ATC1_DESC_C,
|
||||
ATC2_DES AS ATC2_DESC,
|
||||
ATC2_DES_C AS ATC2_DESC_C,
|
||||
ATC3_DES AS ATC3_DESC,
|
||||
ATC3_DES_C AS ATC3_DESC_C,
|
||||
ATC4_DES AS ATC4_DESC,
|
||||
ATC4_DES_C AS ATC4_DESC_C,
|
||||
APP1_DES AS APP1_DESC,
|
||||
APP1_DES_C AS APP1_DESC_C,
|
||||
APP2_DES AS APP2_DESC,
|
||||
APP2_DES_C AS APP2_DESC_C,
|
||||
APP3_DES AS APP3_DESC,
|
||||
APP3_DES_C AS APP3_DESC_C,
|
||||
-- CLASS,
|
||||
-- NULL AS MARKET_RATIO,
|
||||
NULL AS COUNTINGUNIT,
|
||||
NULL AS VBP_BRAND,
|
||||
NULL AS REPLENISH_FALG,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||
FROM DM.DM_AIA_PACK_PROPERTY;
|
||||
334
AIA/07 DM_TD_EXT_AIA_PACK2MARKET.sql
Normal file
334
AIA/07 DM_TD_EXT_AIA_PACK2MARKET.sql
Normal file
@@ -0,0 +1,334 @@
|
||||
-- Databricks notebook source
|
||||
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET (
|
||||
-- MARKET STRING,
|
||||
-- PACK_CODE STRING,
|
||||
-- PACK_DESC STRING,
|
||||
-- STGH_DESC STRING,
|
||||
-- PACK_LCH STRING,
|
||||
-- PROD_CODE STRING,
|
||||
-- CMPS_CODE STRING,
|
||||
-- CMPS_DESC STRING,
|
||||
-- ATC1_CODE STRING,
|
||||
-- ATC2_CODE STRING,
|
||||
-- ATC3_CODE STRING,
|
||||
-- ATC4_CODE STRING,
|
||||
-- APP1_CODE STRING,
|
||||
-- APP2_CODE STRING,
|
||||
-- APP3_CODE STRING,
|
||||
-- BIO_DESC STRING,
|
||||
-- GENE_ORIG_DESC STRING,
|
||||
-- ETH_OTC_DESC STRING,
|
||||
-- NRDL_DESC STRING,
|
||||
-- NRDL_ENTRY_DATE STRING,
|
||||
-- EDL_DESC STRING,
|
||||
-- TCM_DESC STRING,
|
||||
-- PAED_DESC STRING,
|
||||
-- GQCE_DESC STRING,
|
||||
-- VBP_DESC STRING,
|
||||
-- MANU_CODE STRING,
|
||||
-- MANU_DESC STRING,
|
||||
-- MNFL_CODE STRING,
|
||||
-- MNFL_DESC STRING,
|
||||
-- CORP_CODE STRING,
|
||||
-- CORP_DESC STRING,
|
||||
-- BRANDTYPE STRING,
|
||||
-- BU STRING,
|
||||
-- STARTTIME STRING,
|
||||
-- ENDTIME STRING,
|
||||
-- MARKET_RATIO STRING,
|
||||
-- ETL_INSERT_DT TIMESTAMP,
|
||||
-- ETL_UPDATE_DT TIMESTAMP)
|
||||
-- USING delta
|
||||
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market';
|
||||
-- -- 上面是生产环境location,下面是测试环境location
|
||||
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market';
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET_KEYCOMPETITOR (
|
||||
-- KEY_COMPETITOR STRING,
|
||||
-- MARKET STRING,
|
||||
-- PACK_CODE STRING)
|
||||
-- USING delta
|
||||
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market_keycompetitor';
|
||||
-- -- 上面是生产环境location,下面是测试环境location
|
||||
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market_keycompetitor';
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- CREATE or REPLACE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET_CLASS (
|
||||
-- MARKET STRING,
|
||||
-- PACK_CODE STRING,
|
||||
-- CLASS STRING)
|
||||
-- USING delta
|
||||
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market_class';
|
||||
-- -- 上面是生产环境location,下面是测试环境location
|
||||
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_pack2market_class';
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET
|
||||
-- 包含规则
|
||||
SELECT DISTINCT
|
||||
T2.MARKET,
|
||||
T1.PACK_CODE,
|
||||
T1.PACK_DESC,
|
||||
T1.STGH_DESC,
|
||||
T1.PACK_LCH,
|
||||
T1.PROD_CODE,
|
||||
T1.CMPS_CODE,
|
||||
T1.CMPS_DESC,
|
||||
T1.ATC1_CODE,
|
||||
T1.ATC2_CODE,
|
||||
T1.ATC3_CODE,
|
||||
T1.ATC4_CODE,
|
||||
T1.APP1_CODE,
|
||||
T1.APP2_CODE,
|
||||
T1.APP3_CODE,
|
||||
T1.BIO_DESC,
|
||||
T1.GENE_ORIG_DESC,
|
||||
T1.ETH_OTC_DESC,
|
||||
T1.NRDL_DESC,
|
||||
T1.NRDL_ENTRY_DATE,
|
||||
T1.EDL_DESC,
|
||||
T1.TCM_DESC,
|
||||
T1.PAED_DESC,
|
||||
T1.GQCE_DESC,
|
||||
T1.VBP_DESC,
|
||||
T1.MANU_CODE,
|
||||
T1.MANU_DESC,
|
||||
T1.MNFL_CODE,
|
||||
T1.MNFL_DESC,
|
||||
T1.CORP_CODE,
|
||||
T1.CORP_DESC,
|
||||
T1.BRANDTYPE,
|
||||
T2.BU,
|
||||
T2.STARTTIME,
|
||||
T2.ENDTIME,
|
||||
COALESCE(T2.EXTEND_MARKET_RATIO, '1') AS MARKET_RATIO,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||
FROM DM.DM_TD_EXT_AIA_PACK_PROPERTY AS T1
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
MARKET,
|
||||
BU,
|
||||
STARTTIME,
|
||||
ENDTIME,
|
||||
EXTEND_MARKET_RATIO,
|
||||
ATC1_CODE,
|
||||
ATC2_CODE,
|
||||
ATC3_CODE,
|
||||
ATC4_CODE,
|
||||
NFC1_CODE,
|
||||
NFC2_CODE,
|
||||
NFC3_CODE,
|
||||
CORPORATION_CODE,
|
||||
MANUFACTURER_CODE,
|
||||
PRODUCT_CODE,
|
||||
PACK_CODE,
|
||||
STRENGTH,
|
||||
MOLECULE_CODE
|
||||
FROM DWD.DWD_GND_IMS_TBLMARKET_BYMONTH
|
||||
WHERE EXTEND_MARKET IS NULL AND (NOT_IN_FLAG IS NULL OR NOT_IN_FLAG = '1')
|
||||
) AS T2
|
||||
ON (T2.ATC1_CODE IS NULL OR T1.ATC1_CODE = T2.ATC1_CODE)
|
||||
AND (T2.ATC2_CODE IS NULL OR T1.ATC2_CODE = T2.ATC2_CODE)
|
||||
AND (T2.ATC3_CODE IS NULL OR T1.ATC3_CODE = T2.ATC3_CODE)
|
||||
AND (T2.ATC4_CODE IS NULL OR T1.ATC4_CODE = T2.ATC4_CODE)
|
||||
AND (T2.NFC1_CODE IS NULL OR T1.APP1_CODE = T2.NFC1_CODE)
|
||||
AND (T2.NFC2_CODE IS NULL OR T1.APP2_CODE = T2.NFC2_CODE)
|
||||
AND (T2.NFC3_CODE IS NULL OR T1.APP3_CODE = T2.NFC3_CODE)
|
||||
AND (T2.CORPORATION_CODE IS NULL OR T1.CORP_CODE = T2.CORPORATION_CODE)
|
||||
AND (T2.MANUFACTURER_CODE IS NULL OR T1.MANU_CODE = T2.MANUFACTURER_CODE)
|
||||
AND (T2.PRODUCT_CODE IS NULL OR T1.PROD_CODE = T2.PRODUCT_CODE)
|
||||
AND (T2.PACK_CODE IS NULL OR T1.PACK_CODE = T2.PACK_CODE)
|
||||
AND (T2.STRENGTH IS NULL OR T1.STGH_DESC = T2.STRENGTH)
|
||||
AND (T2.MOLECULE_CODE IS NULL OR T1.CMPS_CODE = T2.MOLECULE_CODE)
|
||||
WHERE T2.MARKET IS NOT NULL
|
||||
|
||||
EXCEPT
|
||||
|
||||
-- 排除规则
|
||||
SELECT DISTINCT
|
||||
T2.MARKET,
|
||||
T1.PACK_CODE,
|
||||
T1.PACK_DESC,
|
||||
T1.STGH_DESC,
|
||||
T1.PACK_LCH,
|
||||
T1.PROD_CODE,
|
||||
T1.CMPS_CODE,
|
||||
T1.CMPS_DESC,
|
||||
T1.ATC1_CODE,
|
||||
T1.ATC2_CODE,
|
||||
T1.ATC3_CODE,
|
||||
T1.ATC4_CODE,
|
||||
T1.APP1_CODE,
|
||||
T1.APP2_CODE,
|
||||
T1.APP3_CODE,
|
||||
T1.BIO_DESC,
|
||||
T1.GENE_ORIG_DESC,
|
||||
T1.ETH_OTC_DESC,
|
||||
T1.NRDL_DESC,
|
||||
T1.NRDL_ENTRY_DATE,
|
||||
T1.EDL_DESC,
|
||||
T1.TCM_DESC,
|
||||
T1.PAED_DESC,
|
||||
T1.GQCE_DESC,
|
||||
T1.VBP_DESC,
|
||||
T1.MANU_CODE,
|
||||
T1.MANU_DESC,
|
||||
T1.MNFL_CODE,
|
||||
T1.MNFL_DESC,
|
||||
T1.CORP_CODE,
|
||||
T1.CORP_DESC,
|
||||
T1.BRANDTYPE,
|
||||
T2.BU,
|
||||
T2.STARTTIME,
|
||||
T2.ENDTIME,
|
||||
COALESCE(T2.EXTEND_MARKET_RATIO, '1') AS MARKET_RATIO,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||
FROM DM.DM_TD_EXT_AIA_PACK_PROPERTY AS T1
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
MARKET,
|
||||
BU,
|
||||
STARTTIME,
|
||||
ENDTIME,
|
||||
EXTEND_MARKET_RATIO,
|
||||
ATC1_CODE,
|
||||
ATC2_CODE,
|
||||
ATC3_CODE,
|
||||
ATC4_CODE,
|
||||
NFC1_CODE,
|
||||
NFC2_CODE,
|
||||
NFC3_CODE,
|
||||
CORPORATION_CODE,
|
||||
MANUFACTURER_CODE,
|
||||
PRODUCT_CODE,
|
||||
PACK_CODE,
|
||||
STRENGTH,
|
||||
MOLECULE_CODE
|
||||
FROM DWD.DWD_GND_IMS_TBLMARKET_BYMONTH
|
||||
WHERE EXTEND_MARKET IS NULL AND NOT_IN_FLAG = '0'
|
||||
) AS T2
|
||||
ON (T2.ATC1_CODE IS NULL OR T1.ATC1_CODE = T2.ATC1_CODE)
|
||||
AND (T2.ATC2_CODE IS NULL OR T1.ATC2_CODE = T2.ATC2_CODE)
|
||||
AND (T2.ATC3_CODE IS NULL OR T1.ATC3_CODE = T2.ATC3_CODE)
|
||||
AND (T2.ATC4_CODE IS NULL OR T1.ATC4_CODE = T2.ATC4_CODE)
|
||||
AND (T2.NFC1_CODE IS NULL OR T1.APP1_CODE = T2.NFC1_CODE)
|
||||
AND (T2.NFC2_CODE IS NULL OR T1.APP2_CODE = T2.NFC2_CODE)
|
||||
AND (T2.NFC3_CODE IS NULL OR T1.APP3_CODE = T2.NFC3_CODE)
|
||||
AND (T2.CORPORATION_CODE IS NULL OR T1.CORP_CODE = T2.CORPORATION_CODE)
|
||||
AND (T2.MANUFACTURER_CODE IS NULL OR T1.MANU_CODE = T2.MANUFACTURER_CODE)
|
||||
AND (T2.PRODUCT_CODE IS NULL OR T1.PROD_CODE = T2.PRODUCT_CODE)
|
||||
AND (T2.PACK_CODE IS NULL OR T1.PACK_CODE = T2.PACK_CODE)
|
||||
AND (T2.STRENGTH IS NULL OR T1.STGH_DESC = T2.STRENGTH)
|
||||
AND (T2.MOLECULE_CODE IS NULL OR T1.CMPS_CODE = T2.MOLECULE_CODE)
|
||||
WHERE T2.MARKET IS NOT NULL;
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
--删除需要隐藏的市场
|
||||
MERGE INTO DM.DM_TD_EXT_AIA_PACK2MARKET AS target
|
||||
USING dwd.dwd_gnd_ims_hidden_market AS source
|
||||
ON UPPER(target.market) = UPPER(source.hidden_market)
|
||||
AND source.data_source = 'AIA'
|
||||
WHEN MATCHED THEN DELETE;
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET_KEYCOMPETITOR
|
||||
WITH kc AS (
|
||||
SELECT
|
||||
keycompetitor, no,
|
||||
ROW_NUMBER() OVER (ORDER BY
|
||||
CASE
|
||||
WHEN ATC1_Code IS NOT NULL THEN 1
|
||||
WHEN ATC2_Code IS NOT NULL THEN 2
|
||||
WHEN ATC3_Code IS NOT NULL THEN 3
|
||||
WHEN ATC4_Code IS NOT NULL THEN 4
|
||||
WHEN Molecule_Code IS NOT NULL THEN 5
|
||||
WHEN Product_Code IS NOT NULL THEN 6
|
||||
WHEN Pack_Code IS NOT NULL THEN 7
|
||||
ELSE 999
|
||||
END,
|
||||
CASE WHEN NFC1_CODE IS NULL THEN 0 ELSE 1 END,
|
||||
CASE WHEN NFC2_CODE IS NULL THEN 0 ELSE 1 END,
|
||||
CASE WHEN NFC3_CODE IS NULL THEN 0 ELSE 1 END
|
||||
) AS no1,
|
||||
ATC1_Code, ATC2_Code, ATC3_Code, ATC4_Code,
|
||||
NFC1_Code, NFC2_Code, NFC3_Code,
|
||||
corporation_code, Manufacturer_Code,
|
||||
Product_Code, Pack_Code, Strength, Molecule_Code, market
|
||||
FROM dwd.dwd_gnd_ims_tblkeycompetitor
|
||||
),
|
||||
joined AS (
|
||||
SELECT
|
||||
/*+ BROADCAST(kc) */
|
||||
COALESCE(kc.keycompetitor, 'OTHERS') AS Key_Competitor, -- ✅ UPDATE 逻辑内联
|
||||
t1.market, t1.PACK_CODE
|
||||
/*bug 调试区
|
||||
,
|
||||
kc.no,
|
||||
kc.no1,
|
||||
t1.PROD_COD, t1.CMPS_COD, t1.CMPS_DES,
|
||||
t1.ATC1_COD, t1.ATC2_COD, t1.ATC3_COD, t1.ATC4_COD,
|
||||
t1.APP1_COD, t1.APP2_COD, t1.APP3_COD,
|
||||
t1.BIO_DESC, t1.GENE_ORIG_DESC, t1.ETH_OTC_DESC,
|
||||
t1.NRDL_DESC, t1.NRDL_Entry_Date, t1.EDL_DESC, t1.TCM_DESC,
|
||||
t1.PAED_DESC, t1.GQCE_DESC, t1.VBP_DESC,
|
||||
t1.MANU_COD, t1.MANU_DES, t1.MNFL_COD, t1.MNFL_DES,
|
||||
t1.CORP_COD, t1.CORP_DES, t1.BrandType, t1.bu, t1.Market_Ratio
|
||||
*/
|
||||
,ROW_NUMBER() OVER (
|
||||
PARTITION BY t1.market, t1.PACK_CODE, t1.PROD_CODE
|
||||
ORDER BY kc.no1 DESC, kc.no DESC
|
||||
) AS _rn
|
||||
FROM DM.DM_TD_EXT_AIA_PACK2MARKET t1
|
||||
LEFT JOIN kc
|
||||
ON (kc.ATC1_Code IS NULL OR t1.ATC1_CODE = kc.ATC1_Code)
|
||||
AND (kc.ATC2_Code IS NULL OR t1.ATC2_CODE = kc.ATC2_Code)
|
||||
AND (kc.ATC3_Code IS NULL OR t1.ATC3_CODE = kc.ATC3_Code)
|
||||
AND (kc.ATC4_Code IS NULL OR t1.ATC4_CODE = kc.ATC4_Code)
|
||||
AND (kc.NFC1_Code IS NULL OR t1.APP1_CODE = kc.NFC1_Code)
|
||||
AND (kc.NFC2_Code IS NULL OR t1.APP2_CODE = kc.NFC2_Code)
|
||||
AND (kc.NFC3_Code IS NULL OR t1.APP3_CODE = kc.NFC3_Code)
|
||||
AND (kc.corporation_code IS NULL OR t1.CORP_CODE = kc.corporation_code)
|
||||
AND (kc.Manufacturer_Code IS NULL OR t1.MANU_CODE = kc.Manufacturer_Code)
|
||||
AND (kc.Product_Code IS NULL OR t1.PROD_CODE = kc.Product_Code)
|
||||
AND (kc.Pack_Code IS NULL OR t1.PACK_CODE = kc.Pack_Code)
|
||||
AND (kc.Strength IS NULL OR t1.STGH_DESC = kc.Strength)
|
||||
AND (kc.Molecule_Code IS NULL OR t1.CMPS_CODE = kc.Molecule_Code)
|
||||
AND (kc.market IS NULL OR t1.market = kc.market)
|
||||
)
|
||||
SELECT distinct Key_Competitor,market,pack_code
|
||||
FROM joined
|
||||
WHERE _rn = 1;
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_PACK2MARKET_CLASS
|
||||
SELECT
|
||||
/*+ BROADCAST(T2) */
|
||||
DISTINCT
|
||||
T1.MARKET,
|
||||
T1.PACK_CODE,
|
||||
COALESCE( T2.CLASS, 'OTHERS') AS CLASS
|
||||
FROM DM.DM_TD_EXT_AIA_PACK2MARKET T1
|
||||
LEFT JOIN DWD.DWD_GND_TBLCLASS T2
|
||||
ON (T2.MARKET IS NULL OR T1.MARKET = T2.MARKET)
|
||||
AND (T2.PACK_CODE IS NULL OR T1.PACK_CODE = T2.PACK_CODE)
|
||||
AND (T2.PRODUCT_CODE IS NULL OR T1.PROD_CODE = T2.PRODUCT_CODE)
|
||||
AND (T2.MOLECULE_CODE IS NULL OR T1.CMPS_CODE = T2.MOLECULE_CODE)
|
||||
AND (T2.CORPORATION_CODE IS NULL OR T1.CORP_CODE = T2.CORPORATION_CODE)
|
||||
AND (T2.MANUFACTURER_CODE IS NULL OR T1.MANU_CODE = T2.MANUFACTURER_CODE)
|
||||
AND (T2.ATC1_CODE IS NULL OR T1.ATC1_CODE = T2.ATC1_CODE)
|
||||
AND (T2.ATC2_CODE IS NULL OR T1.ATC2_CODE = T2.ATC2_CODE)
|
||||
AND (T2.ATC3_CODE IS NULL OR T1.ATC3_CODE = T2.ATC3_CODE)
|
||||
AND (T2.ATC4_CODE IS NULL OR T1.ATC4_CODE = T2.ATC4_CODE)
|
||||
AND (T2.NFC1_CODE IS NULL OR T1.APP1_CODE = T2.NFC1_CODE)
|
||||
AND (T2.NFC2_CODE IS NULL OR T1.APP2_CODE = T2.NFC2_CODE)
|
||||
AND (T2.NFC3_CODE IS NULL OR T1.APP3_CODE = T2.NFC3_CODE)
|
||||
AND (T2.STRENGTH IS NULL OR T1.STGH_DESC = T2.STRENGTH)
|
||||
30
AIA/08 DM_TD_EXT_AIA_MARKET_RATIO.sql
Normal file
30
AIA/08 DM_TD_EXT_AIA_MARKET_RATIO.sql
Normal file
@@ -0,0 +1,30 @@
|
||||
-- Databricks notebook source
|
||||
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_MARKET_RATIO (
|
||||
-- MARKET STRING,
|
||||
-- PACK_CODE STRING,
|
||||
-- CMPS_CODE STRING,
|
||||
-- STARTTIME STRING,
|
||||
-- ENDTIME STRING,
|
||||
-- MARKET_RATIO STRING,
|
||||
-- ETL_INSERT_DT TIMESTAMP,
|
||||
-- ETL_UPDATE_DT TIMESTAMP)
|
||||
-- USING delta
|
||||
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_ratio';
|
||||
-- -- 上面是生产环境location,下面是测试环境location
|
||||
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_ratio';
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_MARKET_RATIO
|
||||
SELECT
|
||||
DISTINCT
|
||||
MARKET,
|
||||
PACK_CODE,
|
||||
CMPS_CODE,
|
||||
STARTTIME,
|
||||
ENDTIME,
|
||||
MARKET_RATIO,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||
FROM DM.DM_TD_EXT_AIA_PACK2MARKET
|
||||
WHERE UPPER(MARKET) NOT LIKE '%ALL%'
|
||||
163
AIA/09 DM_TD_EXT_AIA_MARKET_BRAND_RATIO.sql
Normal file
163
AIA/09 DM_TD_EXT_AIA_MARKET_BRAND_RATIO.sql
Normal file
@@ -0,0 +1,163 @@
|
||||
-- Databricks notebook source
|
||||
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_MARKET_BRAND_RATIO (
|
||||
-- MARKET STRING,
|
||||
-- PACK_CODE STRING,
|
||||
-- VALUE_BRAND_RATIO FLOAT,
|
||||
-- VALUE_BRAND_RATIO_START STRING,
|
||||
-- VALUE_BRAND_RATIO_END STRING,
|
||||
-- UNIT_BRAND_RATIO FLOAT,
|
||||
-- UNIT_BRAND_START STRING,
|
||||
-- UNIT_BRAND_END STRING,
|
||||
-- COUNTINGUNIT_BRAND_RATIO FLOAT,
|
||||
-- COUNTINGUNIT_BRAND_START STRING,
|
||||
-- COUNTINGUNIT_BRAND_END STRING,
|
||||
-- PDOT_BRAND_RATIO FLOAT,
|
||||
-- PDOT_BRAND_START STRING,
|
||||
-- PDOT_BRAND_END STRING,
|
||||
-- ETL_INSERT_DT TIMESTAMP,
|
||||
-- ETL_UPDATE_DT TIMESTAMP)
|
||||
-- USING delta
|
||||
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_brand_ratio';
|
||||
-- -- 上面是生产环境location,下面是测试环境location
|
||||
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_brand_ratio';
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_MARKET_BRAND_RATIO
|
||||
SELECT
|
||||
DISTINCT
|
||||
T1.MARKET,
|
||||
T1.PACK_CODE,
|
||||
CASE
|
||||
WHEN T2.RATIO IS NOT NULL THEN T2.RATIO
|
||||
ELSE NVL(T5.RATIO, 1) END AS VALUE_BRAND_RATIO,
|
||||
CASE
|
||||
WHEN T2.RATIO IS NOT NULL THEN T2.STARTTIME
|
||||
ELSE NVL(T5.STARTTIME, 200001) END AS VALUE_BRAND_RATIO_START,
|
||||
CASE
|
||||
WHEN T2.RATIO IS NOT NULL THEN T2.ENDTIME
|
||||
ELSE NVL(T5.ENDTIME, 299912) END AS VALUE_BRAND_RATIO_END,
|
||||
CASE
|
||||
WHEN T3.RATIO IS NOT NULL THEN T3.RATIO
|
||||
ELSE NVL(T6.RATIO, 1) END AS UNIT_BRAND_RATIO,
|
||||
CASE
|
||||
WHEN T3.RATIO IS NOT NULL THEN T3.STARTTIME
|
||||
ELSE NVL(T6.STARTTIME, 200001) END AS UNIT_BRAND_START,
|
||||
CASE
|
||||
WHEN T3.RATIO IS NOT NULL THEN T3.ENDTIME
|
||||
ELSE NVL(T6.ENDTIME, 299912) END AS UNIT_BRAND_END,
|
||||
1 AS COUNTINGUNIT_BRAND_RATIO,
|
||||
200001 AS COUNTINGUNIT_BRAND_START,
|
||||
299912 AS COUNTINGUNIT_BRAND_END,
|
||||
CASE
|
||||
WHEN T4.RATIO IS NOT NULL THEN T4.RATIO
|
||||
ELSE NVL(T7.RATIO, 1) END AS PDOT_BRAND_RATIO,
|
||||
CASE
|
||||
WHEN T4.RATIO IS NOT NULL THEN T4.STARTTIME
|
||||
ELSE NVL(T7.STARTTIME, 200001) END AS PDOT_BRAND_START,
|
||||
CASE
|
||||
WHEN T4.RATIO IS NOT NULL THEN T4.ENDTIME
|
||||
ELSE NVL(T7.ENDTIME, 299912) END AS PDOT_BRAND_END,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_INSERT_DT,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(),'UTC+8') AS ETL_UPDATE_DT
|
||||
FROM DM.DM_TD_EXT_AIA_MARKET_RATIO T1
|
||||
LEFT JOIN ( --VALUE--PACK
|
||||
SELECT
|
||||
DISTINCT
|
||||
MARKET,
|
||||
CMPS_COD,
|
||||
CMPS_DES,
|
||||
PACK_COD,
|
||||
PACK_DES,
|
||||
LEVEL,
|
||||
TYPE,
|
||||
CAST(RATIO AS FLOAT) AS RATIO,
|
||||
CAST(STARTTIME AS INT) STARTTIME,
|
||||
CAST(ENDTIME AS INT) ENDTIME
|
||||
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||
WHERE UPPER(TYPE) = 'VALUE' AND UPPER(LEVEL) = 'PACK'
|
||||
) T2 ON T1.MARKET = T2.MARKET AND T1.PACK_CODE = T2.PACK_COD
|
||||
LEFT JOIN ( --UNIT--PACK
|
||||
SELECT
|
||||
DISTINCT
|
||||
MARKET,
|
||||
CMPS_COD,
|
||||
CMPS_DES,
|
||||
PACK_COD,
|
||||
PACK_DES,
|
||||
LEVEL,
|
||||
TYPE,
|
||||
CAST(RATIO AS FLOAT) AS RATIO,
|
||||
CAST(STARTTIME AS INT) STARTTIME,
|
||||
CAST(ENDTIME AS INT) ENDTIME
|
||||
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||
WHERE UPPER(TYPE) = 'UNIT' AND UPPER(LEVEL) = 'PACK'
|
||||
) T3 ON T1.MARKET = T3.MARKET AND T1.PACK_CODE = T3.PACK_COD
|
||||
LEFT JOIN ( --COUNTINGUNIT--PACK
|
||||
SELECT
|
||||
DISTINCT
|
||||
MARKET,
|
||||
CMPS_COD,
|
||||
CMPS_DES,
|
||||
PACK_COD,
|
||||
PACK_DES,
|
||||
LEVEL,
|
||||
TYPE,
|
||||
CAST(RATIO AS FLOAT) AS RATIO,
|
||||
CAST(STARTTIME AS INT) STARTTIME,
|
||||
CAST(ENDTIME AS INT) ENDTIME
|
||||
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||
WHERE UPPER(TYPE) = 'COUNTINGUNIT' AND UPPER(LEVEL) = 'PACK'
|
||||
) T4 ON T1.MARKET = T4.MARKET AND T1.PACK_CODE = T4.PACK_COD
|
||||
LEFT JOIN ( --VALUE--MOLECULE
|
||||
SELECT
|
||||
DISTINCT
|
||||
MARKET,
|
||||
CMPS_COD,
|
||||
CMPS_DES,
|
||||
PACK_COD,
|
||||
PACK_DES,
|
||||
LEVEL,
|
||||
TYPE,
|
||||
CAST(RATIO AS FLOAT) AS RATIO,
|
||||
CAST(STARTTIME AS INT) STARTTIME,
|
||||
CAST(ENDTIME AS INT) ENDTIME
|
||||
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||
WHERE UPPER(TYPE) = 'VALUE' AND UPPER(LEVEL) = 'MOLECULE'
|
||||
) T5 ON T1.MARKET = T5.MARKET AND T1.CMPS_CODE = T5.CMPS_COD
|
||||
LEFT JOIN ( --UNIT--MOLECULE
|
||||
SELECT
|
||||
DISTINCT
|
||||
MARKET,
|
||||
CMPS_COD,
|
||||
CMPS_DES,
|
||||
PACK_COD,
|
||||
PACK_DES,
|
||||
LEVEL,
|
||||
TYPE,
|
||||
CAST(RATIO AS FLOAT) AS RATIO,
|
||||
CAST(STARTTIME AS INT) STARTTIME,
|
||||
CAST(ENDTIME AS INT) ENDTIME
|
||||
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||
WHERE UPPER(TYPE) = 'UNIT' AND UPPER(LEVEL) = 'MOLECULE'
|
||||
) T6 ON T1.MARKET = T6.MARKET AND T1.CMPS_CODE = T6.CMPS_COD
|
||||
LEFT JOIN ( --COUNTINGUNIT--MOLECULE
|
||||
SELECT
|
||||
DISTINCT
|
||||
MARKET,
|
||||
CMPS_COD,
|
||||
CMPS_DES,
|
||||
PACK_COD,
|
||||
PACK_DES,
|
||||
LEVEL,
|
||||
TYPE,
|
||||
CAST(RATIO AS FLOAT) AS RATIO,
|
||||
CAST(STARTTIME AS INT) STARTTIME,
|
||||
CAST(ENDTIME AS INT) ENDTIME
|
||||
FROM DWD.DWD_GND_IMS_TBLBRANDRATIO_BYMONTH
|
||||
WHERE UPPER(TYPE) = 'COUNTINGUNIT' AND UPPER(LEVEL) = 'MOLECULE'
|
||||
) T7 ON T1.MARKET = T7.MARKET
|
||||
AND T1.CMPS_CODE = T7.CMPS_COD
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
71
AIA/10 DM_TD_EXT_AIA_MARKET_PACK_MAPPING.sql
Normal file
71
AIA/10 DM_TD_EXT_AIA_MARKET_PACK_MAPPING.sql
Normal file
@@ -0,0 +1,71 @@
|
||||
-- Databricks notebook source
|
||||
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_MARKET_PACK_MAPPING
|
||||
-- (
|
||||
-- PACK_CODE STRING,
|
||||
-- MARKET STRING,
|
||||
-- CLASS STRING,
|
||||
-- KEY_COMPETITOR STRING,
|
||||
-- MARKET_RATIO STRING,
|
||||
-- STARTTIME STRING,
|
||||
-- ENDTIME STRING,
|
||||
-- VALUE_BRAND_RATIO STRING,
|
||||
-- VALUE_BRAND_RATIO_START STRING,
|
||||
-- VALUE_BRAND_RATIO_END STRING,
|
||||
-- UNIT_BRAND_RATIO STRING,
|
||||
-- UNIT_BRAND_START STRING,
|
||||
-- UNIT_BRAND_END STRING,
|
||||
-- COUNTINGUNIT_BRAND_RATIO STRING,
|
||||
-- COUNTINGUNIT_BRAND_START STRING,
|
||||
-- COUNTINGUNIT_BRAND_END STRING,
|
||||
-- PDOT_BRAND_RATIO STRING,
|
||||
-- PDOT_BRAND_START STRING,
|
||||
-- PDOT_BRAND_END STRING,
|
||||
-- ETL_INSERT_DT TIMESTAMP,
|
||||
-- ETL_UPDATE_DT TIMESTAMP
|
||||
-- )
|
||||
-- USING delta
|
||||
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_pack_mapping';
|
||||
-- -- 上面是生产环境location,下面是测试环境location
|
||||
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_market_pack_mapping';
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
INSERT OVERWRITE TABLE DM.DM_TD_EXT_AIA_MARKET_PACK_MAPPING
|
||||
SELECT DISTINCT
|
||||
T1.PACK_CODE,
|
||||
T1.MARKET,
|
||||
T4.CLASS,
|
||||
T5.KEY_COMPETITOR,
|
||||
T2.MARKET_RATIO,
|
||||
T2.STARTTIME,
|
||||
T2.ENDTIME,
|
||||
T3.VALUE_BRAND_RATIO,
|
||||
T3.VALUE_BRAND_RATIO_START,
|
||||
T3.VALUE_BRAND_RATIO_END,
|
||||
T3.UNIT_BRAND_RATIO,
|
||||
T3.UNIT_BRAND_START,
|
||||
T3.UNIT_BRAND_END,
|
||||
T3.COUNTINGUNIT_BRAND_RATIO,
|
||||
T3.COUNTINGUNIT_BRAND_START,
|
||||
T3.COUNTINGUNIT_BRAND_END,
|
||||
T3.PDOT_BRAND_RATIO,
|
||||
T3.PDOT_BRAND_START,
|
||||
T3.PDOT_BRAND_END,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
|
||||
FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
|
||||
FROM
|
||||
DM.DM_TD_EXT_AIA_PACK2MARKET T1
|
||||
LEFT JOIN DM.DM_TD_EXT_AIA_MARKET_RATIO T2
|
||||
ON T1.MARKET = T2.MARKET
|
||||
AND T1.PACK_CODE = T2.PACK_CODE
|
||||
LEFT JOIN DM.DM_TD_EXT_AIA_MARKET_BRAND_RATIO T3
|
||||
ON T1.MARKET = T3.MARKET
|
||||
AND T1.PACK_CODE = T3.PACK_CODE
|
||||
LEFT JOIN DM.DM_TD_EXT_AIA_PACK2MARKET_CLASS T4
|
||||
ON T1.PACK_CODE = T4.PACK_CODE
|
||||
AND T1.MARKET = T4.MARKET
|
||||
LEFT JOIN DM.DM_TD_EXT_AIA_PACK2MARKET_KEYCOMPETITOR T5
|
||||
ON T1.PACK_CODE = T5.PACK_CODE
|
||||
AND T1.MARKET = T5.MARKET
|
||||
WHERE
|
||||
UPPER(T1.MARKET) NOT LIKE "%ALL%"
|
||||
11
AIA/11 DM_TD_EXT_AIA_TARGET_INST.sql
Normal file
11
AIA/11 DM_TD_EXT_AIA_TARGET_INST.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
-- Databricks notebook source
|
||||
-- MAGIC %run ../../../Common/config
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- MAGIC %python
|
||||
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dim_product_wide`').createOrReplaceTempView('cdw_dwd_dim_product_wide')
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
|
||||
366
AIA/11 dm_aia_targethp_flag.sql
Normal file
366
AIA/11 dm_aia_targethp_flag.sql
Normal file
@@ -0,0 +1,366 @@
|
||||
-- Databricks notebook source
|
||||
-- MAGIC %run ../../../Common/config
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- MAGIC %python
|
||||
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dim_product_wide`').createOrReplaceTempView('cdw_dwd_dim_product_wide')
|
||||
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dwd_hospital_classification`').createOrReplaceTempView('cdw_dwd_dwd_hospital_classification')
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
---------------------------------------------------------------------------
|
||||
--修改时间:20241108
|
||||
--修改人:Fanxujia
|
||||
--修改内容:
|
||||
--for AIA Dashboard
|
||||
--增加sub_channel,dragon_flag,ka_flag,target_ins_level字段
|
||||
---------------------------------------------------------------------------
|
||||
---------------------------------------------------------------------------
|
||||
--修改时间:20241209
|
||||
--修改人:Fanxujia
|
||||
--修改内容:
|
||||
--增加sub channel的排序
|
||||
---------------------------------------------------------------------------
|
||||
--目标医院
|
||||
insert overwrite table dm.dm_aia_targethp_flag --获取全部的机构 + 市场
|
||||
with sales as (
|
||||
select distinct t1.inst_code,t2.MARKET
|
||||
from dm.dm_ext_aia_sales t1
|
||||
left join DM.DM_TD_EXT_AIA_MARKET_PACK_MAPPING t2
|
||||
on t1.pack_cod = t2.PACK_CODE
|
||||
)
|
||||
--获取产品与市场的映射关系
|
||||
,pack_market as (
|
||||
select distinct
|
||||
PACK_CODE as PACK_COD,
|
||||
MARKET
|
||||
from DM.DM_TD_EXT_AIA_MARKET_PACK_MAPPING
|
||||
)
|
||||
--辖区表,获取到KA_Flag、target_ins_level以及目标机构Flag(在辖区表里存在的就是目标机构)
|
||||
,td_territory_inst as (
|
||||
select distinct
|
||||
inst_code,
|
||||
trty_code,
|
||||
family_code,
|
||||
ka_flag,
|
||||
TARGET_LEVEL as target_ins_level
|
||||
from dm.dm_td_sd_territory_indication --eagle的非重点医院同样加为目标机构
|
||||
where yyyymm in (
|
||||
select
|
||||
key_value
|
||||
from
|
||||
dm.dm_td_date_config
|
||||
where
|
||||
key_code = 'pfmc_cvh'
|
||||
)
|
||||
and bu not in ('CUBE','KA')
|
||||
and key_hp<>3 --chenwu 20250904 排除双考影响
|
||||
)
|
||||
--汇报线表,获取到Channel,后续用于判断出sub_channel
|
||||
,td_org as (
|
||||
select distinct
|
||||
t1.mr_trty_code,
|
||||
case when t1.bu_code in ('PC14','PC16','PC18','PC19') then 'BU'
|
||||
when t1.bu_code = 'SC01' and t1.sub_bu_code = 'NA29' then sub_bu_name
|
||||
when t1.bu_code = 'SC01' then 'BU'
|
||||
when t1.bu_code <> 'SC01' and t2.bm_name = 'BCBH' then 'BU'
|
||||
when t1.bu_code <> 'SC01' then t1.bu_name
|
||||
end as channel
|
||||
from dm.dm_td_org t1
|
||||
left join dws.dws_td_bu_active t2
|
||||
on t1.bu_name = t2.bu_name
|
||||
where t1.yyyymm in (
|
||||
select
|
||||
key_value
|
||||
from
|
||||
dm.dm_td_date_config
|
||||
where
|
||||
key_code = 'pfmc_cvh'
|
||||
)
|
||||
)
|
||||
--Family粒度和Brand粒度的映射表
|
||||
,product_wide as (
|
||||
select distinct
|
||||
prd_family_cd,
|
||||
prd_brand_cd
|
||||
from cdw_dwd_dim_product_wide
|
||||
)
|
||||
--内部Brand粒度与外部pack_cod的映射表
|
||||
,imscode_azcode as (
|
||||
select distinct
|
||||
brandcode,
|
||||
case when length(ims_pack_cod) < 12 and ims_pack_cod REGEXP '^[0-9]' then right(concat('00000000000',ims_pack_cod),12) else ims_pack_cod end as ims_pack_cod
|
||||
from dwd.dwd_td_imscode_azcode
|
||||
)
|
||||
--综合上面的几个表,得到KA_flag、target_ins_level、channel、目标机构Flag
|
||||
,target_hp_all as (
|
||||
select
|
||||
distinct
|
||||
t1.inst_code,
|
||||
t1.ka_flag,
|
||||
t1.target_ins_level,
|
||||
t2.channel,
|
||||
t5.MARKET
|
||||
from
|
||||
td_territory_inst t1
|
||||
inner join td_org t2 on t1.trty_code = t2.mr_trty_code
|
||||
left join product_wide t3 on t3.prd_family_cd = t1.family_code
|
||||
left join imscode_azcode t4 on t3.prd_brand_cd = t4.brandcode
|
||||
left join pack_market t5 on t4.ims_pack_cod = t5.pack_cod
|
||||
where t5.MARKET is not null
|
||||
)
|
||||
--target_hp_all里一个机构一个市场对应了多个属性,这里按优先级依次取唯一值
|
||||
--ka_flag
|
||||
,ka_flag1 as (
|
||||
select distinct
|
||||
inst_code,
|
||||
MARKET,
|
||||
ka_flag
|
||||
from target_hp_all
|
||||
)
|
||||
,ka_flag2 as (
|
||||
select
|
||||
inst_code,
|
||||
MARKET,
|
||||
ka_flag,
|
||||
row_number() over(partition by inst_code,market
|
||||
order by case when ka_flag = 'KA' then 1
|
||||
ELSE 2 end asc) as RN
|
||||
from ka_flag1
|
||||
)
|
||||
,ka_flag_final as (
|
||||
select
|
||||
inst_code,
|
||||
MARKET,
|
||||
ka_flag
|
||||
from ka_flag2
|
||||
where RN = 1
|
||||
)
|
||||
--target_ins_level不取唯一值
|
||||
--加个hardcoding,ONC Market和ALL Market设置成NA
|
||||
,target_ins_level1 as (
|
||||
select distinct
|
||||
inst_code,
|
||||
MARKET,
|
||||
case when MARKET in ('ALL Market','ONC Market') then 'NA'
|
||||
else target_ins_level
|
||||
end as target_ins_level
|
||||
from target_hp_all
|
||||
)
|
||||
,target_ins_level_final as (
|
||||
select distinct
|
||||
inst_code,
|
||||
MARKET,
|
||||
target_ins_level
|
||||
from target_ins_level1
|
||||
)
|
||||
--target_hp_all里一个机构一个市场对应了多个属性,这里按优先级依次取唯一值
|
||||
--Channel
|
||||
,Channel1 as (
|
||||
select distinct
|
||||
inst_code,
|
||||
MARKET,
|
||||
channel
|
||||
from target_hp_all
|
||||
)
|
||||
,channel2 as (
|
||||
select
|
||||
inst_code,
|
||||
MARKET,
|
||||
channel,
|
||||
row_number() over(partition by inst_code,market
|
||||
order by case when channel = 'BU' then 1
|
||||
when channel = 'BBU_County' then 2
|
||||
when channel = 'CHC' then 3
|
||||
when channel = 'Eagle' then 4
|
||||
ELSE 5 end asc) as RN
|
||||
from channel1
|
||||
)
|
||||
,channel_final as (
|
||||
select
|
||||
inst_code,
|
||||
MARKET,
|
||||
channel
|
||||
from channel2
|
||||
where RN = 1
|
||||
)
|
||||
--确定唯一值后重新放一起
|
||||
,target_hp1 as (
|
||||
select distinct
|
||||
inst_code,
|
||||
MARKET
|
||||
from target_hp_all
|
||||
)
|
||||
,target_hp as (
|
||||
select t1.inst_code,
|
||||
t1.market,
|
||||
t2.ka_flag,
|
||||
t3.target_ins_level,
|
||||
t4.channel
|
||||
from target_hp1 t1
|
||||
left join ka_flag_final t2
|
||||
on t1.inst_code = t2.inst_code
|
||||
and t1.market = t2.market
|
||||
left join target_ins_level_final t3
|
||||
on t1.inst_code = t3.inst_code
|
||||
and t1.market = t3.market
|
||||
left join channel_final t4
|
||||
on t1.inst_code = t4.inst_code
|
||||
and t1.market = t4.market
|
||||
)
|
||||
|
||||
--获取Dragon_type
|
||||
--判断Dragon_type是否为County
|
||||
,county as (
|
||||
select distinct inst_code,county_tier_desc
|
||||
from dm.dm_td_institution
|
||||
)
|
||||
--判断另外2种
|
||||
-- 20250825修改
|
||||
,dragon_type as (
|
||||
select src_hcc_code as inst_code,
|
||||
src_prod_code as family_code,
|
||||
max(is_core) is_core
|
||||
from cdw_dwd_dwd_hospital_classification
|
||||
where active_status = 1
|
||||
and src_sales_cycle = (select key_value from dm.dm_td_date_config where key_code = 'pfmc_cvh')
|
||||
group by src_hcc_code,
|
||||
src_prod_code
|
||||
)
|
||||
--将Family_code转为pack_cod
|
||||
,dragon_type_market1 as (
|
||||
select distinct
|
||||
t1.inst_code,
|
||||
t1.is_core,
|
||||
t4.market
|
||||
from dragon_type t1
|
||||
left join cdw_dwd_dim_product_wide t2
|
||||
on t1.family_code = t2.prd_family_cd
|
||||
left join dwd.dwd_td_imscode_azcode t3
|
||||
on t2.prd_brand_cd = t3.brandcode
|
||||
left join pack_market t4
|
||||
on t3.ims_pack_cod = t4.pack_cod
|
||||
where t4.market is not null
|
||||
)
|
||||
--按优先级取唯一值
|
||||
,dragon_type_market2 as (
|
||||
select distinct
|
||||
inst_code,
|
||||
is_core,
|
||||
market,
|
||||
row_number() over(partition by inst_code,market
|
||||
order by case when is_core = 'CORE' then 1
|
||||
when is_core = 'EMERGING' then 2
|
||||
else 3 end asc) as RN
|
||||
from dragon_type_market1
|
||||
)
|
||||
,dragon_type_market as (
|
||||
select distinct
|
||||
inst_code,
|
||||
is_core,
|
||||
market
|
||||
from dragon_type_market2
|
||||
where RN = 1
|
||||
)
|
||||
--综合前面几个表,得出dragon_flag
|
||||
,dragon_flag1 as (
|
||||
select distinct
|
||||
t0.inst_code,
|
||||
t0.market,
|
||||
case when t1.county_tier_desc in ('County','County level city') then 'County'
|
||||
when t2.is_core is null or length(t2.is_core) = 0 then 'Others'
|
||||
else t2.is_core
|
||||
end as dragon_flag
|
||||
from target_hp t0
|
||||
left join county t1
|
||||
on t0.inst_code = t1.inst_code
|
||||
left join dragon_type_market t2
|
||||
on t0.inst_code = t2.inst_code
|
||||
and t0.MARKET = t2.MARKET
|
||||
)
|
||||
--按优先级取唯一值
|
||||
,dragon_flag2 as (
|
||||
select inst_code,
|
||||
market,
|
||||
dragon_flag,
|
||||
row_number() over(partition by inst_code,market
|
||||
order by case when dragon_flag = 'CORE' then 1
|
||||
when dragon_flag = 'EMERGING' then 2
|
||||
when dragon_flag = 'County' then 3
|
||||
else 4 end asc) as RN
|
||||
from dragon_flag1
|
||||
)
|
||||
,dragon_flag as (
|
||||
select inst_code,
|
||||
market,
|
||||
dragon_flag
|
||||
from dragon_flag2
|
||||
where RN = 1
|
||||
)
|
||||
|
||||
--下面是为了把其他渠道并进来
|
||||
,other_data_source as (
|
||||
select explode(array('IQVIA-CHPA(Monthly)','XH Data(Quarterly)','EC(Monthly)','Retail(Quarterly)','CHC(Quarterly)','THC(Quarterly)','IQVIA-COUNTY(Quarterly)'))
|
||||
)
|
||||
,col as (
|
||||
select '' as inst_code,
|
||||
'' as MARKET,
|
||||
'' as is_target_hp,
|
||||
'' as sub_channel,
|
||||
'' as dragon_flag,
|
||||
'' as ka_flag,
|
||||
'' as target_ins_level
|
||||
)
|
||||
|
||||
,temp_1 as (
|
||||
select distinct
|
||||
case when t1.inst_code is null or t1.inst_code = '' then 'ROC'
|
||||
else t1.inst_code
|
||||
end as inst_code,
|
||||
t1.MARKET,
|
||||
case when t2.inst_code is null then 'N' else 'Y' end as is_target_hp,
|
||||
case when t2.channel = 'BU' and geo.county_tier_desc in ('County','County level city') then 'BU_County'
|
||||
when t2.channel = 'BU' and geo.county_tier_desc in ('City-3b','City-4a','City-4b') then 'BU_Emerging'
|
||||
when t2.channel = 'BU' and t3.dragon_flag = 'CORE' then 'BU_Core'
|
||||
when t2.channel = 'BU' then 'BU_Emerging'
|
||||
when t2.channel in ('Eagle','CHC','BBU_County') then t2.channel
|
||||
when t2.inst_code is null then 'NON_TARGET'
|
||||
else 'NA'
|
||||
end as sub_channel,
|
||||
case when t3.dragon_flag is null then 'NON_TARGET'
|
||||
else t3.dragon_flag
|
||||
end as dragon_flag,
|
||||
case when t2.ka_flag is null then 'NON_TARGET'
|
||||
else t2.ka_flag
|
||||
end as ka_flag,
|
||||
case when t2.target_ins_level is null then 'NON_TARGET'
|
||||
else t2.target_ins_level
|
||||
end as target_ins_level,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from sales t1
|
||||
left join target_hp t2
|
||||
on t1.inst_code = t2.inst_code
|
||||
and t1.MARKET = t2.MARKET
|
||||
left join dragon_flag t3
|
||||
on t1.inst_code = t3.inst_code
|
||||
and t1.MARKET = t3.MARKET
|
||||
left join dm.dm_td_institution inst
|
||||
on t1.inst_code =inst.inst_code
|
||||
left join dm.dm_td_geography geo
|
||||
on inst.county_code = geo.geo_key
|
||||
union all
|
||||
select *
|
||||
from col
|
||||
cross join other_data_source
|
||||
)
|
||||
,sub_channel_rank as (
|
||||
select distinct SUB_CHANNELSTD,SUB_CHANNEL_ORDER
|
||||
from dm.dm_td_sd_channel
|
||||
)
|
||||
|
||||
|
||||
select t1.*,NVL(t2.SUB_CHANNEL_ORDER,100) as SUB_CHANNEL_ORDER
|
||||
from temp_1 t1
|
||||
left join sub_channel_rank t2
|
||||
on t1.sub_channel = t2.SUB_CHANNELSTD
|
||||
643
AIA/z_03 dm_aia_flag_备份用.sql
Normal file
643
AIA/z_03 dm_aia_flag_备份用.sql
Normal file
@@ -0,0 +1,643 @@
|
||||
-- Databricks notebook source
|
||||
-- MAGIC %run ../../../Common/config
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-- MAGIC %python
|
||||
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dim_product_wide`').createOrReplaceTempView('cdw_dwd_dim_product_wide')
|
||||
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dwd_hospital_classification`').createOrReplaceTempView('cdw_dwd_dwd_hospital_classification')
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-----------------------------------------------------------------------------------
|
||||
--修改时间:20241021
|
||||
--修改人:FanXujia
|
||||
--修改内容:
|
||||
--只取最新年-1 ~ 最新年范围内的,称为“2年共有”。增加
|
||||
-----------------------------------------------------------------------------------
|
||||
--共有机构
|
||||
insert overwrite table dm.dm_aia_hp_flag
|
||||
with max_year as (
|
||||
select
|
||||
max(cast(year as int)) as max_year,
|
||||
max(cast(year as int) - 1) as max_year_ly
|
||||
from dwd.dwd_gnd_hospital_not_provided
|
||||
)
|
||||
,hp_flag as (
|
||||
select
|
||||
distinct concat(t1.year, '-Y') as hp_his_flag,
|
||||
case when length(t1.cpa_hospital_code) < 7 then right(concat('0000000',t1.cpa_hospital_code),7) else t1.cpa_hospital_code end as cpa_hospital_code
|
||||
from
|
||||
dwd.dwd_gnd_hospital_not_provided t1
|
||||
cross join max_year t2
|
||||
where cast(t1.year as int) >= t2.max_year_ly
|
||||
)
|
||||
,hp_flag2 as (
|
||||
SELECT
|
||||
cpa_hospital_code,
|
||||
CONCAT_WS(' ∩ ', SORT_ARRAY(COLLECT_LIST(hp_his_flag))) AS FLAG
|
||||
FROM
|
||||
hp_flag
|
||||
GROUP BY
|
||||
cpa_hospital_code
|
||||
)
|
||||
,inst_mapping as (
|
||||
-- select
|
||||
-- distinct right(concat('0000000', org_cd), 7) org_cd,
|
||||
-- ins_cd
|
||||
-- from
|
||||
-- dwd.dwd_gnd_hospitalmapping
|
||||
select distinct
|
||||
case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code,
|
||||
ins_cd_nl as inst_code
|
||||
from dwd.dwd_gnd_hospital_not_provided
|
||||
)
|
||||
|
||||
,ALL_INS as (
|
||||
select distinct inst_code
|
||||
from dm.dm_ext_aia_sales
|
||||
)
|
||||
|
||||
,flag as (
|
||||
select distinct
|
||||
coalesce(t2.inst_code,t1.cpa_hospital_code) as inst_code,
|
||||
t1.flag as aia_hp_flag,
|
||||
'Y' as FLAG,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from hp_flag2 t1
|
||||
left join inst_mapping t2 on t1.cpa_hospital_code = t2.cpa_hospital_code
|
||||
where
|
||||
length(t1.flag) > 6
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-CHPA(Monthly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'XH Data(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'EC(Monthly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'Retail(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'CHC(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'THC(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-COUNTY(Quarterly)' as DATA_SOURCE
|
||||
)
|
||||
|
||||
select t1.inst_code,
|
||||
NVL(t2.aia_hp_flag,'') as aia_hp_flag,
|
||||
NVL(t2.FLAG,'N') as FLAG,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from ALL_INS t1
|
||||
left join flag t2
|
||||
on t1.inst_code = t2.inst_code
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
-----------------------------------------------------------------------------------
|
||||
--修改时间:20241021
|
||||
--修改人:FanXujia
|
||||
--修改内容:
|
||||
--只取最新年-1 ~ 最新年范围内的,称为“2年共有”。增加
|
||||
-----------------------------------------------------------------------------------
|
||||
--共有机构
|
||||
insert overwrite table dm.dm_aia_hp_flag
|
||||
with max_year as (
|
||||
select
|
||||
max(cast(year as int)) as max_year,
|
||||
max(cast(year as int) - 1) as max_year_ly
|
||||
from dwd.dwd_gnd_hospital_not_provided
|
||||
)
|
||||
,hp_flag as (
|
||||
select
|
||||
distinct concat(t1.year, '-Y') as hp_his_flag,
|
||||
case when length(t1.cpa_hospital_code) < 7 then right(concat('0000000',t1.cpa_hospital_code),7) else t1.cpa_hospital_code end as cpa_hospital_code
|
||||
from
|
||||
dwd.dwd_gnd_hospital_not_provided t1
|
||||
cross join max_year t2
|
||||
where cast(t1.year as int) >= t2.max_year_ly
|
||||
)
|
||||
,hp_flag2 as (
|
||||
SELECT
|
||||
cpa_hospital_code,
|
||||
CONCAT_WS(' ∩ ', SORT_ARRAY(COLLECT_LIST(hp_his_flag))) AS FLAG
|
||||
FROM
|
||||
hp_flag
|
||||
GROUP BY
|
||||
cpa_hospital_code
|
||||
)
|
||||
,inst_mapping as (
|
||||
-- select
|
||||
-- distinct right(concat('0000000', org_cd), 7) org_cd,
|
||||
-- ins_cd
|
||||
-- from
|
||||
-- dwd.dwd_gnd_hospitalmapping
|
||||
select distinct
|
||||
case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code,
|
||||
ins_cd_nl as inst_code
|
||||
from dwd.dwd_gnd_hospital_not_provided
|
||||
)
|
||||
|
||||
,ALL_INS as (
|
||||
select distinct inst_code
|
||||
from dm.dm_ext_aia_sales
|
||||
)
|
||||
|
||||
,flag as (
|
||||
select distinct
|
||||
coalesce(t2.inst_code,t1.cpa_hospital_code) as inst_code,
|
||||
t1.flag as aia_hp_flag,
|
||||
'Y' as FLAG,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from hp_flag2 t1
|
||||
left join inst_mapping t2 on t1.cpa_hospital_code = t2.cpa_hospital_code
|
||||
where
|
||||
length(t1.flag) > 6
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-CHPA(Monthly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'XH Data(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'EC(Monthly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'Retail(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'CHC(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'THC(Quarterly)' as DATA_SOURCE
|
||||
union all
|
||||
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-COUNTY(Quarterly)' as DATA_SOURCE
|
||||
)
|
||||
|
||||
select t1.inst_code,
|
||||
NVL(t2.aia_hp_flag,'') as aia_hp_flag,
|
||||
NVL(t2.FLAG,'N') as FLAG,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from ALL_INS t1
|
||||
left join flag t2
|
||||
on t1.inst_code = t2.inst_code
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
--连续取数机构
|
||||
----------------------------------------------------------------------------------------------------------------------
|
||||
--修改时间:20241029
|
||||
--修改人:FanXujia
|
||||
--修改内容:
|
||||
--根据事实表最新月,往前推12个月(含最新月),按机构 + 年月粒度汇总,如果每个月销量都不等于0,这家机构称为:滚动一年有数
|
||||
--根据事实表最新月,往前推24个月(含最新月),按机构 + 年月粒度汇总,如果每个月销量都不等于0,这家机构称为:滚动两年有数
|
||||
--2022年没有销量为正常情况,判断时仅考虑2023年01月起销量是否等于0
|
||||
----------------------------------------------------------------------------------------------------------------------
|
||||
insert overwrite table dm.dm_aia_provided_flag
|
||||
with max_ym as (
|
||||
select max(ym) as max_ym
|
||||
from dm.dm_ext_aia_sales
|
||||
)
|
||||
,ym_range as (
|
||||
select max_ym,
|
||||
case when max_ym <= '202412' then cast(right(max_ym,2) as int)
|
||||
else cast(12 as int)
|
||||
end ym_12m,
|
||||
case when max_ym <= '202312' then '202401'
|
||||
when right(max_ym,2) = '12' then concat(left(max_ym,4),'01')
|
||||
else concat(cast(left(max_ym, 4) - 1 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2))
|
||||
end as ym_12m_before,
|
||||
case when max_ym <= '202312' then cast(right(max_ym,2) as int)
|
||||
when max_ym <= '202512' then cast(right(max_ym,2) as int) + 12
|
||||
else cast(24 as int)
|
||||
end as ym_24m,
|
||||
case when max_ym <= '202512' then '202401'
|
||||
when right(max_ym,2) = '12' then concat(cast(left(max_ym,4) - 1 as int),'01')
|
||||
else concat(cast(left(max_ym, 4) -2 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2))
|
||||
end as ym_24m_before
|
||||
from max_ym
|
||||
)
|
||||
,sales_1 as (
|
||||
select
|
||||
inst_code,
|
||||
ym
|
||||
from
|
||||
dm.dm_ext_aia_sales
|
||||
group by
|
||||
ym,
|
||||
inst_code
|
||||
having
|
||||
sum(value) <> 0
|
||||
)
|
||||
,sales_2 as (
|
||||
select
|
||||
t1.inst_code,
|
||||
t1.ym,
|
||||
t2.max_ym,
|
||||
t2.ym_12m,
|
||||
t2.ym_12m_before,
|
||||
t2.ym_24m,
|
||||
t2.ym_24m_before
|
||||
from sales_1 t1
|
||||
cross join ym_range t2
|
||||
)
|
||||
,flag_12m as (
|
||||
select inst_code,
|
||||
'滚动一年有数' as aia_provided_flag,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from sales_2
|
||||
where ym <= max_ym
|
||||
and ym >= ym_12m_before
|
||||
group by inst_code
|
||||
having count(1) = (select ym_12m from ym_range)
|
||||
)
|
||||
,flag_24m as (
|
||||
select inst_code,
|
||||
'滚动两年有数' as aia_provided_flag,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from sales_2
|
||||
where ym <= max_ym
|
||||
and ym >= ym_24m_before
|
||||
group by inst_code
|
||||
having count(1) = (select ym_24m from ym_range)
|
||||
)
|
||||
|
||||
select inst_code,
|
||||
aia_provided_flag,
|
||||
DATA_SOURCE
|
||||
from flag_12m
|
||||
union all
|
||||
select inst_code,
|
||||
aia_provided_flag,
|
||||
DATA_SOURCE
|
||||
from flag_24m
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'IQVIA-CHPA(Monthly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'XH Data(Quarterly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'EC(Monthly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'Retail(Quarterly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'CHC(Quarterly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'THC(Quarterly)' as data_source
|
||||
union all
|
||||
select '' as inst_code,'' as aia_provided_flag,'IQVIA-COUNTY(Quarterly)' as data_source
|
||||
|
||||
|
||||
-- COMMAND ----------
|
||||
|
||||
---------------------------------------------------------------------------
|
||||
--修改时间:20241108
|
||||
--修改人:Fanxujia
|
||||
--修改内容:
|
||||
--for AIA Dashboard
|
||||
--增加sub_channel,dragon_flag,ka_flag,target_ins_level字段
|
||||
---------------------------------------------------------------------------
|
||||
---------------------------------------------------------------------------
|
||||
--修改时间:20241209
|
||||
--修改人:Fanxujia
|
||||
--修改内容:
|
||||
--增加sub channel的排序
|
||||
---------------------------------------------------------------------------
|
||||
--目标医院
|
||||
insert overwrite table dm.dm_aia_targethp_flag --获取全部的机构 + 市场
|
||||
with sales as (
|
||||
select distinct t1.inst_code,t2.MARKET
|
||||
from dm.dm_ext_aia_sales t1
|
||||
left join dm.dm_aia_pack_property t2
|
||||
on t1.pack_cod = t2.PACK_COD
|
||||
)
|
||||
--获取产品与市场的映射关系
|
||||
,pack_market as (
|
||||
select distinct
|
||||
PACK_COD,
|
||||
MARKET
|
||||
from dm.dm_aia_pack_property
|
||||
)
|
||||
--辖区表,获取到KA_Flag、target_ins_level以及目标机构Flag(在辖区表里存在的就是目标机构)
|
||||
,td_territory_inst as (
|
||||
select distinct
|
||||
inst_code,
|
||||
trty_code,
|
||||
family_code,
|
||||
ka_flag,
|
||||
TARGET_LEVEL as target_ins_level
|
||||
from dm.dm_td_sd_territory_indication --eagle的非重点医院同样加为目标机构
|
||||
where yyyymm in (
|
||||
select
|
||||
key_value
|
||||
from
|
||||
dm.dm_td_date_config
|
||||
where
|
||||
key_code = 'pfmc_cvh'
|
||||
)
|
||||
and bu not in ('CUBE','KA')
|
||||
and key_hp<>3 --chenwu 20250904 排除双考影响
|
||||
)
|
||||
--汇报线表,获取到Channel,后续用于判断出sub_channel
|
||||
,td_org as (
|
||||
select distinct
|
||||
t1.mr_trty_code,
|
||||
case when t1.bu_code in ('PC14','PC16','PC18','PC19') then 'BU'
|
||||
when t1.bu_code = 'SC01' and t1.sub_bu_code = 'NA29' then sub_bu_name
|
||||
when t1.bu_code = 'SC01' then 'BU'
|
||||
when t1.bu_code <> 'SC01' and t2.bm_name = 'BCBH' then 'BU'
|
||||
when t1.bu_code <> 'SC01' then t1.bu_name
|
||||
end as channel
|
||||
from dm.dm_td_org t1
|
||||
left join dws.dws_td_bu_active t2
|
||||
on t1.bu_name = t2.bu_name
|
||||
where t1.yyyymm in (
|
||||
select
|
||||
key_value
|
||||
from
|
||||
dm.dm_td_date_config
|
||||
where
|
||||
key_code = 'pfmc_cvh'
|
||||
)
|
||||
)
|
||||
--Family粒度和Brand粒度的映射表
|
||||
,product_wide as (
|
||||
select distinct
|
||||
prd_family_cd,
|
||||
prd_brand_cd
|
||||
from cdw_dwd_dim_product_wide
|
||||
)
|
||||
--内部Brand粒度与外部pack_cod的映射表
|
||||
,imscode_azcode as (
|
||||
select distinct
|
||||
brandcode,
|
||||
case when length(ims_pack_cod) < 12 and ims_pack_cod REGEXP '^[0-9]' then right(concat('00000000000',ims_pack_cod),12) else ims_pack_cod end as ims_pack_cod
|
||||
from dwd.dwd_td_imscode_azcode
|
||||
)
|
||||
--综合上面的几个表,得到KA_flag、target_ins_level、channel、目标机构Flag
|
||||
,target_hp_all as (
|
||||
select
|
||||
distinct
|
||||
t1.inst_code,
|
||||
t1.ka_flag,
|
||||
t1.target_ins_level,
|
||||
t2.channel,
|
||||
t5.MARKET
|
||||
from
|
||||
td_territory_inst t1
|
||||
inner join td_org t2 on t1.trty_code = t2.mr_trty_code
|
||||
left join product_wide t3 on t3.prd_family_cd = t1.family_code
|
||||
left join imscode_azcode t4 on t3.prd_brand_cd = t4.brandcode
|
||||
left join pack_market t5 on t4.ims_pack_cod = t5.pack_cod
|
||||
where t5.MARKET is not null
|
||||
)
|
||||
--target_hp_all里一个机构一个市场对应了多个属性,这里按优先级依次取唯一值
|
||||
--ka_flag
|
||||
,ka_flag1 as (
|
||||
select distinct
|
||||
inst_code,
|
||||
MARKET,
|
||||
ka_flag
|
||||
from target_hp_all
|
||||
)
|
||||
,ka_flag2 as (
|
||||
select
|
||||
inst_code,
|
||||
MARKET,
|
||||
ka_flag,
|
||||
row_number() over(partition by inst_code,market
|
||||
order by case when ka_flag = 'KA' then 1
|
||||
ELSE 2 end asc) as RN
|
||||
from ka_flag1
|
||||
)
|
||||
,ka_flag_final as (
|
||||
select
|
||||
inst_code,
|
||||
MARKET,
|
||||
ka_flag
|
||||
from ka_flag2
|
||||
where RN = 1
|
||||
)
|
||||
--target_ins_level不取唯一值
|
||||
--加个hardcoding,ONC Market和ALL Market设置成NA
|
||||
,target_ins_level1 as (
|
||||
select distinct
|
||||
inst_code,
|
||||
MARKET,
|
||||
case when MARKET in ('AIA ALL Market','ONC Market') then 'NA'
|
||||
else target_ins_level
|
||||
end as target_ins_level
|
||||
from target_hp_all
|
||||
)
|
||||
,target_ins_level_final as (
|
||||
select distinct
|
||||
inst_code,
|
||||
MARKET,
|
||||
target_ins_level
|
||||
from target_ins_level1
|
||||
)
|
||||
--target_hp_all里一个机构一个市场对应了多个属性,这里按优先级依次取唯一值
|
||||
--Channel
|
||||
,Channel1 as (
|
||||
select distinct
|
||||
inst_code,
|
||||
MARKET,
|
||||
channel
|
||||
from target_hp_all
|
||||
)
|
||||
,channel2 as (
|
||||
select
|
||||
inst_code,
|
||||
MARKET,
|
||||
channel,
|
||||
row_number() over(partition by inst_code,market
|
||||
order by case when channel = 'BU' then 1
|
||||
when channel = 'BBU_County' then 2
|
||||
when channel = 'CHC' then 3
|
||||
when channel = 'Eagle' then 4
|
||||
ELSE 5 end asc) as RN
|
||||
from channel1
|
||||
)
|
||||
,channel_final as (
|
||||
select
|
||||
inst_code,
|
||||
MARKET,
|
||||
channel
|
||||
from channel2
|
||||
where RN = 1
|
||||
)
|
||||
--确定唯一值后重新放一起
|
||||
,target_hp1 as (
|
||||
select distinct
|
||||
inst_code,
|
||||
MARKET
|
||||
from target_hp_all
|
||||
)
|
||||
,target_hp as (
|
||||
select t1.inst_code,
|
||||
t1.market,
|
||||
t2.ka_flag,
|
||||
t3.target_ins_level,
|
||||
t4.channel
|
||||
from target_hp1 t1
|
||||
left join ka_flag_final t2
|
||||
on t1.inst_code = t2.inst_code
|
||||
and t1.market = t2.market
|
||||
left join target_ins_level_final t3
|
||||
on t1.inst_code = t3.inst_code
|
||||
and t1.market = t3.market
|
||||
left join channel_final t4
|
||||
on t1.inst_code = t4.inst_code
|
||||
and t1.market = t4.market
|
||||
)
|
||||
|
||||
--获取Dragon_type
|
||||
--判断Dragon_type是否为County
|
||||
,county as (
|
||||
select distinct inst_code,county_tier_desc
|
||||
from dm.dm_td_institution
|
||||
)
|
||||
--判断另外2种
|
||||
-- 20250825修改
|
||||
,dragon_type as (
|
||||
select src_hcc_code as inst_code,
|
||||
src_prod_code as family_code,
|
||||
max(is_core) is_core
|
||||
from cdw_dwd_dwd_hospital_classification
|
||||
where active_status = 1
|
||||
and src_sales_cycle = (select key_value from dm.dm_td_date_config where key_code = 'pfmc_cvh')
|
||||
group by src_hcc_code,
|
||||
src_prod_code
|
||||
)
|
||||
--将Family_code转为pack_cod
|
||||
,dragon_type_market1 as (
|
||||
select distinct
|
||||
t1.inst_code,
|
||||
t1.is_core,
|
||||
t4.market
|
||||
from dragon_type t1
|
||||
left join cdw_dwd_dim_product_wide t2
|
||||
on t1.family_code = t2.prd_family_cd
|
||||
left join dwd.dwd_td_imscode_azcode t3
|
||||
on t2.prd_brand_cd = t3.brandcode
|
||||
left join pack_market t4
|
||||
on t3.ims_pack_cod = t4.pack_cod
|
||||
where t4.market is not null
|
||||
)
|
||||
--按优先级取唯一值
|
||||
,dragon_type_market2 as (
|
||||
select distinct
|
||||
inst_code,
|
||||
is_core,
|
||||
market,
|
||||
row_number() over(partition by inst_code,market
|
||||
order by case when is_core = 'CORE' then 1
|
||||
when is_core = 'EMERGING' then 2
|
||||
else 3 end asc) as RN
|
||||
from dragon_type_market1
|
||||
)
|
||||
,dragon_type_market as (
|
||||
select distinct
|
||||
inst_code,
|
||||
is_core,
|
||||
market
|
||||
from dragon_type_market2
|
||||
where RN = 1
|
||||
)
|
||||
--综合前面几个表,得出dragon_flag
|
||||
,dragon_flag1 as (
|
||||
select distinct
|
||||
t0.inst_code,
|
||||
t0.market,
|
||||
case when t1.county_tier_desc in ('County','County level city') then 'County'
|
||||
when t2.is_core is null or length(t2.is_core) = 0 then 'Others'
|
||||
else t2.is_core
|
||||
end as dragon_flag
|
||||
from target_hp t0
|
||||
left join county t1
|
||||
on t0.inst_code = t1.inst_code
|
||||
left join dragon_type_market t2
|
||||
on t0.inst_code = t2.inst_code
|
||||
and t0.MARKET = t2.MARKET
|
||||
)
|
||||
--按优先级取唯一值
|
||||
,dragon_flag2 as (
|
||||
select inst_code,
|
||||
market,
|
||||
dragon_flag,
|
||||
row_number() over(partition by inst_code,market
|
||||
order by case when dragon_flag = 'CORE' then 1
|
||||
when dragon_flag = 'EMERGING' then 2
|
||||
when dragon_flag = 'County' then 3
|
||||
else 4 end asc) as RN
|
||||
from dragon_flag1
|
||||
)
|
||||
,dragon_flag as (
|
||||
select inst_code,
|
||||
market,
|
||||
dragon_flag
|
||||
from dragon_flag2
|
||||
where RN = 1
|
||||
)
|
||||
|
||||
--下面是为了把其他渠道并进来
|
||||
,other_data_source as (
|
||||
select explode(array('IQVIA-CHPA(Monthly)','XH Data(Quarterly)','EC(Monthly)','Retail(Quarterly)','CHC(Quarterly)','THC(Quarterly)','IQVIA-COUNTY(Quarterly)'))
|
||||
)
|
||||
,col as (
|
||||
select '' as inst_code,
|
||||
'' as MARKET,
|
||||
'' as is_target_hp,
|
||||
'' as sub_channel,
|
||||
'' as dragon_flag,
|
||||
'' as ka_flag,
|
||||
'' as target_ins_level
|
||||
)
|
||||
|
||||
,temp_1 as (
|
||||
select distinct
|
||||
case when t1.inst_code is null or t1.inst_code = '' then 'ROC'
|
||||
else t1.inst_code
|
||||
end as inst_code,
|
||||
t1.MARKET,
|
||||
case when t2.inst_code is null then 'N' else 'Y' end as is_target_hp,
|
||||
case when t2.channel = 'BU' and geo.county_tier_desc in ('County','County level city') then 'BU_County'
|
||||
when t2.channel = 'BU' and geo.county_tier_desc in ('City-3b','City-4a','City-4b') then 'BU_Emerging'
|
||||
when t2.channel = 'BU' and t3.dragon_flag = 'CORE' then 'BU_Core'
|
||||
when t2.channel = 'BU' then 'BU_Emerging'
|
||||
when t2.channel in ('Eagle','CHC','BBU_County') then t2.channel
|
||||
when t2.inst_code is null then 'NON_TARGET'
|
||||
else 'NA'
|
||||
end as sub_channel,
|
||||
case when t3.dragon_flag is null then 'NON_TARGET'
|
||||
else t3.dragon_flag
|
||||
end as dragon_flag,
|
||||
case when t2.ka_flag is null then 'NON_TARGET'
|
||||
else t2.ka_flag
|
||||
end as ka_flag,
|
||||
case when t2.target_ins_level is null then 'NON_TARGET'
|
||||
else t2.target_ins_level
|
||||
end as target_ins_level,
|
||||
'AIA(Monthly)' as DATA_SOURCE
|
||||
from sales t1
|
||||
left join target_hp t2
|
||||
on t1.inst_code = t2.inst_code
|
||||
and t1.MARKET = t2.MARKET
|
||||
left join dragon_flag t3
|
||||
on t1.inst_code = t3.inst_code
|
||||
and t1.MARKET = t3.MARKET
|
||||
left join dm.dm_td_institution inst
|
||||
on t1.inst_code =inst.inst_code
|
||||
left join dm.dm_td_geography geo
|
||||
on inst.county_code = geo.geo_key
|
||||
union all
|
||||
select *
|
||||
from col
|
||||
cross join other_data_source
|
||||
)
|
||||
,sub_channel_rank as (
|
||||
select distinct SUB_CHANNELSTD,SUB_CHANNEL_ORDER
|
||||
from dm.dm_td_sd_channel
|
||||
)
|
||||
|
||||
|
||||
select t1.*,NVL(t2.SUB_CHANNEL_ORDER,100) as SUB_CHANNEL_ORDER
|
||||
from temp_1 t1
|
||||
left join sub_channel_rank t2
|
||||
on t1.sub_channel = t2.SUB_CHANNELSTD
|
||||
Reference in New Issue
Block a user