-- *****************************
-- 创建人员: 杨坤安
-- 创建时间: 2024-09-29
-- 功能描述: 销售业绩组合统计表
-- *****************************

-- 思路： 1. 先按维度汇总，实际上是张汇总表
--       2. 按维度去关联，形成一张大表
--       3. 根据不同的筛选条件去筛选数据

--  动态维度：年、季、月、日、事业部、事业部大区、办事处、城市单元、公司大区、省、市、区、产品线、品项、产品、主经销商、子经销商
-- 指标：合同、订单、回款、发货（金额、件数、瓶数）、实物库存、

-- 默认展示到办事处维度 指标展示全部
-- 去除区
-- 年月季度日 单选 可以选择区间
-- 根据不同的



-- EXTRACT(YEAR FROM order_time)  AS order_year       -- 订单年度
-- ,EXTRACT(QUARTER FROM order_time)  AS order_quar    -- 订单季度
-- ,EXTRACT(MONTH FROM order_time)    AS order_month   -- 订单月份
-- ,TO_CHAR(order_time, 'YYYY-MM-DD')  AS order_time   -- 订单时间
-- ,EXTRACT(QUARTER FROM A.signed_date) AS ct_quar   -- 合同季度
-- ,EXTRACT(MONTH FROM A.signed_date) AS ct_month   -- 合同月份
-- ,TO_CHAR(A.signed_date, 'YYYY-MM-DD')  AS signed_date   -- 合同签订日期
-- EXTRACT(YEAR FROM bill_date)  AS order_year       -- 单据年度
--      ,EXTRACT(QUARTER FROM bill_date)  AS order_quar    -- 单据季度
--      ,EXTRACT(MONTH FROM bill_date)    AS order_month   -- 单据月份
--      ,TO_CHAR(bill_date, 'YYYY-MM-DD')  AS order_time   -- 单据时间
-- EXTRACT(YEAR FROM deli_time)     AS order_year  -- 发货年度
--          , EXTRACT(QUARTER FROM deli_time)  AS order_quar  -- 发货季度
--          , EXTRACT(MONTH FROM deli_time)    AS order_month -- 发货月份
--          , TO_CHAR(deli_time, 'YYYY-MM-DD') AS order_time  -- 发货时间
--
--          , CASE
--                WHEN stock_month IN ('01', '02', '03') THEN 1
--                WHEN stock_month IN ('04', '05', '06') THEN 2
--                WHEN stock_month IN ('07', '08', '09') THEN 3
--                WHEN stock_month IN ('10', '11', '12') THEN 4
-- END                                            AS stock_quar
--          , stock_month                                 AS stock_month
--          , CONCAT(stock_year, '-', stock_month, '-01') AS stock_date
--
--
-- 参数
-- 事业部              sBu
-- 事业部大区            sBuRegion
-- 办事处                sOffice
-- 城市单元             sCityUnit
-- 公司大区                 sCompRegion

 --   ${if(len(sCityUnit) == 0,"","and city_unit_code IN ('"+sBu+"')")}
-- 省                   sProvince
-- 市                   sCity
-- 区                   sCounty
-- 产品线                sPl
-- 品项                  sPi
-- 产品                  sProduct
-- 经销商                  sDealName
-- 子主经销商             sDeal
-- 开始时间              sStartDate
-- 结束时间              sEndDate

SELECT
    T1.bu_name        AS bu_name       -- 事业部名称
     ,T1.region_name    AS region_name   -- 大区名称
     ,T1.office_name    AS office_name   -- 办事处名称
     ,T1.city_unit_name AS city_unit_name-- 城市单元名称
     ,T1.province_name  AS province_name -- 省
     ,T1.city_name      AS city_name     -- 市
     ,T1.pl_name        AS pl_name       -- 产品线名称
     ,T1.pi_name        AS pi_name       -- 品项
     ,T1.product_name   AS product_name  -- 产品名称
     ,T1.deal_name      AS deal_name     -- 经销商名称
     ,T1.src_deal_name  AS src_deal_name -- 子经销商名称
     ,SUM(T1.ct_amt )   AS ct_amt  -- 合同金额
     ,SUM(T1.total_amt) AS total_amt -- 订单金额
     ,SUM(T1.clc_amt)   AS clc_amt      -- 回款金额
     ,SUM(T1.deli_amt)  AS deli_amt     -- 发货金额
     ,SUM(T1.ship_num)  AS ship_num     -- 发货件数
     ,SUM(T1.ship_botnum)   AS ship_botnum  -- 发货瓶数
     ,SUM(T1.real_amt)  AS real_amt     -- 实物库存

