SAP B1中可用库存的历史

时间:2017-09-07 22:19:44

标签: tsql sql-server-2008-r2 report sap reporting

如何找到过去特定日期的可用库存?

我知道如何在过去的任何日期获得OnHand:

    SELECT LocType, LocCode, ItemCode, SUM(InQty-OutQty) [OnHand]
    From OIVL
    Where DocDate <= '7/7/2017'
        AND ITEMCODE = 'xyz' 
    Group by LocType, LocCode, ItemCode

但这并不能解释所犯下的内容。它显示了手头的内容,但没有显示实际可用的内容(OnHand - Committed = Available)。那么,我怎样才能获得过去日期的“可用”数字?

谢谢!

1 个答案:

答案 0 :(得分:1)

如果这有助于其他人......以下是如何在SAP BusinessOne中获取商品数量和成本历史记录的示例。将要查看历史记录的日期和项目传递到该功能中。如果省略项目代码,则返回所有项目。

CREATE FUNCTION [dbo].[ufnStockAndCostHistory]
(	
	@EndDate DATETIME, @ItemCode NVARCHAR(255) = 'All'
)
RETURNS TABLE 
AS
RETURN 
(
	WITH Cost AS (
		SELECT
			OINM.TransNum,
			OINM.ItemCode,
			OINM.Warehouse,
			OINM.Balance,
			OINM.CreateDate
		FROM OINM (NOLOCK)
			JOIN OITM (NOLOCK) ON OITM.ItemCode = OINM.ItemCode
			JOIN OITB (NOLOCK) ON OITM.ItmsGrpCod=OITB.ItmsGrpCod
		WHERE  OINM.CreateDate <= @EndDate

	),
	FinalCostByWarehouse AS (
		SELECT *
		FROM Cost 
		WHERE TransNum = (
			SELECT MAX(TransNum)
			FROM Cost sub
			WHERE Cost.ItemCode = sub.ItemCode
				AND Cost.Warehouse = sub.Warehouse)
	),
	Quantity AS (
		SELECT
			OINM.ItemCode,
			OINM.Warehouse,
			SUM(OINM.InQty) - SUM(OINM.OutQty) [Qty]
		FROM OINM (NOLOCK)
			JOIN OITM (NOLOCK) ON OITM.ItemCode = OINM.ItemCode
			JOIN OITB (NOLOCK) ON OITM.ItmsGrpCod=OITB.ItmsGrpCod
		WHERE OINM.CreateDate <= @EndDate
		group by OINM.ItemCode, OINM.Warehouse
	)
	SELECT 
		OITW.ItemCode,
		OITW.WhsCode,
		c.Balance [Cost],
		q.Qty [Quantity]
	FROM OITW
		LEFT JOIN FinalCostByWarehouse c ON OITW.ItemCode = c.ItemCode
			AND OITW.WhsCode = c.Warehouse
		LEFT JOIN Quantity q ON OITW.ItemCode = q.ItemCode
			AND OITW.WhsCode = q.Warehouse
	WHERE (OITW.ItemCode = @ItemCode
		OR @ItemCode = 'All')
		AND (ISNULL(c.Balance,0) > 0
			OR ISNULL(q.Qty,0) > 0)
)

用法如下:

SELECT *
FROM ufnStockAndCostHistory('9/15/2017','B107A-12HB')