-- Databricks notebook source
-------------------------------------county新倒减拆分需求代码示例之KPI递减-------------------------------------
insert overwrite table tmp.tmp_imscounty_Result 
with ObuEast5 as (
  --取OBU 东五的数据（仅包含L01+L02）
  SELECT YYYYMM,region_cod,pack_cod,SalesValue_LC,SalesValue_LC_LY,Counting_Unit,Counting_Unit_LY,Sales_Unit,Sales_Unit_LY 
FROM tmp.tmp_ims_county_fact_sales_sum
WHERE region_cod = 'County2'
),CountyProv as (
  --得到省份的数据
SELECT YYYYMM,region_cod,pack_cod,SalesValue_LC,SalesValue_LC_LY,Counting_Unit,Counting_Unit_LY,Sales_Unit,Sales_Unit_LY 
FROM tmp.tmp_ims_county_fact_sales_sum
WHERE region_cod NOT IN ('County','County2','National')
)
,CountyProvSH as (
  --取河南、四川的数据（肿瘤+非肿瘤）
  SELECT YYYYMM,pack_cod,SUM(Counting_Unit) Counting_Unit,sum(Counting_Unit_LY) Counting_Unit_LY,SUM(Sales_Unit) Sales_Unit,sum(Sales_Unit_LY) Sales_Unit_LY,SUM(SalesValue_LC) SalesValue_LC,sum(SalesValue_LC_LY) SalesValue_LC_LY
  FROM CountyProv
  WHERE region_cod IN ('Sichuan','Henan')
  GROUP BY YYYYMM,pack_cod
),BBU_Other as (
  --减去重复的四川河南部分的BBU County数据，即可得到BBU Others数据
  --BBU County = BBU_OtherProv+四川+河南(肿瘤+非肿瘤产品)
    SELECT A.YYYYMM,'BBU_OtherProv' region_cod,A.pack_cod,A.Counting_Unit-NVL(B.Counting_Unit,0) Counting_Unit,A.Counting_Unit_LY-NVL(B.Counting_Unit_LY,0) Counting_Unit_LY,
  A.Sales_Unit-NVL(B.Sales_Unit,0) Sales_Unit, A.Sales_Unit_LY-NVL(B.Sales_Unit_LY,0) Sales_Unit_LY,A.SalesValue_LC-NVL(B.SalesValue_LC,0) SalesValue_LC,A.SalesValue_LC_LY-NVL(B.SalesValue_LC_LY,0) SalesValue_LC_LY
  FROM tmp.tmp_ims_county_fact_sales_sum A
   LEFT JOIN CountyProvSH B ON A.YYYYMM = B.YYYYMM AND 
   A.pack_cod = B.pack_cod
  WHERE A.region_cod = 'County'
),CountyProvOthers_tmp as (
  --取山东、江苏、浙江、广东四省数据
  SELECT YYYYMM,pack_cod,SUM(Counting_Unit) Counting_Unit,sum(Counting_Unit_LY) Counting_Unit_LY,SUM(Sales_Unit) Sales_Unit,sum(Sales_Unit_LY) Sales_Unit_LY,SUM(SalesValue_LC) SalesValue_LC,sum(SalesValue_LC_LY) SalesValue_LC_LY
  FROM CountyProv
  WHERE region_cod IN ('Shandong','Jiangsu','Zhejiang','Guangdong')
  GROUP BY YYYYMM,pack_cod
),CountyProvFJ as (
  --通过OBU东五的数据倒减四省数据，得到福建省的数据
  SELECT A.YYYYMM, 'OBU_OtherProv' region_cod,A.pack_cod,A.Counting_Unit-NVL(B.Counting_Unit,0) Counting_Unit,A.Counting_Unit_LY-NVL(B.Counting_Unit_LY,0) Counting_Unit_LY,
  A.Sales_Unit-NVL(B.Sales_Unit,0) Sales_Unit, A.Sales_Unit_LY-NVL(B.Sales_Unit_LY,0) Sales_Unit_LY,A.SalesValue_LC-NVL(B.SalesValue_LC,0) SalesValue_LC,A.SalesValue_LC_LY-NVL(B.SalesValue_LC_LY,0) SalesValue_LC_LY
  FROM ObuEast5  A
   LEFT JOIN CountyProvOthers_tmp B ON A.YYYYMM = B.YYYYMM AND A.pack_cod = B.pack_cod
),Result_NonOthers as (
  --汇总省份+BBU other+obu otter
  SELECT YYYYMM,region_cod,pack_cod,SalesValue_LC,SalesValue_LC_LY,Counting_Unit,Counting_Unit_LY,Sales_Unit,Sales_Unit_LY
  FROM CountyProv
  UNION ALL
  SELECT YYYYMM,region_cod,pack_cod,SalesValue_LC,SalesValue_LC_LY,Counting_Unit,Counting_Unit_LY,Sales_Unit,Sales_Unit_LY
  FROM CountyProvFJ
  UNION ALL
  SELECT YYYYMM,region_cod,pack_cod,SalesValue_LC,SalesValue_LC_LY,Counting_Unit,Counting_Unit_LY,Sales_Unit,Sales_Unit_LY 
  FROM BBU_Other
),Result_NonOthersGroup AS (
  --用于全国产品数据倒减，得到other省份数据
  SELECT YYYYMM,pack_cod,SUM(Counting_Unit) Counting_Unit,sum(Counting_Unit_LY) Counting_Unit_LY,SUM(Sales_Unit) Sales_Unit,sum(Sales_Unit_LY) Sales_Unit_LY,SUM(SalesValue_LC) SalesValue_LC,sum(SalesValue_LC_LY) SalesValue_LC_LY
  FROM Result_NonOthers
  GROUP BY YYYYMM,pack_cod
)
SELECT YYYYMM,region_cod,pack_cod,SalesValue_LC,SalesValue_LC_LY,Counting_Unit,Counting_Unit_LY,Sales_Unit,Sales_Unit_LY
  FROM Result_NonOthers
  UNION ALL
  SELECT A.YYYYMM,'Others' region_cod,A.pack_cod,A.SalesValue_LC-NVL(B.SalesValue_LC,0) SalesValue_LC,A.SalesValue_LC_LY-NVL(B.SalesValue_LC_LY,0) SalesValue_LC_LY,A.Counting_Unit-NVL(B.Counting_Unit,0) Counting_Unit,A.Counting_Unit_LY-NVL(B.Counting_Unit_LY,0) Counting_Unit_LY,
  A.Sales_Unit-NVL(B.Sales_Unit,0) Sales_Unit, A.Sales_Unit_LY-NVL(B.Sales_Unit_LY,0) Sales_Unit_LY
  FROM (SELECT * from tmp.tmp_ims_county_fact_sales_sum WHERE region_cod = 'National') A
   LEFT JOIN Result_NonOthersGroup B ON A.YYYYMM = B.YYYYMM AND A.pack_cod = B.pack_cod
