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&& 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 -- 销售订单视图
|
2024-11-12 18:15:06 +08:00
|
|
|
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(len(sDeal) == 0,"","and 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+"')")}
|
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
|
2024-11-12 18:15:06 +08:00
|
|
|
WHERE signed_date >= '${CONCATENATE(sStart_date," 00:00:00.000")}'
|
|
|
|
AND signed_date < '${CONCATENATE(sEnd_date," 00:00:00.000")}'
|
|
|
|
-- 事业部
|
2024-09-30 16:57:39 +08:00
|
|
|
${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
|
2024-11-12 18:15:06 +08:00
|
|
|
-- 大区
|
|
|
|
${if(len(sRegion) == 0,"","and region_code IN ('"+sRegion+"')")}
|
|
|
|
-- 办事处
|
2024-09-30 16:57:39 +08:00
|
|
|
${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
|
2024-11-12 18:15:06 +08:00
|
|
|
-- 城市单元
|
|
|
|
${if(len(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
|
|
|
|
-- 省
|
2024-09-30 16:57:39 +08:00
|
|
|
${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
|
2024-11-12 18:15:06 +08:00
|
|
|
-- 市
|
2024-09-30 16:57:39 +08:00
|
|
|
${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
|
2024-11-12 18:15:06 +08:00
|
|
|
-- 经销商
|
|
|
|
${if(len(sDeal) == 0,"","and deal_code IN ('"+sDeal+"')")}
|
|
|
|
-- 产品线
|
2024-09-30 16:57:39 +08:00
|
|
|
${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
|
2024-11-12 18:15:06 +08:00
|
|
|
-- 品项
|
|
|
|
${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
|
|
|
|
-- 产品
|
|
|
|
${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
|
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 -- 子经销商名称
|
|
|
|
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
|
2024-11-12 18:15:06 +08:00
|
|
|
AND bill_date >= '${CONCATENATE(sStart_date," 00:00:00.000")}'
|
|
|
|
AND bill_date < '${CONCATENATE(sEnd_date," 00:00:00.000")}'
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 事业部
|
|
|
|
${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
|
|
|
|
-- 大区
|
2024-11-12 18:15:06 +08:00
|
|
|
${if(len(sRegion) == 0,"","and region_code IN ('"+sRegion+"')")}
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 办事处
|
|
|
|
${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
|
|
|
|
-- 城市单元
|
2024-11-12 18:15:06 +08:00
|
|
|
${if(len(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 省
|
|
|
|
${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
|
|
|
|
-- 市
|
|
|
|
${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
|
2024-11-12 18:15:06 +08:00
|
|
|
-- 经销商
|
|
|
|
${if(len(sDeal) == 0,"","and deal_code IN ('"+sDeal+"')")}
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 产品线
|
|
|
|
${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
|
2024-11-12 18:15:06 +08:00
|
|
|
-- 品项
|
|
|
|
${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
|
|
|
|
-- 产品
|
|
|
|
${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
|
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 -- 产品线名称
|
|
|
|
,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
|
2024-11-12 18:15:06 +08:00
|
|
|
AND deli_time >= '${CONCATENATE(sStart_date," 00:00:00.000")}'
|
|
|
|
AND deli_time < '${CONCATENATE(sEnd_date," 00:00:00.000")}'
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 事业部
|
|
|
|
${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
|
|
|
|
-- 大区
|
2024-11-12 18:15:06 +08:00
|
|
|
${if(len(sRegion) == 0,"","and region_code IN ('"+sRegion+"')")}
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 办事处
|
|
|
|
${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
|
|
|
|
-- 城市单元
|
2024-11-12 18:15:06 +08:00
|
|
|
${if(len(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 省
|
|
|
|
${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
|
|
|
|
-- 市
|
|
|
|
${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
|
2024-11-12 18:15:06 +08:00
|
|
|
-- 经销商
|
|
|
|
${if(len(sDeal) == 0,"","and deal_code IN ('"+sDeal+"')")}
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 产品线
|
|
|
|
${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
|
|
|
|
-- 品项
|
2024-11-12 18:15:06 +08:00
|
|
|
${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
|
|
|
|
-- 产品
|
|
|
|
${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
|
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 -- 子经销商名称
|
|
|
|
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
|
2024-11-12 18:15:06 +08:00
|
|
|
WHERE stock_year=left('${sEnd_date}',4)
|
|
|
|
AND stock_month < right('${sEnd_date}',2)
|
|
|
|
AND stock_month >= right('${sStart_date}',2)
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 事业部
|
|
|
|
${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
|
|
|
|
-- 大区
|
2024-11-12 18:15:06 +08:00
|
|
|
${if(len(sRegion) == 0,"","and region_code IN ('"+sRegion+"')")}
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 办事处
|
|
|
|
${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
|
|
|
|
-- 城市单元
|
2024-11-12 18:15:06 +08:00
|
|
|
${if(len(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 省
|
|
|
|
${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
|
|
|
|
-- 市
|
|
|
|
${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
|
2024-11-12 18:15:06 +08:00
|
|
|
-- 经销商
|
|
|
|
${if(len(sDeal) == 0,"","and deal_code IN ('"+sDeal+"')")}
|
2024-09-30 16:57:39 +08:00
|
|
|
-- 产品线
|
|
|
|
${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
|
|
|
|
-- 品项
|
2024-11-12 18:15:06 +08:00
|
|
|
${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
|
|
|
|
-- 产品
|
|
|
|
${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
|
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 -- 子经销商名称
|
|
|
|
)T1
|
2024-10-18 14:57:28 +08:00
|
|
|
${if(len(sDim)>0," GROUP BY " + sDim ," ")}
|