SELECT
    ${if(len(sDim)>0,sDim + ","," ")}
        ${if(FIND('合同金额', sIndex)>0," ROUND(SUM(T1.ct_amt ) / 10000,2) AS 合同金额,","")}
        ${if(FIND('订单金额', sIndex)>0," ROUND(SUM(T1.total_amt) / 10000,2) AS 订单金额,","")}
        ${if(FIND('回款金额', sIndex)>0," ROUND(SUM(T1.clc_amt) / 10000,2) AS 回款金额,","")}
        ${if(FIND('发货金额', sIndex)>0," ROUND(SUM(T1.deli_amt) / 10000,2) AS 发货金额,","")}
        ${if(FIND('发货件数', sIndex)>0," ROUND(SUM(T1.ship_num) / 10000,2) AS 发货件数,","")}
        ${if(FIND('发货瓶数', sIndex)>0," ROUND(SUM(T1.ship_botnum),0) AS 发货瓶数,","")}
        ${if(FIND('实物库存', sIndex)>0," ROUND(SUM(T1.real_amt) / 10000,2) AS 实物库存,","")}
        ${if(len(sIndex)>0&& FIND(sIndex,sIndex)>0," 1","")}
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 order_time  >=  '${CONCATENATE(sStart_date," 00:00:00.000")}'
        AND order_time < '${CONCATENATE(sEnd_date," 00:00:00.000")}'
        -- 事业部
        ${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(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
        -- 省
        ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
        ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 经销商
        ${if(sIs_srcdeal == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}
        -- 产品线
        ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 品项
        ${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
        -- 产品
        ${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
        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 signed_date >= '${CONCATENATE(sStart_date," 00:00:00.000")}'
          AND signed_date < '${CONCATENATE(sEnd_date," 00:00:00.000")}'
            -- 事业部
            ${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(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
            -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
            -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
            -- 经销商
            ${if(sIs_srcdeal == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}
            -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
            -- 品项
            ${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
            -- 产品
            ${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}

        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  bill_date  >= '${CONCATENATE(sStart_date," 00:00:00.000")}'
          AND  bill_date  < '${CONCATENATE(sEnd_date," 00:00:00.000")}'
        -- 事业部
            ${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(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
        -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 经销商
            ${if(sIs_srcdeal == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}
        -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 品项
            ${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
        -- 产品
            ${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
        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 -- 发货件数
                ,SUM(pack_base_num)     AS ship_botnum-- 发货瓶数
                ,0     AS real_amt     -- 实物库存
        FROM dm.v_ag_sal_deliver
        WHERE 1=1
          AND deli_time  >= '${CONCATENATE(sStart_date," 00:00:00.000")}'
          AND deli_time  < '${CONCATENATE(sEnd_date," 00:00:00.000")}'
        -- 事业部
            ${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(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
        -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 经销商
            ${if(sIs_srcdeal == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}
        -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 品项
            ${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
        -- 产品
            ${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
        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 stock_year=left('${sEnd_date}',4)
        AND stock_month < right('${sEnd_date}',2)
        AND stock_month >= right('${sStart_date}',2)
        -- 事业部
            ${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(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
        -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 经销商
            ${if(sIs_srcdeal == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}
        -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 品项
            ${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
        -- 产品
            ${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
        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
    ${if(len(sDim)>0," GROUP BY " + sDim ," ")}


-- ======================================
-- 数据更新时间
-- ======================================

-- 存在多个表，取表最近的更新时间

SELECT
CONCAT('数据更新日期:',LEFT(MAX(T.update_time),19)) AS update_time
FROM
(
SELECT MAX(update_time) AS update_time FROM dwr.fact_sal_order  --  dm.v_ag_sal_order  -- 销售订单视图主表
UNION ALL
SELECT MAX(update_time) AS update_time FROM dwr.fact_sal_contract -- dm.v_ag_sal_contract  -- 合同表  产品+合同ID
UNION ALL
SELECT MAX(update_time) AS update_time FROM dwr.fact_sal_collection -- dm.v_ag_sal_collection -- 销售回款
UNION ALL
SELECT MAX(update_time) AS update_time FROM dwr.fact_sal_deliver -- dm.v_ag_sal_deliver -- 销售发货
UNION ALL
SELECT MAX(update_time) AS update_time FROM dwr.fact_sal_dealer_stock -- dm.v_ag_sal_deal_stock -- 实物库存
)T
