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(M1.[TransValue]),0) from oinm M1 where M1.[ItemCode]=T0.[ItemCode] and M1.[DocDate]<=@BeginDate),
入库金额=(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(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(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]