-- ======================================
-- 销售指标月度汇总
-- ======================================


select
    to_char(a.stat_date,'yyyy') as year, -- 年度
    to_char(a.stat_date,'mm') as month, -- 月度
    case when day(a.stat_date)>=1 and day(a.stat_date)<=10
             then '上旬'
         when day(a.stat_date)>=10 and day(a.stat_date)<=20
             then '中旬'
         else '下旬' end as ten_day, -- 旬
    a.indi_type, -- 指标类型
    a.bu_code, -- 事业部编码
    bu.dept_name as bu_name, -- 事业部名称
    a.region_code, -- 大区编码
    dmd.secd_dept_name as region_name, -- 大区名称
    a.office_code,  -- 办事处编码
    dmd.thrd_dept_name as office_name, -- 办事处名称
    a.city_unit_code,  -- 城市单元编码
    dmd.dept_name as city_unit_name, -- 城市单元名称
    max(pv.town_name) as province_name,  -- 省
    max(cit.town_name) as city_name,  -- 市
    a.deal_code,  -- 主经销商编码
    deal.deal_name,  -- 主经销商名称
    a.src_deal_code,  -- 子经销商编码
    sd.deal_name as src_deal_name,  -- 子经销商名称
    a.product_code,   -- 产品编码
    p.product_name,  -- 产品名称
    p.pl_code,  -- 产品线编码
    p.pl_name,   -- 产品线名称
    p.pi_code,   -- 品项编码
    p.pi_name,   -- 品项名称
    p.pr_code,   -- 产品系列编码
    p.pr_name,   -- 产品系列名称
    p.report_pi_name,  -- 报表品项
    p.bu_code as p_bu_code, -- 产品所属事业部编码
    d.dept_name as p_bu_name,  -- 产品所属事业部名称
    p.spec, -- 规格
    p.aroma_type,  -- 香型
    sum(a.num_bu) as num_bu, -- 计事业部数量
    sum(a.num_region) as num_region, -- 计大区数量
    sum(a.num_office) as num_office, -- 计办事处数量
    sum(a.num_city_unit) as num_city_unit, -- 计城市单元数量
    sum(a.num_dealer) as num_dealer,  -- 计经销商数量
    round(sum(a.num_bu/p.pack_base_num),4) as pack_bu, -- 计事业部件数
    round(sum(a.num_region/p.pack_base_num),4) as pack_region, -- 计大区件数
    round(sum(a.num_office/p.pack_base_num),4) as pack_office, -- 计办事处件数
    round(sum(a.num_city_unit/p.pack_base_num),4) as pack_city_unit, -- 计城市单元件数
    round(sum(a.num_dealer/p.pack_base_num),4) as pack_dealer, -- 计经销商件数
    round(sum(a.num_bu * to_number(p.spec) / 1000000),4) as ton_bu, -- 计事业部吨数
    round(sum(a.num_region * to_number(p.spec) / 1000000),4) as ton_region, -- 计大区吨数
    round(sum(a.num_office * to_number(p.spec) / 1000000),4) as ton_office, -- 计办事处吨数
    round(sum(a.num_city_unit * to_number(p.spec) / 1000000),4) as ton_city_unit, -- 计城市单元吨数
    round(sum(a.num_dealer * to_number(p.spec) / 1000000),4) as ton_dealer, -- 计经销商吨数
    sum(amt_bu) / 10000 as amt_bu,  -- 计事业部金额
    sum(amt_region) / 10000 as amt_region, -- 计大区金额
    sum(amt_office) / 10000 as amt_office, -- 计办事处金额
    sum(amt_city_unit) / 10000 as amt_city_unit, -- 计城市单元金额
    sum(amt_dealer) / 10000 as amt_dealer -- 计经销商金额
from
(
    SELECT
    *
    FROM dm.dm_sal_indicator_day
    WHERE to_char(stat_date,'yyyy') = '${sYear}' -- 年度
    AND indi_type = '${sIndi}'              -- 指标类型
    ${if(len(sMonth) == 0,"","and to_char(stat_date,'mm') IN ('"+sMonth+"')")}
    -- 事业部
    ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
    -- 大区
    ${if(len(sRegion) == 0,"","and region_code IN ('"+ sRegion+"')")}
    -- 办事处
    ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
    -- 城市单元
    ${if(len(sCityUnit) == 0,"","and city_unit_code IN ('"+ sCityUnit+"')")}
    -- 产品
    ${if(len(sProduct) == 0,"","and product_code IN ('"+ sProduct +"')")}
    -- 子主经销商
     ${if(sDeal == 1,if(len(sDealName) == 0,"","and deal_code IN ('"+ sDealName+"')"),if(len(sDealName) == 0,"","and src_deal_code IN ('"+ sDealName +"')"))}
)a
left join dwr.dim_product p on a.product_code=p.product_code
left join dwr.dim_department dmd on a.city_unit_code=dmd.dept_code
left join dwr.dim_department bu on a.bu_code=bu.dept_code
left join dwr.dim_department d on p.bu_code=d.dept_code
left join dwr.dim_dealer deal on a.deal_code=deal.deal_code
left join dwr.dim_dealer sd on a.src_deal_code=sd.deal_code
left join dwr.dim_area pv on a.province_code=pv.town_code
left join dwr.dim_area cit on a.city_code=cit.town_code
where 1 = 1

-- 产品线
${if(len(sPl) == 0,"","and p.pl_code IN ('"+ sPl+"')")}


group by to_char(a.stat_date,'yyyy'),
         to_char(a.stat_date,'mm'),
         case when day(a.stat_date)>=1 and day(a.stat_date)<=10
                  then '上旬'
              when day(a.stat_date)>=10 and day(a.stat_date)<=20
                  then '中旬'
              else '下旬' end,
         a.indi_type,
         a.bu_code,
         bu.dept_name,
         a.region_code,
         dmd.secd_dept_name,
         a.office_code,
         dmd.thrd_dept_name,
         a.city_unit_code,
         dmd.dept_name,
         a.deal_code,
         deal.deal_name,
         a.src_deal_code,
         sd.deal_name,
         a.product_code,
         p.product_name,
         p.pl_code,
         p.pl_name,
         p.pi_code,
         p.pi_name,
         p.pr_code,
         p.pr_name,
         p.report_pi_name,
         p.bu_code,
         d.dept_name,
         p.spec,
         p.aroma_type