2024-09-29 18:21:17 +08:00
|
|
|
SELECT
|
2024-10-18 14:57:28 +08:00
|
|
|
${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," max(1)","")}
|
|
|
|
-- ${if(len(sIndex)>0," 1","")}
|
|
|
|
${if(len(sIndex)>0&& FIND(sIndex,sIndex)>0," 1","")}
|
|
|
|
|
2024-09-29 18:21:17 +08:00
|
|
|
|
|
|
|
FROM
|
2024-10-18 14:57:28 +08:00
|
|
|
(
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 销售订单(订单金额)
|
|
|
|
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
|
2024-10-18 14:57:28 +08:00
|
|
|
-- AND TO_CHAR(order_time, 'YYYY-MM-DD') >= '2024-10-11'
|
|
|
|
-- AND TO_CHAR(order_time, 'YYYY-MM-DD') < '${sEndDate}'
|
|
|
|
|
|
|
|
${SWITCH(sDateGroup
|
|
|
|
,"'y'","AND LEFT(order_time, 4) > LFET('" + sStartDate + "', 4) AND LFET(order_time, 4) < LFET('" + sEndDate + "', 4)"
|
|
|
|
,"'q'","AND TO_CHAR(order_time, 'YYYY-MM-DD') > TO_CHAR('" + sStartDate + "', 'YYYY-MM-DD') AND TO_CHAR(order_time, 'YYYY-MM-DD') < TO_CHAR('" + sEndDate + "', 'YYYY-MM-DD')"
|
|
|
|
,"'m'","AND LFET(order_time,7) > LFET('" + sStartDate + "', 7) AND LFET(order_time, 7) < LFET('" + sEndDate + "', 7)"
|
|
|
|
,"'d'","AND TO_CHAR(order_time, 'YYYY-MM-DD') > TO_CHAR('" + sStartDate + "', 'YYYY-MM-DD') AND TO_CHAR(order_time, 'YYYY-MM-DD') < TO_CHAR('" + sEndDate + "', 'YYYY-MM-DD')")}
|
|
|
|
|
|
|
|
|
|
|
|
-- 事业部
|
|
|
|
${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 +"')"))}
|
2024-09-29 18:21:17 +08:00
|
|
|
|
2024-09-30 16:57:39 +08:00
|
|
|
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 -- 子经销商名称
|
2024-10-18 14:57:28 +08:00
|
|
|
|
|
|
|
UNION ALL
|
2024-09-29 18:21:17 +08:00
|
|
|
-- 合同汇总
|
2024-09-30 16:57:39 +08:00
|
|
|
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 -- 子经销商名称
|
2024-09-29 18:21:17 +08:00
|
|
|
|
2024-09-30 16:57:39 +08:00
|
|
|
UNION ALL
|
2024-09-29 18:21:17 +08:00
|
|
|
-- 销售发货
|
2024-09-30 16:57:39 +08:00
|
|
|
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 -- 发货件数
|
2024-10-18 14:57:28 +08:00
|
|
|
,SUM(pack_base_num) AS ship_botnum-- 发货瓶数
|
2024-09-30 16:57:39 +08:00
|
|
|
,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
|
2024-10-18 14:57:28 +08:00
|
|
|
|
|
|
|
${if(len(sDim)>0," GROUP BY " + sDim ," ")}
|