[易飞]呆滞仓物料预计领用

最近公司要我写一个预警,每周发送给QC,PMC等。

需求:呆滞仓物料在未来两月内的预计领用报表。

写一存储过程:

-- =============================================   
-- Author: <David Gong>   
-- Create date: <2013-05-23>    
-- Description: <呆滞仓物料预计领用预警>   
-- ============================================= 
ALTER Proc [dbo].[UP_IdleStockPreNeed]
(
	@0 as nvarchar(15)=null
)
as
begin

---订单预计领用--------
declare @startdate as nvarchar(8),@enddate as nvarchar(8)
set @startdate=Convert(varchar(10),Getdate(),112) 
set @enddate=Convert(varchar(10),dateadd(MONTH,2,Getdate()),112) ;

with ct as(
select a.Item,sum(a.Qty) as Qty from (
select TD004 as Item,sum(cast(TD008-TD009+TD024-TD025 as int)) as Qty from COPTD
where TD013>=@startdate and TD013<=@enddate
group by TD004
--工单预计领用-------
UNION ALL
select TB003 as Item,sum(cast(TB004-TB005 as decimal(16,2))) AS Qty from MOCTA inner join MOCTB on TA001=TB001 AND TA002=TB002
where TA009>=@startdate and TA009<=@enddate
GROUP BY TB003) a
group by a.Item)

select b.*,ct.Qty as 预计领用  from (
select MC001 品号,MB002,MB003,SUM(MC007) 数量 ,SUM(MC008) 金额
from INVMC INNER JOIN  INVMB on MC001=MB001 
    
where MC002='04' AND MC007>0
GROUP BY MC001,MB002,MB003) b  left join ct on ct.Item=b.品号
end 


你可能感兴趣的:([易飞]呆滞仓物料预计领用)