跳至主要內容

库存查询出入库结存+期初+期末(按期间范围)

五六零网校小于 1 分钟

代码示例

DECLARE @A DATE
DECLARE @B DATE
SELECT @A= T0.[DocDate] FROM OIVL T0 WHERE T0.[DocDate] =[%1]
SELECT @B= T0.[DocDate] FROM OIVL T0 WHERE T0.[DocDate] =[%2] 

SELECT T2.ItemCode AS 物料号, T0.ItemName , t0.codebars AS '名称', T0.DfltWH AS 仓库编号, t2.avgprice
	, ISNULL((
		SELECT SUM(T3.InQty - T3.OutQty)
		FROM OINM T3
		WHERE T2.ItemCode = T3.ItemCode
			AND T2.WhsCode = T3.Warehouse
			AND T3.DocDate <@A
		GROUP BY T3.ItemCode, T3.Warehouse
	), 0) AS 期初数量
	, ISNULL((
		SELECT SUM(T3.InQty)
		FROM OINM T3
		WHERE T2.ItemCode = T3.ItemCode
			AND T2.WhsCode = T3.Warehouse
			AND T3.DocDate >= @A
			AND T3.DocDate <=@B
		GROUP BY T3.ItemCode, T3.Warehouse
	), 0) AS 入库数量
	, ISNULL((
		SELECT SUM(T3.OutQty)
		FROM OINM T3
		WHERE T2.ItemCode = T3.ItemCode
			AND T2.WhsCode = T3.Warehouse
			AND T3.DocDate >=@A
			AND T3.DocDate <= '[%2]'
		GROUP BY T3.ItemCode, T3.Warehouse
	), 0) AS 出库数量
	, ISNULL((
		SELECT SUM(T3.InQty - T3.OutQty)
		FROM OINM T3
		WHERE T2.ItemCode = T3.ItemCode
			AND T2.WhsCode = T3.Warehouse
			AND T3.DocDate <=@B
		GROUP BY T3.ItemCode, T3.Warehouse
	), 0) AS 期末数量
FROM OITM T0, OWHS T1, OITW T2
WHERE T0.ItemCode = T2.ItemCode
	AND T1.WhsCode = T2.WhsCode
	AND T1.WhsCode = T0.DfltWH
	AND (T1.WhsCode = '[%3]' or  '[%3]' ='')
GROUP BY T2.ItemCode, T0.ItemName, T2.WhsCode, T1.WhsName, T0.DfltWH, t2.avgprice, t0.codebars
ORDER BY T2.ItemCode
FOR BROWSE