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"
1201 lines
36 KiB
SQL
1201 lines
36 KiB
SQL
-- Databricks notebook source
|
||
-- MAGIC %md
|
||
-- MAGIC 此代码主要加工 free report 中retail 自有部分的 overview 文件 和 b2c配置表维度 及品牌数据报表指标导出
|
||
-- MAGIC * 1.dm_zk_retail_overview_data overview 文件union 表格
|
||
-- MAGIC * 2.dm_zk_retail_product_info B2C 配置表的维度(底表直接抽)
|
||
-- MAGIC * 3.dm_zk_retail_brand 品牌数据报表中的指标
|
||
-- MAGIC
|
||
|
||
-- COMMAND ----------
|
||
|
||
----------------------------------------------------------------------------------------
|
||
--修改时间:20240821
|
||
--修改人:FanXujia
|
||
--修改内容:
|
||
--导出模板整合,由原来的十几个文件改为3个文件
|
||
----------------------------------------------------------------------------------------
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- -------------------------------------------将所有overview 数据整合在同一张表里--------------------------------------------------------------
|
||
-- -- 注意事项:
|
||
-- --rank1字段:Total行排名设为99,其他行如果数据源是null,则设为98。总之不能为null。
|
||
-- -- 每个数据源文件(file_name)判断是否Total行的方式不尽相同,具体参考代码。
|
||
-- --sales_quarter字段:数据源中有两种格式,24Q1以及2024Q1,因此需要统一成2024Q1这种格式。
|
||
-- -- 有些因为是YTD的值,源文件里sales_quarter是null,需将YTD字段中的时间拆出来作为sales_quarter字段的值
|
||
-- --最终表的source_name字段的内容是各个TA,RC字段是南区、北区等。而数据源文件中都叫source_name,每个源文件需按实际情况拆分成2列
|
||
-- --yyyymm字段:Q1是3月、Q2是6月、Q3是9月、Q4是12月。
|
||
insert overwrite table dws.dws_zk_retail_overview_data
|
||
(
|
||
rank1
|
||
,product_desc
|
||
,product_type
|
||
,common_name
|
||
,brand
|
||
,corp
|
||
,corporation
|
||
,corp_type
|
||
,sales_quarter
|
||
,yyyymm
|
||
,ytd
|
||
,sales_amount
|
||
,sales_amount_ly
|
||
,sales_amount_total
|
||
,sales_amount_ly_total
|
||
,rx_type
|
||
,top1_brand_val
|
||
,top1_brand_gr
|
||
,top2_brand_val
|
||
,top2_brand_gr
|
||
,top1_incremental_brand
|
||
,top1_incremental_gr
|
||
,top2_incremental_brand
|
||
,top2_incremental_gr
|
||
,ytd_gr
|
||
,ytd_ms
|
||
,ytd_delta_ms
|
||
,dtp_name
|
||
,file_name
|
||
,source_name
|
||
,rc
|
||
,rx_otc
|
||
,top_incre
|
||
,etl_insert_dt
|
||
,etl_update_dt
|
||
)
|
||
with overview as
|
||
(
|
||
-- select
|
||
-- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
-- when rank1 is null then '98'
|
||
-- else replace(rank1,' ','') end as rank1
|
||
-- ,null as product_desc
|
||
-- ,null as product_type
|
||
-- ,null as common_name
|
||
-- ,null as brand
|
||
-- ,corp_desc as corp
|
||
-- ,corporation
|
||
-- ,corp_type
|
||
-- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
-- else sales_quarter
|
||
-- end as sales_quarter
|
||
-- ,null as ytd
|
||
-- ,sales_amount
|
||
-- ,null as rx_type
|
||
-- ,null as top1_brand_val
|
||
-- ,null as top1_brand_gr
|
||
-- ,null as top2_brand_val
|
||
-- ,null as top2_brand_gr
|
||
-- ,null as top1_incremental_brand
|
||
-- ,null as top1_incremental_gr
|
||
-- ,null as top2_incremental_brand
|
||
-- ,null as top2_incremental_gr
|
||
-- ,ytd_gr
|
||
-- ,ytd_ms
|
||
-- ,ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'全国-Rx-TOP集团' file_name
|
||
-- ,null source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_nataional_top_corp
|
||
-- union all
|
||
-- select
|
||
-- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
-- when rank1 is null then '98'
|
||
-- else replace(rank1,' ','') end as rank1
|
||
-- ,product_desc
|
||
-- ,product_type
|
||
-- ,common_name
|
||
-- ,brand
|
||
-- ,corp
|
||
-- ,corporation
|
||
-- ,null as corp_type
|
||
-- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
-- else sales_quarter
|
||
-- end as sales_quarter
|
||
-- ,null as ytd
|
||
-- ,sales_amount
|
||
-- ,null as rx_type
|
||
-- ,null as top1_brand_val
|
||
-- ,null as top1_brand_gr
|
||
-- ,null as top2_brand_val
|
||
-- ,null as top2_brand_gr
|
||
-- ,null as top1_incremental_brand
|
||
-- ,null as top1_incremental_gr
|
||
-- ,null as top2_incremental_brand
|
||
-- ,null as top2_incremental_gr
|
||
-- ,ytd_gr
|
||
-- ,ytd_ms
|
||
-- ,ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'全国-Rx-TOP产品' file_name
|
||
-- ,null source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_nataional_top_product
|
||
-- union all
|
||
-- select
|
||
-- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
-- when rank1 is null then '98'
|
||
-- else replace(rank1,' ','') end as rank1
|
||
-- ,product_desc
|
||
-- ,product_type
|
||
-- ,common_name
|
||
-- ,brand
|
||
-- ,corp
|
||
-- ,corporation
|
||
-- ,null as corp_type
|
||
-- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
-- else sales_quarter
|
||
-- end as sales_quarter
|
||
-- ,null as ytd
|
||
-- ,sales_amount
|
||
-- ,null as rx_type
|
||
-- ,null as top1_brand_val
|
||
-- ,null as top1_brand_gr
|
||
-- ,null as top2_brand_val
|
||
-- ,null as top2_brand_gr
|
||
-- ,null as top1_incremental_brand
|
||
-- ,null as top1_incremental_gr
|
||
-- ,null as top2_incremental_brand
|
||
-- ,null as top2_incremental_gr
|
||
-- ,ytd_gr
|
||
-- ,ytd_ms
|
||
-- ,ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'全国-Rx-TOP增量产品' file_name
|
||
-- ,null source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_nataional_top_incr_product
|
||
-- union all
|
||
-- select
|
||
-- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
-- when rank1 is null then '98'
|
||
-- else replace(rank1,' ','') end as rank1
|
||
-- ,null as product_desc
|
||
-- ,null as product_type
|
||
-- ,null as common_name
|
||
-- ,null as brand
|
||
-- ,corp_desc corp
|
||
-- ,corporation
|
||
-- ,corp_type
|
||
-- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
-- else sales_quarter
|
||
-- end as sales_quarter
|
||
-- ,null as ytd
|
||
-- ,sales_amount
|
||
-- ,null as rx_type
|
||
-- ,null as top1_brand_val
|
||
-- ,null as top1_brand_gr
|
||
-- ,null as top2_brand_val
|
||
-- ,null as top2_brand_gr
|
||
-- ,null as top1_incremental_brand
|
||
-- ,null as top1_incremental_gr
|
||
-- ,null as top2_incremental_brand
|
||
-- ,null as top2_incremental_gr
|
||
-- ,ytd_gr
|
||
-- ,ytd_ms
|
||
-- ,ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'全国-Rx+OTC-TOP集团' file_name
|
||
-- ,null source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_nataional_rx_otc_corp
|
||
|
||
-- union all
|
||
-- select
|
||
-- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
-- when rank1 is null then '98'
|
||
-- else replace(rank1,' ','') end as rank1
|
||
-- ,product_desc
|
||
-- ,product_type
|
||
-- ,common_name
|
||
-- ,brand
|
||
-- ,corp
|
||
-- ,corporation
|
||
-- ,null as corp_type
|
||
-- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
-- else sales_quarter
|
||
-- end as sales_quarter
|
||
-- ,null as ytd
|
||
-- ,sales_amount
|
||
-- ,null as rx_type
|
||
-- ,null as top1_brand_val
|
||
-- ,null as top1_brand_gr
|
||
-- ,null as top2_brand_val
|
||
-- ,null as top2_brand_gr
|
||
-- ,null as top1_incremental_brand
|
||
-- ,null as top1_incremental_gr
|
||
-- ,null as top2_incremental_brand
|
||
-- ,null as top2_incremental_gr
|
||
-- ,ytd_gr
|
||
-- ,ytd_ms
|
||
-- ,ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'全国-Rx+OTC-TOP产品' file_name
|
||
-- ,null source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_nataional_rx_otc_product
|
||
|
||
|
||
-- union all
|
||
-- select
|
||
-- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
-- when rank1 is null then '98'
|
||
-- else replace(rank1,' ','') end as rank1
|
||
-- ,null as product_desc
|
||
-- ,null as product_type
|
||
-- ,null as common_name
|
||
-- ,null as brand
|
||
-- ,corp_desc as corp
|
||
-- ,corp_name
|
||
-- ,corp_type
|
||
-- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
-- else sales_quarter
|
||
-- end as sales_quarter
|
||
-- ,null as ytd
|
||
-- ,sales_amount
|
||
-- ,null as rx_type
|
||
-- ,null as top1_brand_val
|
||
-- ,null as top1_brand_gr
|
||
-- ,null as top2_brand_val
|
||
-- ,null as top2_brand_gr
|
||
-- ,null as top1_incremental_brand
|
||
-- ,null as top1_incremental_gr
|
||
-- ,null as top2_incremental_brand
|
||
-- ,null as top2_incremental_gr
|
||
-- ,ytd_gr
|
||
-- ,ytd_ms
|
||
-- ,ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'全国-分TA-TOP集团' file_name
|
||
-- ,source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_nataional_ta_top_corp
|
||
|
||
-- union all
|
||
|
||
-- select
|
||
-- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
-- when rank1 is null then '98'
|
||
-- else replace(rank1,' ','') end as rank1
|
||
-- ,product_desc
|
||
-- ,null as product_type
|
||
-- ,common_name
|
||
-- ,null as brand
|
||
-- ,corp
|
||
-- ,corp_name corporation
|
||
-- ,null as corp_type
|
||
-- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
-- else sales_quarter
|
||
-- end as sales_quarter
|
||
-- ,null as ytd
|
||
-- ,sales_amount
|
||
-- ,null as rx_type
|
||
-- ,null as top1_brand_val
|
||
-- ,null as top1_brand_gr
|
||
-- ,null as top2_brand_val
|
||
-- ,null as top2_brand_gr
|
||
-- ,null as top1_incremental_brand
|
||
-- ,null as top1_incremental_gr
|
||
-- ,null as top2_incremental_brand
|
||
-- ,null as top2_incremental_gr
|
||
-- ,ytd_gr
|
||
-- ,ytd_ms
|
||
-- ,ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'全国-分TA-TOP产品' file_name
|
||
-- ,source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_nataional_ta_top_product
|
||
-- union all
|
||
-- select
|
||
-- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
-- when rank1 is null then '98'
|
||
-- else replace(rank1,' ','') end as rank1
|
||
-- ,product_desc
|
||
-- ,null as product_type
|
||
-- ,common_name
|
||
-- ,null as brand
|
||
-- ,corp
|
||
-- ,corp_name as corporation
|
||
-- ,null as corp_type
|
||
-- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
-- else sales_quarter
|
||
-- end as sales_quarter
|
||
-- ,null as ytd
|
||
-- ,sales_amount
|
||
-- ,null as rx_type
|
||
-- ,null as top1_brand_val
|
||
-- ,null as top1_brand_gr
|
||
-- ,null as top2_brand_val
|
||
-- ,null as top2_brand_gr
|
||
-- ,null as top1_incremental_brand
|
||
-- ,null as top1_incremental_gr
|
||
-- ,null as top2_incremental_brand
|
||
-- ,null as top2_incremental_gr
|
||
-- ,ytd_gr
|
||
-- ,ytd_ms
|
||
-- ,ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'全国-分TA-TOP增量产品' file_name
|
||
-- ,source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_nataional_ta_incr_product
|
||
|
||
-- union all
|
||
-- select
|
||
-- case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
-- when rank1 is null then '98'
|
||
-- else replace(rank1,' ','') end as rank1
|
||
-- ,null as product_desc
|
||
-- ,null as product_type
|
||
-- ,null as common_name
|
||
-- ,null as brand
|
||
-- ,corp_desc as corp
|
||
-- ,corporation
|
||
-- ,corp_type
|
||
-- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
-- else sales_quarter
|
||
-- end as sales_quarter
|
||
-- ,null as ytd
|
||
-- ,sales_amount
|
||
-- ,null as rx_type
|
||
-- ,null as top1_brand_val
|
||
-- ,null as top1_brand_gr
|
||
-- ,null as top2_brand_val
|
||
-- ,null as top2_brand_gr
|
||
-- ,null as top1_incremental_brand
|
||
-- ,null as top1_incremental_gr
|
||
-- ,null as top2_incremental_brand
|
||
-- ,null as top2_incremental_gr
|
||
-- ,ytd_gr
|
||
-- ,ytd_ms
|
||
-- ,ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'分大区-Rx-TOP集团' file_name
|
||
-- ,null source_name
|
||
-- ,source_name rc
|
||
-- from dwd.dwd_gnd_ext_retail_area_top_corp
|
||
|
||
-- union all
|
||
-- select
|
||
-- case when upper(type_name) = 'RX' then '99'
|
||
-- else '98' end as rank1
|
||
-- ,null as product_desc
|
||
-- ,null as product_type
|
||
-- ,null as common_name
|
||
-- ,null as brand
|
||
-- ,null as corp
|
||
-- ,null as corporation
|
||
-- ,type_name as corp_type
|
||
-- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
-- else sales_quarter
|
||
-- end as sales_quarter
|
||
-- ,null as ytd
|
||
-- ,sales_amount
|
||
-- ,null as rx_type
|
||
-- ,null as top1_brand_val
|
||
-- ,null as top1_brand_gr
|
||
-- ,null as top2_brand_val
|
||
-- ,null as top2_brand_gr
|
||
-- ,null as top1_incremental_brand
|
||
-- ,null as top1_incremental_gr
|
||
-- ,null as top2_incremental_brand
|
||
-- ,null as top2_incremental_gr
|
||
-- ,null as ytd_gr
|
||
-- ,null as ytd_ms
|
||
-- ,null as ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'Rx市场' file_name
|
||
-- ,null as source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_rx_market ---这个没有ytd 三个字段添加
|
||
|
||
-- union all
|
||
-- select
|
||
-- case when upper(corporation) like 'TOTAL %' then '99'
|
||
-- else '98' end as rank1
|
||
-- ,null as product_desc
|
||
-- ,null as product_type
|
||
-- ,null as common_name
|
||
-- ,null as brand
|
||
-- ,null as corp
|
||
-- ,corporation
|
||
-- ,null as corp_type
|
||
-- ,concat('20',trim(replace(quarter_flag,'YTD','' ))) as sales_quarter
|
||
-- ,quarter_flag as ytd
|
||
-- ,null as sales_amount
|
||
-- ,null as rx_type
|
||
-- ,replace(top1_brand_val,'-','') top1_brand_val
|
||
-- ,top1_brand_gr top1_brand_gr
|
||
-- ,replace(top2_brand_val,'-','') top2_brand_val
|
||
-- ,top2_brand_gr top2_brand_gr
|
||
-- ,replace(top1_incremental_brand,'-','') top1_incremental_brand
|
||
-- ,top1_incremental_gr
|
||
-- ,replace(top2_incremental_brand,'-','') top2_incremental_brand
|
||
-- ,top2_incremental_gr
|
||
-- ,null as ytd_gr
|
||
-- ,null as ytd_ms
|
||
-- ,null as ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'Rx-TOP集团' file_name
|
||
-- ,null as source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_rx_top_corp ---这个没有ytd 三个字段添加
|
||
-- union all
|
||
|
||
-- select
|
||
-- case when upper(corporation) like '% TTL' then '99'
|
||
-- else '98' end as rank1
|
||
-- ,null as product_desc
|
||
-- ,null as product_type
|
||
-- ,null as common_name
|
||
-- ,null as brand
|
||
-- ,null as corp
|
||
-- ,corporation
|
||
-- ,null as corp_type
|
||
-- ,concat('20',trim(replace(quarter_flag,'YTD','' ))) as sales_quarter
|
||
-- ,quarter_flag as ytd
|
||
-- ,NULL as sales_amount
|
||
-- ,rx_type
|
||
-- ,replace(top1_brand_val,'-','') as top1_brand_val
|
||
-- ,top1_brand_gr
|
||
-- ,replace(top2_brand_val,'-','') as top2_brand_val
|
||
-- , top2_brand_gr
|
||
-- ,replace(top1_incremental_brand,'-','') as top1_incremental_brand
|
||
-- , top1_incremental_gr
|
||
-- ,replace(top2_incremental_brand,'-','') as top2_incremental_brand
|
||
-- ,top2_incremental_gr
|
||
-- ,null as ytd_gr
|
||
-- ,null as ytd_ms
|
||
-- ,null as ytd_delta_ms
|
||
-- ,null as dtp_name
|
||
-- ,'分TA-TOP集团' file_name
|
||
-- ,trim(replace(rx_type,' TTL','' ) ) as source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_rx_ta_top_corp ---这个没有ytd 三个字段添加
|
||
-- union all
|
||
|
||
-- select
|
||
-- '99' as rank1
|
||
-- ,null as product_desc
|
||
-- ,null as product_type
|
||
-- ,null as common_name
|
||
-- ,null as brand
|
||
-- ,null as corp
|
||
-- ,null as corporation
|
||
-- ,null as corp_type
|
||
-- ,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
-- else sales_quarter
|
||
-- end as sales_quarter
|
||
-- ,null as ytd
|
||
-- ,sales_amount
|
||
-- ,null as rx_type
|
||
-- ,null as top1_brand_val
|
||
-- ,null as top1_brand_gr
|
||
-- ,null as top2_brand_val
|
||
-- ,null as top2_brand_gr
|
||
-- ,null as top1_incremental_brand
|
||
-- ,null as top1_incremental_gr
|
||
-- ,null as top2_incremental_brand
|
||
-- ,null as top2_incremental_gr
|
||
-- ,null as ytd_gr
|
||
-- ,null as ytd_ms
|
||
-- ,null as ytd_delta_ms
|
||
-- ,dtp_name
|
||
-- ,'DTP 整体市场' file_name
|
||
-- ,NULL as source_name
|
||
-- ,null rc
|
||
-- from dwd.dwd_gnd_ext_retail_dtp_overall_market ---这个没有ytd 三个字段添加 rx_type
|
||
|
||
|
||
-- union all
|
||
select
|
||
case when replace(upper(corp_name),'-','') = 'DTP TOTAL' then '99'
|
||
when rank1 is null then '98'
|
||
else replace(rank1,' ','') end as rank1
|
||
,null as product_desc
|
||
,null as product_type
|
||
,null as common_name
|
||
,null as brand
|
||
,replace(corp_name,'-','') as corp
|
||
,null as corporation
|
||
,null as corp_type
|
||
,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
else sales_quarter
|
||
end as sales_quarter
|
||
,null as ytd
|
||
,sales_amount
|
||
,null as rx_type
|
||
,null as top1_brand_val
|
||
,null as top1_brand_gr
|
||
,null as top2_brand_val
|
||
,null as top2_brand_gr
|
||
,null as top1_incremental_brand
|
||
,null as top1_incremental_gr
|
||
,null as top2_incremental_brand
|
||
,null as top2_incremental_gr
|
||
,null as ytd_gr
|
||
,null as ytd_ms
|
||
,null as ytd_delta_ms
|
||
,null as dtp_name
|
||
,'DTP TOP10企业排名' file_name
|
||
,NULL as source_name
|
||
,null rc
|
||
,'Rx' as rx_otc
|
||
,'TOP' as top_incre
|
||
from dwd.dwd_gnd_ext_retail_dtp_top_copd ---这个没有ytd 三个字段添加
|
||
union all
|
||
select
|
||
case when replace(upper(brand_name),'-','') = 'DTP TOTAL' then '99'
|
||
when rank1 is null then '98'
|
||
else replace(rank1,' ','') end as rank1
|
||
,null as product_desc
|
||
,null as product_type
|
||
,null as common_name
|
||
,brand_name as brand
|
||
,replace(corp_name,'-','') as corp
|
||
,null as corporation
|
||
,null as corp_type
|
||
,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
else sales_quarter
|
||
end as sales_quarter
|
||
,null as ytd
|
||
,sales_amount
|
||
,null as rx_type
|
||
,null as top1_brand_val
|
||
,null as top1_brand_gr
|
||
,null as top2_brand_val
|
||
,null as top2_brand_gr
|
||
,null as top1_incremental_brand
|
||
,null as top1_incremental_gr
|
||
,null as top2_incremental_brand
|
||
,null as top2_incremental_gr
|
||
,null as ytd_gr
|
||
,null as ytd_ms
|
||
,null as ytd_delta_ms
|
||
,null as dtp_name
|
||
,'DTP TOP10品牌排名' file_name
|
||
,NULL as source_name
|
||
,null rc
|
||
,'Rx' as rx_otc
|
||
,'TOP' as top_incre
|
||
from dwd.dwd_gnd_ext_retail_dtp_top_brand ---这个没有ytd 三个字段添加
|
||
union all
|
||
|
||
select
|
||
case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
when rank1 is null then '98'
|
||
else replace(rank1,' ','') end as rank1
|
||
,null as product_desc
|
||
,null as product_type
|
||
,null as common_name
|
||
,null as brand
|
||
,corp_desc as corp
|
||
,corporation
|
||
,`type` as corp_type
|
||
,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
else sales_quarter
|
||
end as sales_quarter
|
||
,null as ytd
|
||
,sales_amount
|
||
,null as rx_type
|
||
,null as top1_brand_val
|
||
,null as top1_brand_gr
|
||
,null as top2_brand_val
|
||
,null as top2_brand_gr
|
||
,null as top1_incremental_brand
|
||
,null as top1_incremental_gr
|
||
,null as top2_incremental_brand
|
||
,null as top2_incremental_gr
|
||
,null as ytd_gr
|
||
,null as ytd_ms
|
||
,null as ytd_delta_ms
|
||
,null as dtp_name
|
||
,'全国-集团排名' file_name
|
||
,case when ta = 'GI' then concat(ta,'-',rx_otc)
|
||
when ta = 'NIAD-excl. GLP1' then 'DM'
|
||
else ta end as source_name
|
||
,null rc
|
||
,rx_otc
|
||
,case when upper(top_top_incre) = 'TOP SALES' then 'TOP' else 'Incre' end as top_incre
|
||
from dwd.dwd_gnd_retail_national_corp_rank
|
||
|
||
union all
|
||
select
|
||
case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
when rank1 is null then '98'
|
||
else replace(rank1,' ','') end as rank1
|
||
,prod_desc as product_desc
|
||
,`type` as product_type
|
||
,cmps_desc as common_name
|
||
,brand
|
||
,corp_desc as corp
|
||
,corporation
|
||
,null as corp_type
|
||
,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
else sales_quarter
|
||
end as sales_quarter
|
||
,null as ytd
|
||
,sales_amount
|
||
,null as rx_type
|
||
,null as top1_brand_val
|
||
,null as top1_brand_gr
|
||
,null as top2_brand_val
|
||
,null as top2_brand_gr
|
||
,null as top1_incremental_brand
|
||
,null as top1_incremental_gr
|
||
,null as top2_incremental_brand
|
||
,null as top2_incremental_gr
|
||
,null as ytd_gr
|
||
,null as ytd_ms
|
||
,null as ytd_delta_ms
|
||
,null as dtp_name
|
||
,'全国-产品排名' file_name
|
||
,case when ta = 'GI' then concat(ta,'-',rx_otc)
|
||
when ta = 'NIAD-excl. GLP1' then 'DM'
|
||
else ta end as source_name
|
||
,null rc
|
||
,rx_otc
|
||
,case when upper(top_top_incre) = 'TOP SALES' then 'TOP' else 'Incre' end as top_incre
|
||
from dwd.dwd_gnd_retail_national_prd_rank
|
||
union all
|
||
select
|
||
case when upper(replace(rank1,' ','')) = 'TOTAL' then '99'
|
||
when rank1 is null then '98'
|
||
else replace(rank1,' ','') end as rank1
|
||
,null as product_desc
|
||
,null as product_type
|
||
,null as common_name
|
||
,null as brand
|
||
,corp_desc as corp
|
||
,corporation
|
||
,`type` as corp_type
|
||
,case when length(sales_quarter) = 4 then concat('20',sales_quarter)
|
||
else sales_quarter
|
||
end as sales_quarter
|
||
,null as ytd
|
||
,sales_amount
|
||
,null as rx_type
|
||
,null as top1_brand_val
|
||
,null as top1_brand_gr
|
||
,null as top2_brand_val
|
||
,null as top2_brand_gr
|
||
,null as top1_incremental_brand
|
||
,null as top1_incremental_gr
|
||
,null as top2_incremental_brand
|
||
,null as top2_incremental_gr
|
||
,null as ytd_gr
|
||
,null as ytd_ms
|
||
,null as ytd_delta_ms
|
||
,null as dtp_name
|
||
,'大区-集团排名' file_name
|
||
,case when ta = 'GI' then concat(ta,'-',rx_otc)
|
||
when ta = 'NIAD-excl. GLP1' then 'DM'
|
||
else ta end as source_name
|
||
,region rc
|
||
,rx_otc
|
||
,case when upper(top_top_incre) = 'TOP SALES' then 'TOP' else 'Incre' end as top_incre
|
||
from dwd.dwd_gnd_retail_region_corp_rank
|
||
)
|
||
|
||
-- --计算Total值,按文件、季度、TA、rx_otc、top_incre进行合计
|
||
-- --因为数据中本身就包含了Total行,因此直接筛选了rank1 = '99'(即筛选Total行)进行合计
|
||
,overview_total as (
|
||
select file_name,
|
||
source_name,
|
||
rx_otc,
|
||
top_incre,
|
||
sales_quarter,
|
||
rc,
|
||
sum(sales_amount) sales_amount_total
|
||
from overview
|
||
where rank1 = '99'
|
||
group by file_name,sales_quarter,rx_otc,top_incre,source_name,rc
|
||
)
|
||
|
||
-- --取到文件中的最大时间,在获取ly值的时候,用于限定时间范围
|
||
,overview_maxyq as (
|
||
select max(sales_quarter) max_sales_quarter
|
||
from overview
|
||
where sales_quarter is not null
|
||
)
|
||
|
||
-- --在数据源的基础上,增加yyyymm字段、sales_amount_ly、sales_amount_total、sales_amount_ly_total字段
|
||
select case when max(rank1) < 10000 then max(rank1) else cast(max(rank1) as int) - 10000 end as rank1
|
||
,product_desc
|
||
,product_type
|
||
,common_name
|
||
,brand
|
||
,corp
|
||
,corporation
|
||
,corp_type
|
||
,sales_quarter
|
||
,case when right(sales_quarter,1) = '1' then concat(left(sales_quarter,4),'03')
|
||
when right(sales_quarter,1) = '2' then concat(left(sales_quarter,4),'06')
|
||
when right(sales_quarter,1) = '3' then concat(left(sales_quarter,4),'09')
|
||
when right(sales_quarter,1) = '4' then concat(left(sales_quarter,4),'12')
|
||
end as yyyymm
|
||
,max(ytd) ytd
|
||
,sum(sales_amount) as sales_amount
|
||
,sum(sales_amount_ly) as sales_amount_ly
|
||
,sum(sales_amount_total) as sales_amount_total
|
||
,sum(sales_amount_ly_total) as sales_amount_ly_total
|
||
,rx_type
|
||
,max(top1_brand_val) top1_brand_val
|
||
,max(top1_brand_gr) top1_brand_gr
|
||
,max(top2_brand_val) top2_brand_val
|
||
,max(top2_brand_gr) top2_brand_gr
|
||
,max(top1_incremental_brand) top1_incremental_brand
|
||
,max(top1_incremental_gr) top1_incremental_gr
|
||
,max(top2_incremental_brand) top2_incremental_brand
|
||
,max(top2_incremental_gr) top2_incremental_gr
|
||
,max(ytd_gr) ytd_gr
|
||
,max(ytd_ms) ytd_ms
|
||
,max(ytd_delta_ms) ytd_delta_ms
|
||
,dtp_name
|
||
,file_name
|
||
,source_name
|
||
,rc
|
||
,rx_otc
|
||
,top_incre
|
||
,from_utc_timestamp(current_timestamp(),'UTC+8') etl_insert_dt
|
||
,from_utc_timestamp(current_timestamp(),'UTC+8') etl_update_dt
|
||
from
|
||
(
|
||
select
|
||
cast(rank1 as int) + 10000 rank1
|
||
,product_desc
|
||
,product_type
|
||
,common_name
|
||
,brand
|
||
,corp
|
||
,corporation
|
||
,corp_type
|
||
,t1.sales_quarter
|
||
,ytd
|
||
,sales_amount
|
||
,0 as sales_amount_ly
|
||
,t2.sales_amount_total
|
||
,0 as sales_amount_ly_total
|
||
,rx_type
|
||
,top1_brand_val
|
||
,top1_brand_gr
|
||
,top2_brand_val
|
||
,top2_brand_gr
|
||
,top1_incremental_brand
|
||
,top1_incremental_gr
|
||
,top2_incremental_brand
|
||
,top2_incremental_gr
|
||
,ytd_gr
|
||
,ytd_ms
|
||
,ytd_delta_ms
|
||
,dtp_name
|
||
,t1.file_name
|
||
,t1.source_name
|
||
,t1.rc
|
||
,t1.rx_otc
|
||
,t1.top_incre
|
||
from overview t1
|
||
left join overview_total t2
|
||
on t1.file_name = t2.file_name
|
||
and nvl(t1.sales_quarter,'') = nvl(t2.sales_quarter,'')
|
||
and t1.rx_otc = t2.rx_otc
|
||
and t1.top_incre = t2.top_incre
|
||
and NVL(t1.source_name,'') = NVL(t2.source_name,'')
|
||
and NVL(t1.rc,'') = NVL(t2.rc,'')
|
||
union all
|
||
select
|
||
cast(rank1 as int) rank1
|
||
,product_desc
|
||
,product_type
|
||
,common_name
|
||
,brand
|
||
,corp
|
||
,corporation
|
||
,corp_type
|
||
,concat(cast(left(t1.sales_quarter,4) as int) + 1,right(t1.sales_quarter,2)) as sales_quarter
|
||
,null as ytd
|
||
,0 as sales_amount
|
||
,sales_amount as sales_amount_ly
|
||
,0 as sales_amount_total
|
||
,t2.sales_amount_total as sales_amount_ly_total
|
||
,rx_type
|
||
,null as top1_brand_val
|
||
,null as top1_brand_gr
|
||
,null as top2_brand_val
|
||
,null as top2_brand_gr
|
||
,null as top1_incremental_brand
|
||
,null as top1_incremental_gr
|
||
,null as top2_incremental_brand
|
||
,null as top2_incremental_gr
|
||
,null as ytd_gr
|
||
,null as ytd_ms
|
||
,null as ytd_delta_ms
|
||
,dtp_name
|
||
,t1.file_name
|
||
,t1.source_name
|
||
,t1.rc
|
||
,t1.rx_otc
|
||
,t1.top_incre
|
||
from overview t1
|
||
left join overview_total t2
|
||
on t1.file_name = t2.file_name
|
||
and nvl(t1.sales_quarter,'') = nvl(t2.sales_quarter,'')
|
||
and t1.rx_otc = t2.rx_otc
|
||
and t1.top_incre = t2.top_incre
|
||
and NVL(t1.source_name,'') = NVL(t2.source_name,'')
|
||
and NVL(t1.rc,'') = NVL(t2.rc,'')
|
||
left join overview_maxyq t3
|
||
on 1=1
|
||
where concat(cast(left(t1.sales_quarter,4) as int) + 1,right(t1.sales_quarter,2)) <= t3.max_sales_quarter
|
||
) t
|
||
group by
|
||
product_desc
|
||
,product_type
|
||
,common_name
|
||
,brand
|
||
,corp
|
||
,corporation
|
||
,corp_type
|
||
,sales_quarter
|
||
,rx_type
|
||
,dtp_name
|
||
,file_name
|
||
,source_name
|
||
,rc
|
||
,rx_otc
|
||
,top_incre
|
||
|
||
-- COMMAND ----------
|
||
|
||
-- --------将free report overview 导出的合并文件写入dm
|
||
insert overwrite table dm.dm_zk_retail_overview_data(
|
||
rank1,
|
||
product_desc,
|
||
product_type,
|
||
common_name,
|
||
brand,
|
||
corp,
|
||
corporation,
|
||
corp_type,
|
||
sales_quarter,
|
||
yyyymm,
|
||
ytd,
|
||
sales_amount,
|
||
sales_amount_ly,
|
||
sales_amount_total,
|
||
sales_amount_ly_total,
|
||
rx_type,
|
||
top1_brand_val,
|
||
top1_brand_gr,
|
||
top2_brand_val,
|
||
top2_brand_gr,
|
||
top1_incremental_brand,
|
||
top1_incremental_gr,
|
||
top2_incremental_brand,
|
||
top2_incremental_gr,
|
||
ytd_gr,
|
||
ytd_ms,
|
||
ytd_delta_ms,
|
||
dtp_name,
|
||
file_name,
|
||
source_name,
|
||
rc,
|
||
rx_otc,
|
||
top_incre,
|
||
etl_insert_dt,
|
||
etl_update_dt
|
||
)
|
||
select
|
||
trim(rank1) rank1
|
||
,product_desc
|
||
,product_type
|
||
,common_name
|
||
,brand
|
||
,nvl(corp,'' ) corp
|
||
,corporation
|
||
,corp_type
|
||
,sales_quarter
|
||
,yyyymm
|
||
,case when ytd is null then 'N' ELSE 'Y' END ytd
|
||
,sales_amount
|
||
,sales_amount_ly
|
||
,sales_amount_total
|
||
,sales_amount_ly_total
|
||
,rx_type
|
||
,top1_brand_val
|
||
,cast(top1_brand_gr as double) top1_brand_gr
|
||
,top2_brand_val
|
||
,cast(top2_brand_gr as double) top2_brand_gr
|
||
,top1_incremental_brand
|
||
,cast(top1_incremental_gr as double) top1_incremental_gr
|
||
,top2_incremental_brand
|
||
,cast(top2_incremental_gr as double) top2_incremental_gr
|
||
,cast(ytd_gr as double ) ytd_gr
|
||
,cast(ytd_ms as double) ytd_ms
|
||
,cast(ytd_delta_ms as double) ytd_delta_ms
|
||
,dtp_name
|
||
,file_name
|
||
,trim(source_name) source_name
|
||
,rc
|
||
,rx_otc
|
||
,top_incre
|
||
,from_utc_timestamp(current_timestamp(),'UTC+8') etl_insert_dt
|
||
,from_utc_timestamp(current_timestamp(),'UTC+8') etl_update_dt
|
||
from dws.dws_zk_retail_overview_data
|
||
|
||
-- COMMAND ----------
|
||
|
||
------------------------------retail/EC原始维度,从b2c配置表直取-----------------------------------------
|
||
insert overwrite table dws.dws_zk_retail_product_info
|
||
select distinct
|
||
product_id
|
||
,iqvia_pack_code
|
||
,prescription_nature
|
||
,medicine_type
|
||
,zk_medicine_tier1
|
||
,zk_medicine_tier2
|
||
,zk_medicine_tier3
|
||
,zk_medicine_tier4
|
||
,common_name
|
||
,dosage_form
|
||
,user_type
|
||
,category_name
|
||
,product_name
|
||
,brand_name
|
||
,zk_manu_des
|
||
,zk_corp_des
|
||
,zk_pack_des
|
||
,from_utc_timestamp(current_timestamp(),'UTC+8') etl_insert_dt
|
||
,from_utc_timestamp(current_timestamp(),'UTC+8') etl_update_dt
|
||
from dwd.dwd_gnd_ext_retail_pack_property;
|
||
------------------------------retail/EC原始维度,从b2c直取 写入dm-----------------------------------------
|
||
insert overwrite table dm.dm_zk_retail_product_info
|
||
select distinct
|
||
product_id
|
||
,iqvia_pack_code
|
||
,prescription_nature
|
||
,medicine_type
|
||
,zk_medicine_tier1
|
||
,zk_medicine_tier2
|
||
,zk_medicine_tier3
|
||
,zk_medicine_tier4
|
||
,common_name
|
||
,dosage_form
|
||
,user_type
|
||
,category_name
|
||
,product_name
|
||
,brand_name
|
||
,zk_manu_des
|
||
,zk_corp_des
|
||
,zk_pack_des
|
||
,from_utc_timestamp(current_timestamp(),'UTC+8') etl_insert_dt
|
||
,from_utc_timestamp(current_timestamp(),'UTC+8') etl_update_dt
|
||
from dws.dws_zk_retail_product_info;
|
||
|
||
-- COMMAND ----------
|
||
|
||
------------------------------retail 自有部分需要导出数据,从品牌数据报表直取,无特殊加工-----------------------------------------
|
||
--注意事项:
|
||
----market 排除数据为:从pack 文件中通过比例拆分写入品牌数据的 品牌数据,包括多达一、天一宁等品牌,这些并非原始文件中的数据,故排除。
|
||
---- (具体拆分代码详见:1 dwd_inc_gnd_ext_retail_nataional
|
||
insert overwrite table dws.dws_zk_retail_brand
|
||
with max_yq as (
|
||
select TA,market,rc_name_en,province_city,ranked_by,max(yq) as yq
|
||
from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all
|
||
WHERE market NOT IN ('pack-CV-高血压-化学药-全国.xlsx','pack-CV-他汀类+血脂康-全国.xlsx')
|
||
group by TA,market,rc_name_en,province_city,ranked_by
|
||
)
|
||
|
||
select distinct
|
||
t1.TA,
|
||
t1.market,
|
||
t1.rc_name_en,
|
||
t1.province_city
|
||
,key_brand_ytd
|
||
,key_brand_rank_ytd
|
||
,top_brand_ytd
|
||
,top_brand_ms_ytd
|
||
,top_brand_inc_ms_ytd
|
||
,top_brand_gr_ytd
|
||
,key_brand_qtd
|
||
,key_brand_rank_qtd
|
||
,top_brand_qtd
|
||
,top_brand_ms_qtd
|
||
,top_brand_inc_ms_qtd
|
||
,top_brand_gr_qtd
|
||
,t1.ranked_by
|
||
,from_utc_timestamp(current_timestamp(),'UTC+8') etl_insert_dt
|
||
,from_utc_timestamp(current_timestamp(),'UTC+8') etl_update_dt
|
||
from dwd.dwd_inc_gnd_ext_retail_nataional_brand_union_all t1
|
||
inner join max_yq t2
|
||
on t1.TA = t2.TA
|
||
and t1.market = t2.market
|
||
and t1.rc_name_en = t2.rc_name_en
|
||
and t1.province_city = t2.province_city
|
||
and t1.yq = t2.yq
|
||
and t1.ranked_by = t2.ranked_by
|
||
;
|
||
|
||
-- dm_zk_retail_brand
|
||
|
||
------------------------------retail 自有部分需要导出数据,写入dm-----------------------------------------
|
||
insert overwrite table dm.dm_zk_retail_brand
|
||
select distinct
|
||
TA,market,rc_name_en,province_city
|
||
,key_brand_ytd
|
||
,cast(key_brand_rank_ytd as double) key_brand_rank_ytd
|
||
,top_brand_ytd
|
||
,cast(top_brand_ms_ytd as double) top_brand_ms_ytd
|
||
,cast(top_brand_inc_ms_ytd as double) top_brand_inc_ms_ytd
|
||
,cast(top_brand_gr_ytd as double) top_brand_gr_ytd
|
||
,key_brand_qtd
|
||
,cast(key_brand_rank_qtd as double ) key_brand_rank_qtd
|
||
,top_brand_qtd
|
||
,cast(top_brand_ms_qtd as double ) top_brand_ms_qtd
|
||
,cast(top_brand_inc_ms_qtd as double ) top_brand_inc_ms_qtd
|
||
,cast(top_brand_gr_qtd as double ) top_brand_gr_qtd
|
||
,ranked_by
|
||
from dws.dws_zk_retail_brand ;
|
||
|
||
|
||
|
||
|
||
|
||
|
||
-- COMMAND ----------
|
||
|
||
----------------------------------------------------------------------------------------
|
||
--修改时间:20240821
|
||
--修改人:FanXujia
|
||
--修改内容:
|
||
--导出模板整合,由原来的十几个文件改为3个文件
|
||
--《全国-集团排名》:dwd.dwd_gnd_retail_national_corp_rank
|
||
--《全国-产品排名》:dwd.dwd_gnd_retail_national_prd_rank
|
||
--《大区-集团排名》:dwd.dwd_gnd_retail_region_corp_rank
|
||
----------------------------------------------------------------------------------------
|
||
insert overwrite table dws.dws_zk_retail_export
|
||
(
|
||
ta,
|
||
rx_otc,
|
||
top_top_incre,
|
||
rank1,
|
||
corp_desc,
|
||
corporation,
|
||
type,
|
||
prod_desc,
|
||
cmps_desc,
|
||
brand,
|
||
region,
|
||
sales_quarter,
|
||
sales_amount,
|
||
file_name,
|
||
etl_insert_dt,
|
||
etl_update_dt
|
||
)
|
||
--《全国-集团排名》
|
||
select distinct
|
||
ta,
|
||
rx_otc,
|
||
top_top_incre,
|
||
case when upper(trim(rank1)) = 'TOTAL' then '99'
|
||
when rank1 is null then '98'
|
||
else trim(rank1) end as rank1,
|
||
corp_desc,
|
||
corporation,
|
||
type,
|
||
null as prod_desc,
|
||
null as cmps_desc,
|
||
null as brand,
|
||
null as region,
|
||
sales_quarter,
|
||
sales_amount,
|
||
'全国-集团排名' as file_name,
|
||
from_utc_timestamp(current_timestamp(),'UTC+8') as etl_insert_dt,
|
||
from_utc_timestamp(current_timestamp(),'UTC+8') as etl_update_dt
|
||
from dwd.dwd_gnd_retail_national_corp_rank
|
||
union all
|
||
--《全国-产品排名》
|
||
select distinct
|
||
ta,
|
||
rx_otc,
|
||
top_top_incre,
|
||
case when upper(trim(rank1)) = 'TOTAL' then '99'
|
||
when rank1 is null then '98'
|
||
else trim(rank1) end as rank1,
|
||
corp_desc,
|
||
corporation,
|
||
type,
|
||
prod_desc,
|
||
cmps_desc,
|
||
brand,
|
||
null as region,
|
||
sales_quarter,
|
||
sales_amount,
|
||
'全国-产品排名' as file_name,
|
||
from_utc_timestamp(current_timestamp(),'UTC+8') as etl_insert_dt,
|
||
from_utc_timestamp(current_timestamp(),'UTC+8') as etl_update_dt
|
||
from dwd.dwd_gnd_retail_national_prd_rank
|
||
union all
|
||
--《大区-集团排名》
|
||
select distinct
|
||
ta,
|
||
rx_otc,
|
||
top_top_incre,
|
||
case when upper(trim(rank1)) = 'TOTAL' then '99'
|
||
when rank1 is null then '98'
|
||
else trim(rank1) end as rank1,
|
||
corp_desc,
|
||
corporation,
|
||
type,
|
||
null as prod_desc,
|
||
null as cmps_desc,
|
||
null as brand,
|
||
region,
|
||
sales_quarter,
|
||
sales_amount,
|
||
'大区-集团排名' as file_name,
|
||
from_utc_timestamp(current_timestamp(),'UTC+8') as etl_insert_dt,
|
||
from_utc_timestamp(current_timestamp(),'UTC+8') as etl_update_dt
|
||
from dwd.dwd_gnd_retail_region_corp_rank;
|
||
|
||
|
||
--写入dm表
|
||
insert overwrite table dm.dm_zk_retail_export
|
||
(
|
||
ta,
|
||
rx_otc,
|
||
top_top_incre,
|
||
rank1,
|
||
corp_desc,
|
||
corporation,
|
||
type,
|
||
prod_desc,
|
||
cmps_desc,
|
||
brand,
|
||
region,
|
||
sales_quarter,
|
||
sales_amount,
|
||
file_name,
|
||
etl_insert_dt,
|
||
etl_update_dt
|
||
)
|
||
select
|
||
ta,
|
||
rx_otc,
|
||
top_top_incre,
|
||
rank1,
|
||
corp_desc,
|
||
corporation,
|
||
type,
|
||
prod_desc,
|
||
cmps_desc,
|
||
brand,
|
||
region,
|
||
sales_quarter,
|
||
sales_amount,
|
||
file_name,
|
||
etl_insert_dt,
|
||
etl_update_dt
|
||
from dws.dws_zk_retail_export; |