标签:
一 需求
各位销售总:
关于年度会议上提到,希望销售关注备料订单数量动态维护问题, 和IT沟通过, 我们可以在OA提供如下数据, 供各位参考,判断是否有必要调整备料订单的数量, 同时也请提出你们希望还要体现那些信息方便你们判断是否要做备料调整。
客户 料号 品名或规格 备料总数 最近2个月的销量 最新备料时间 公司库存 VMI库存 在制数量
合肥XXX 123 xzm-16187 10000 5000 2015.12.01 10000 10000 3000
数据内容:
1. 客户,品号,产品品名,该产品所有备料订单的合计备料数量,最后一次备料订单的下单时间。
2. 该客户最近2个月的出货数量。(VMI客户按销售对账数量统计)—客户的销售数量时间段, 1个月,2个月还是3个月?请销售确认一下多长比较合适,比较能贴近客户的近期需求。
3. 备料订单产品在公司和VMI仓的实时库存数量, 在途生产工单的数量。
以上信息,希望对你们评估备料数量是否合理有帮助。请在这两天回复一下, 以便IT可以着手做程序设计。
另:
一旦你们确认要减低或取消备料订单数量, 若这些备料订单是我们销售内部申请的, 发出通知后,让销售助理第一时间取消或减少。 同时让物控确认在途相关材料,生产计划,库存等情况, 能调整的及时调整, 造成库存的那也没办法, 做个库存报备,秋后再算,这样能及时将所有库存,含材料库存释放出来, 供其他订单使用, 降低呆滞风险, 同时也减产产能挤压。
若是客户下的备料订单, 任何的数量变动, 必须严格按公司的订单变更流程走。
可用库存函数:
-- =============================================
-- Author: <David Gong>
-- Create date: <>
-- Description: <可用库存>
-- =============================================
ALTER function [dbo].[GetCurrentInvQty](
@ITEM AS VARCHAR(20)
)returns decimal(15,2)
as
begin
declare @qty decimal(15,2), --固定仓库现有库存
@qty1 as decimal(15,2), --未审核销货单据数量
@qty2 as decimal(15,2), --未审核领料单数量
@MB025 AS char(1) --品号属性
set @qty1=0
set @qty2=0
select @MB025=MB025 from INVMB WHERE MB001=@ITEM
select @qty=sum(MC007) from INVMC
where MC001=@ITEM and MC002 in(‘01‘,‘02‘,‘03‘,‘04‘,‘05‘)
group by MC001
select @qty1=isnull(sum(TH008),0) from COPTH WITH(NOLOCK) INNER JOIN COPTG ON TH001=TG001 AND TG002=TH002
where TH004=@ITEM AND TG023=‘N‘
--if (@MB025=‘P‘) --采购件,则计入未审核领料单数量
--BEGIN
select @qty2=isnull(sum(TE005),0) from MOCTE WITH(NOLOCK) inner join MOCTC ON TE001=TC001 AND TE002=TC002
where TE004=@ITEM AND TC009=‘N‘
--END
return @qty-@qty1-@qty2
end
WIP数量
-- =============================================
-- Author: <David Gong>
-- Create date: <2016-01-22>
-- Description: <WIP Qty>
-- =============================================
ALTER function [dbo].[WIPQty](
@Item AS VARCHAR(20)
)returns decimal(18,4)
as
begin
declare @qty as decimal(18,4)
SELECT @qty=SUM(TA015-TA017) from MOCTA
WHERE TA013=‘Y‘ AND TA011 IN (‘2‘,‘3‘) AND TA006=@Item
return @qty
end
VMI仓数量
– =============================================
– Author:
– Create date: <2016-01-22>
– Description:
– =============================================
ALTER function [dbo].[VMIQty](
@CustomerNo as nvarchar(20),
@Item AS VARCHAR(20)
)returns decimal(18,4)
as
begin
declare @qty as decimal(18,4)
select @qty=sum(INVMC.MC007) from INVMC inner join CMSMC ON INVMC.MC002=CMSMC.MC001
where INVMC.MC001=@Item and CMSMC.UDF01=@CustomerNo
group by INVMC.MC001
return @qty
end
最近销货数量函数:
-- =============================================
-- Author: <David Gong>
-- Create date: <2016-01-22>
-- Description: <指定客户 品号 最近 销货数量>
-- =============================================
ALTER function [dbo].[RecentlyDaySaleQty](
@CustomerNo as nvarchar(10),
@Item AS VARCHAR(20),
@Day as int
)returns decimal(18,4)
as
begin
declare @qty as decimal(18,4)
SELECT @qty=sum(TH008) FROM COPTG INNER JOIN COPTH ON TG001=TH001 AND TG002=TH002
WHERE TG023=‘Y‘ AND TG004=@CustomerNo AND TH004=@Item
AND DATEDIFF(DAY,TG003,GETDATE())<=@Day AND DATEDIFF(DAY,TG003,GETDATE())>=0
return @qty
end
最近备料日期
-- =============================================
-- Author: <David Gong>
-- Create date: <2016-01-22>
-- Description: <最近一次备料时间>
-- =============================================
ALTER function [dbo].[RecentlyBLDate](
@CustomerNo as nvarchar(20),
@Item AS VARCHAR(20)
)returns nvarchar(10)
as
begin
declare @TC003 as nvarchar(10)
select @TC003=TC003 from (
SELECT TOP 1 TC003 FROM COPTC INNER JOIN COPTD ON TC001=TD001 AND TC002=TD002
WHERE TC027=‘Y‘ and TC004=@CustomerNo and TD004=@Item
order by TC003 DESC ) as A
return @TC003
end
SQL:
with 备料订单 as(
SELECT TC004 客户编码,MA002 客户简称,TD004 品号,MB002 品名,MB003 规格,sum(TD008-TD009) 备料数量
FROM COPTC WITH(NOLOCK) INNER JOIN COPTD ON TC001=TD001 AND TC002=TD002
left join INVMB ON TD004=MB001
left join COPMA ON TC004=MA001
WHERE TC027=‘Y‘ and TC001=‘2207‘ and (TD008-TD009)<>0
GROUP BY TC004,TD004,MB002,MB003,MA002)
SELECT 客户编码,客户简称,品号,品名,规格,备料数量,
dbo.[RecentlyDaySaleQty](客户编码,品号,60) as 最近2月销量,[dbo].[RecentlyBLDate](客户编码,品号) as 最近备料日期,
dbo.GetCurrentInvQty(品号) as 可用库存,[dbo].[WIPQty](品号) as 在制数量,[dbo].[VMIQty](客户编码,品号) as VMI数量
FROM 备料订单
9秒钟的运行速度:
可用库存开销占用79%。
若备料比较多,运行速度会快速下降
可用库存改进成视图,表与表关联,减少可用库存的全扫描的次数。
Create View [dbo].[AvailableQty]
as
with A as
(
select MC001 as Item,sum(MC007) InvQty from INVMC WITH(NOLOCK)
where MC002 in(‘01‘,‘02‘,‘03‘,‘04‘,‘05‘)
group by MC001
),
B as (
select TH004 as Item,isnull(sum(TH008),0) ShippedQty from COPTH WITH(NOLOCK) INNER JOIN COPTG ON TH001=TG001 AND TG002=TH002
where TG023=‘N‘
GROUP BY TH004
),
C as (
select TE004 as Item,sum(isnull(TE005,0)) requisitionQty from MOCTE WITH(NOLOCK) inner join MOCTC ON TE001=TC001 AND TE002=TC002
where TC009=‘N‘
Group by TE004)
select * from (
select A.Item,A.InvQty-isnull(B.ShippedQty,0)-isnull(C.requisitionQty,0) as AvailableQty from A LEFT JOIN B ON A.Item=B.Item
LEFT JOIN C ON A.Item=C.Item) AS D
WHERE D.AvailableQty<>0
GO
改进后SQL:
with 备料订单 as(
SELECT TC004 客户编码,MA002 客户简称,TD004 品号,MB002 品名,MB003 规格,sum(TD008-TD009) 备料数量
FROM COPTC WITH(NOLOCK) INNER JOIN COPTD ON TC001=TD001 AND TC002=TD002
left join INVMB ON TD004=MB001
left join COPMA ON TC004=MA001
WHERE TC027=‘Y‘ and TC001=‘2207‘ and (TD008-TD009)<>0
GROUP BY TC004,TD004,MB002,MB003,MA002)
SELECT 客户编码,客户简称,品号,品名,规格,备料数量,
dbo.[RecentlyDaySaleQty](客户编码,品号,60) as 最近2月销量,[dbo].[RecentlyBLDate](客户编码,品号) as 最近备料日期,
AvailableQty 可用库存,
[dbo].[WIPQty](品号) as 在制数量,[dbo].[VMIQty](客户编码,品号) as VMI数量,b.区域
FROM 备料订单 left join AvailableQty ON 品号=Item
left join COPMA ON 客户编码=MA001
left join (select MR002,ltrim(MR003) as 区域 from CMSMR where MR001=‘2‘) AS b on COPMA.MA076=b.MR002
速度下降到1秒内完成:
在实际开发过程中,函数若涉及到多表的全扫描,数据量大的时候,资源开销会占用比较大,若能改成一次性获取结果集,最后关联的方式会提高运行效率。
标签:
原文地址:http://blog.csdn.net/david_520042/article/details/50592111