FROM
-- 销售订单(订单金额)
        SELECT
            bu_name        -- 事业部名称
             ,region_name    -- 大区名称
             ,office_name    -- 办事处名称
             ,city_unit_name -- 城市单元名称
             ,province_name  -- 省
             ,city_name      -- 市
             ,pl_name        -- 产品线名称
             ,pi_name        -- 品项
             ,product_name   -- 产品名称
             ,deal_name      -- 经销商名称
             ,src_deal_name  -- 子经销商名称
             ,0     AS  ct_amt  -- 合同金额
             ,SUM(COALESCE(d_amt,0)) + SUM(COALESCE(e_amt,0)) + SUM(COALESCE(f_amt,0)) + SUM(COALESCE(fz_amt,0)) + SUM(COALESCE(q_amt,0)) AS total_amt -- 订单金额
             ,0     AS clc_amt      -- 回款金额
             ,0     AS deli_amt     -- 发货金额
             ,0     AS ship_num     -- 发货件数
             ,0     AS ship_botnum  -- 发货瓶数
             ,0     AS real_amt     -- 实物库存
        FROM dm.v_ag_sal_order  -- 销售订单视图
        WHERE 1 = 1
          AND TO_CHAR(order_time, 'YYYY-MM-DD') >= '${sStartDate}'
          AND TO_CHAR(order_time, 'YYYY-MM-DD') < '${sEndDate}'
          -- 事业部
            ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
    -- 大区
            ${if(len(sCompRegion) == 0,"","and region_code IN ('"+ sCompRegion+"')")}
          -- 办事处
            ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
          -- 城市单元
            ${if(len(sCityUnit) == 0,"","and city_unit_code IN ('"+ sCityUnit+"')")}
          -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
          -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
          -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
          -- 品项
            ${if(len(sPi) == 0,"","and pi_name IN ('"+ sPi+"')")}
          -- 子主经销商
            ${if(sDeal == 1,if(len(sDealName) == 0,"","and deal_code IN ('"+ sDealName+"')"),if(len(sDealName) == 0,"","and src_deal_code IN ('"+ sDealName +"')"))}

        GROUP BY
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
/*        UNION ALL
-- 合同汇总
        SELECT
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
                ,sum(ct_amt) as ct_amt -- 合同金额
                ,0      AS total_amt  -- 订单金额
                ,0      AS clc_amt -- 回款金额
                ,0      AS deli_amt -- 发货金额
                ,0      AS ship_num -- 发货件数
                ,0      AS ship_botnum-- 发货瓶数
                ,0      AS real_amt     -- 实物库存
        FROM dm.v_ag_sal_contract  -- 合同表  产品+合同ID
        WHERE 1 = 1
          AND TO_CHAR(signed_date, 'YYYY-MM-DD') >= '${sStartDate}'
          AND TO_CHAR(signed_date, 'YYYY-MM-DD') < '${sEndDate}'
        -- 事业部
            ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
        -- 大区
            ${if(len(sCompRegion) == 0,"","and region_code IN ('"+ sCompRegion+"')")}
        -- 办事处
            ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
        -- 城市单元
            ${if(len(sCityUnit) == 0,"","and city_unit_code IN ('"+ sCityUnit+"')")}
        -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 品项
            ${if(len(sPi) == 0,"","and pi_name IN ('"+ sPi+"')")}
        -- 子主经销商
            ${if(sDeal == 1,if(len(sDealName) == 0,"","and deal_code IN ('"+ sDealName+"')"),if(len(sDealName) == 0,"","and src_deal_code IN ('"+ sDealName +"')"))}

        GROUP BY
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
        UNION ALL
        -- 销售回款
        SELECT
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,'' AS pi_name        -- 品项
                ,'' AS product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
                ,0         AS ct_amt -- 合同金额
                ,0         AS total_amt  -- 订单金额
                ,SUM(COALESCE(clc_amt,0)) AS clc_amt -- 回款金额
                ,0         AS deli_amt -- 发货金额
                ,0         AS ship_num -- 发货件数
                ,0         AS ship_botnum-- 发货瓶数
                ,0         AS real_amt     -- 实物库存
        FROM dm.v_ag_sal_collection -- 销售回款
        WHERE 1=1
          AND TO_CHAR(bill_date, 'YYYY-MM-DD') >= '${sStartDate}'
          AND TO_CHAR(bill_date, 'YYYY-MM-DD') < '${sEndDate}'
        -- 事业部
            ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
        -- 大区
            ${if(len(sCompRegion) == 0,"","and region_code IN ('"+ sCompRegion+"')")}
        -- 办事处
            ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
        -- 城市单元
            ${if(len(sCityUnit) == 0,"","and city_unit_code IN ('"+ sCityUnit+"')")}
        -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 子主经销商
            ${if(sDeal == 1,if(len(sDealName) == 0,"","and deal_code IN ('"+ sDealName+"')"),if(len(sDealName) == 0,"","and src_deal_code IN ('"+ sDealName +"')"))}

        GROUP BY
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称

        UNION ALL
-- 销售发货
        SELECT
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
                ,0         AS ct_amt -- 合同金额
                ,0         AS total_amt  -- 订单金额
                ,0         AS clc_amt -- 回款金额
                ,SUM(deli_amt) AS deli_amt   -- 发货金额
                ,SUM(deli_num / pack_base_num)     AS ship_num -- 发货件数
                ,0     AS ship_botnum-- 发货瓶数
                ,0     AS real_amt     -- 实物库存
        FROM dm.v_ag_sal_deliver
        WHERE 1=1
          AND TO_CHAR(deli_time, 'YYYY-MM-DD') >= '${sStartDate}'
          AND TO_CHAR(deli_time, 'YYYY-MM-DD') < '${sEndDate}'
        -- 事业部
            ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
        -- 大区
            ${if(len(sCompRegion) == 0,"","and region_code IN ('"+ sCompRegion+"')")}
        -- 办事处
            ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
        -- 城市单元
            ${if(len(sCityUnit) == 0,"","and city_unit_code IN ('"+ sCityUnit+"')")}
        -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 品项
            ${if(len(sPi) == 0,"","and pi_name IN ('"+ sPi+"')")}
        -- 子主经销商
            ${if(sDeal == 1,if(len(sDealName) == 0,"","and deal_code IN ('"+ sDealName+"')"),if(len(sDealName) == 0,"","and src_deal_code IN ('"+ sDealName +"')"))}

        GROUP BY
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
        UNION ALL
        -- 实物库存
        SELECT
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
                ,0     AS ct_amt -- 合同金额
                ,0      AS total_amt  -- 订单金额
                ,0      AS clc_amt -- 回款金额
                ,0     AS deli_amt   -- 发货金额
                ,0     AS ship_num -- 发货件数
                ,0     AS ship_botnum-- 发货瓶数
                ,SUM(end_num * price)  AS real_amt   -- 实物库存金额
        FROM dm.v_ag_sal_deal_stock
        WHERE 1=1
          AND CONCAT(stock_year, '-', stock_month, '-01') >= '${sStartDate}'
          AND CONCAT(stock_year, '-', stock_month, '-01') < '${sEndDate}'
        -- 事业部
            ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
        -- 大区
            ${if(len(sCompRegion) == 0,"","and region_code IN ('"+ sCompRegion+"')")}
        -- 办事处
            ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
        -- 城市单元
            ${if(len(sCityUnit) == 0,"","and city_unit_code IN ('"+ sCityUnit+"')")}
        -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 品项
            ${if(len(sPi) == 0,"","and pi_name IN ('"+ sPi+"')")}
        -- 子主经销商
            ${if(sDeal == 1,if(len(sDealName) == 0,"","and deal_code IN ('"+ sDealName+"')"),if(len(sDealName) == 0,"","and src_deal_code IN ('"+ sDealName +"')"))}

        GROUP BY
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称

        */
    )T1
GROUP BY
    T1.bu_name
       ,T1.region_name
       ,T1.office_name
       ,T1.city_unit_name
       ,T1.province_name
       ,T1.city_name
       ,T1.pl_name
       ,T1.pi_name
       ,T1.product_name
       ,T1.deal_name
       ,T1.src_deal_name
;