财务审计SBO某一仓库的库存变动表SQL

DECLARE @BeginDate DATETIME,@EndDate DATETIME ,@Warehouse nvarchar(20)
SET @BeginDate ='20100101'/* FROM OINM T0 WHERE T0.DocDate >= */ --'[%0]'
SET @EndDate = '20101231'/* FROM OINM T1 WHERE T1.DocDate <= */ --'[%1]'
SET @Warehouse = '010108'/* FROM OWHS T2 WHERE T2.[WhsCode]= */ --'[%2]'
SELECT 
物料编号=T0.[ItemCode],
物料描述=T0.[Dscription],
组名称=(SELECT [ItmsGrpNam] FROM OITB where OITB.ItmsGrpCod = T1.ItmsGrpCod),
期初数量=(select isnull(sum(M0.[InQty]-M0.[OutQty]),0) from oinm M0 where M0.[ItemCode]=T0.[ItemCode] and M0.[DocDate]< =@BeginDate),
期初金额=(select isnull(sum(M1.[TransValue]),0) from oinm M1 where M1.[ItemCode]=T0.[ItemCode] and M1.[DocDate]< =@BeginDate),
入库数量=(select isnull(sum(RK.[InQty]),0) from oinm RK where RK.[ItemCode]=T0.[ItemCode] and RK.[InQty]>0 and RK.[DocDate]>=@BeginDate and RK.[DocDate]< =@EndDate),
入库金额=(select isnull(sum(RK1.[TransValue]),0) from oinm RK1 where RK1.[ItemCode]=T0.[ItemCode] and RK1.[InQty]>0 and RK1.[DocDate]>=@BeginDate and RK1.[DocDate]< =@EndDate),
出库数量=(select isnull(sum(CK.[OutQty]),0) from oinm CK where CK.[ItemCode]=T0.[ItemCode] and CK.[OutQty]>0 and CK.[DocDate]>=@BeginDate and CK.[DocDate]< =@EndDate),
出库金额=(select -isnull(sum(CK1.[TransValue]),0) from oinm CK1 where CK1.[ItemCode]=T0.[ItemCode] and CK1.[OutQty]>0 and CK1.[DocDate]>=@BeginDate and CK1.[DocDate]< =@EndDate),
库存数量=(select isnull(sum(ME1.[InQty]-ME1.[OutQty]),0) from oinm ME1 where ME1.[ItemCode]=T0.[ItemCode] and ME1.[DocDate]< =@EndDate),
库存金额=(select isnull(sum(ME.[TransValue]),0) from oinm ME where ME.[ItemCode]=T0.[ItemCode] and ME.[DocDate]< =@EndDate)
FROM OINM T0  right JOIN OITM T1 ON T0.ItemCode = T1.ItemCode
 
WHERE T0.[InQty]-T0.[OutQty]<>0
and  T0.[DocDate] >=@BeginDate
and  T0.[DocDate]< =@EndDate
and T0.[Warehouse]=@Warehouse
group by T0.[ItemCode],T0.[Dscription], T1.[ItmsGrpCod]

本文出自 “阚山” 博客,转载请与作者联系!

你可能感兴趣的:(数据库,SAP,SAP,one,Business,b1,Sbo,SBO财务审计)