-- Databricks notebook source
-- --OBU COUNTY 改为 OBU EAST5、OBU_BU_COUNTY 改为 OBU NON EAST5
-- drop table if exists dm.dm_td_county_pack_region;
-- create table if not exists dm.dm_td_county_pack_region
-- using delta
-- as
-- with tmp as (
--   select 
--   t1.pack_cod,
--   t1.region_cod,
--   t2.ATC1_COD,
--   t2.ATC2_COD
-- from (select distinct PACK_COD,REGION_COD from dws.dws_ext_county_tf_sales) t1
-- left join (select distinct PACK_COD,ATC1_COD,ATC2_COD from dm.dm_ext_county_td_pack_property) t2 
-- on t1.PACK_COD = t2.pack_cod
-- ),obu_county as (
--   select pack_cod,region_cod,'OBU East5' region_type from tmp where region_cod in ('Shandong','Jiangsu','Zhejiang','Guangdong','OBU_OtherProv') and (ATC2_COD = 'L01' OR ATC2_COD = 'L02')--ATC1_COD = 'L'
-- ),bbu_county as (
--   select pack_cod,region_cod,'BBU COUNTY' region_type from tmp where region_cod in('Henan','Sichuan','BBU_OtherProv')
--    and ATC1_COD <> 'L'
-- ),bbu_bu_county as (
--   select pack_cod,region_cod,'BBU_BU_COUNTY' region_type from tmp where region_cod  in ('Guangdong','Zhejiang','Shandong','Jiangsu','Others') and ATC1_COD <> 'L'
-- ),obu_bu_county as (
--   select pack_cod,region_cod,'OBU NonEast5' region_type from tmp where region_cod in ('Henan','Sichuan','Others','BBU_OtherProv') and  (ATC2_COD = 'L01' OR ATC2_COD = 'L02') 
-- ),county_all as (
--   select * from obu_county
--   union 
--   select * from bbu_county
--   union 
--   select * from bbu_bu_county
--   union 
--   select * from obu_bu_county
-- )
-- select 
--   pack_cod,
--   region_cod,
--   region_type
-- from county_all order by region_type

-- COMMAND ----------

----------------------------------------------------------------------------------
--修改时间：20240913
--修改人：FanXujia
--修改内容：
--划分BBU_COUNTY、BBU_BU_COUNTY两个区域时，去掉ATC1_COD <> 'L'的限制
--这个需求使得BBU和OBU会有一部分重叠的区域
--因此对区域划分逻辑进行调整
-----几个region_cod的含义
--OBU_OtherProv：福建的ATC2=L01、L02
--BBU_OtherProv：BBU_COUNTY的18个省减去四川、河南
--Others：其他省份，这里面包含了福建的ATC2<>L01、L02的数
----------------------------------------------------------------------------------
insert overwrite table dm.dm_td_county_pack_region 
with tmp as (
  select 
  t1.pack_cod,
  t1.region_cod,
  t2.ATC1_COD,
  t2.ATC2_COD
from (select distinct PACK_COD,REGION_COD from dws.dws_ext_county_tf_sales) t1
left join (select distinct PACK_COD,ATC1_COD,ATC2_COD from dm.dm_ext_county_td_pack_property) t2 
on t1.PACK_COD = t2.pack_cod
)
--单独bbu_bu_county
,bbu_bu_county as (
  select pack_cod,region_cod,'BBU_BU_COUNTY' region_type from tmp
   where region_cod  in ('Guangdong','Jiangsu','Others','Shandong','Zhejiang') and ATC2_COD NOT IN ('L01','L02')
)
--bbu_bu_county与OBU East5共有
,bbu_bu_county_obu_East5 as  (
  select pack_cod,region_cod,'bbu_bu_county_obu_East5' region_type from tmp
   where region_cod  in ('Guangdong','Jiangsu','Shandong','Zhejiang','OBU_OtherProv') and ATC2_COD in ('L01','L02')
)
--bbu_bu_county与OBU NonEast5共有
,bbu_bu_county_obu_nonEast5 as  (
  select pack_cod,region_cod,'bbu_bu_county_obu_nonEast5' region_type from tmp
   where region_cod  in ('Others') and ATC2_COD in ('L01','L02')
)
--bbu_county与OBU NonEast5共有
,bbu_county_obu_nonEast5 as (
  select pack_cod,region_cod,'bbu_county_obu_nonEast5' region_type from tmp
   where region_cod  in ('BBU_OtherProv','Henan','Sichuan') and ATC2_COD in ('L01','L02')
)
--单独bbu_county
,bbu_county as (
  select pack_cod,region_cod,'BBU County' region_type from tmp
   where region_cod in('BBU_OtherProv','Henan','Sichuan') and ATC2_COD NOT IN ('L01','L02')
)
--全部
,county_all as (
  select * from bbu_bu_county
  union 
  select * from bbu_bu_county_obu_East5
  union 
  select * from bbu_bu_county_obu_nonEast5
  union 
  select * from bbu_county_obu_nonEast5
  union 
  select * from bbu_county
)
select 
  pack_cod,
  region_cod,
  region_type
from county_all order by region_type
