-- 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;