我的企业一直在尝试在SAP企业中开发一个SQL查询,该查询将显示我们所有项目的“可承诺量”。这个想法是针对某个日期范围(在我们的情况下为90天)中的每一天,采取初始库存位置,减去订单,添加进货库存然后达到当天的最终库存位置。然后在第二天进行计算,依此类推。我们希望每个项目在该日期范围内的最低最终库存排名。
我们已经有一个查询,但问题是我不知道如何更新初始库存位置,因此每天的起始位置保持不变。
declare @start DATE = GETDATE()
declare @end DATE = (GETDATE()+90)
;with DAYS (date)
AS
(
SELECT
@start
UNION ALL
SELECT
DATEADD(dd,1,date)
FROM DAYS
WHERE DATEADD(dd,1,date)<=@end
)
(
SELECT
T100.*,
T0.[ItemCode],
T0.[ItemName],
(
T1.[OnHand] -
(
SELECT
ISNULL(SUM(T6.[OpenQty]),0)
FROM ORDR T5
INNER JOIN RDR1 T6
ON T5.[DocEntry] = T6.[DocEntry]
WHERE
T0.[ItemCode] = T6.[ItemCode]
AND T5.[DocStatus] = 'O'
AND T6.[LineStatus] = 'O'
AND T5.[CANCELED] = 'N'
AND T5.DocDueDate = T100.date
) +
(
SELECT
ISNULL(SUM(T8.[Quantity]),0)
FROM OPOR T7
INNER JOIN POR1 T8
ON T7.[DocEntry] = T8.[DocEntry]
WHERE
T0.[ItemCode] = T8.[ItemCode]
AND T7.[DocStatus] = 'O'
AND T8.[LineStatus] = 'O'
AND T7.[CANCELED] = 'N'
AND T7.DocDueDate = T100.date
)
) AS 'ATP Qty'
INTO #temp
FROM DAYS T100,
OITM T0
INNER JOIN OITW T1
ON T0.[ItemCode] = T1.[ItemCode]
WHERE
T1.[WhsCode] = '01'
)
option (maxrecursion 365)
SELECT
t.[ItemCode] AS [Item Number],
t.[ItemName],
MIN(t.[ATP Qty]) AS [ATP Qty]
FROM #temp t
GROUP BY t.ItemCode, t.[ItemName]
ORDER BY t.ItemCode
DROP TABLE #temp
正如您在上面所看到的,计算采用的是T1。[每日销售量]-库存支出+每天的库存支出。 T1。[Onhand]应该是一个变量,然后每天这样更新(不幸的是,我比SQL更习惯C):
@inventory1 = T1.[OnHand]
@inventory2 = 0
Day 1:
@inventory2 = @inventory1-outs+ins
@inventory1 = @inventory2
Day 2:
@inventory2=@inventory1-outs+ins
@inventory1 = @inventory2
...
任何帮助将不胜感激。