Hi all,
I have a problem with a stock valuation query that I have built for the Finance Department. Please see below query. It basically shows the Opening Balance Incomming quantity and value, Outgoing Quantity and value and the Closing Balance for a specific period.
However when we try to match the VALUE of the report with the balance sheet, in particular the stock account it does not match.
Does anybody know what I am doing wrong?
THANKS
Select
a.Itemcode,
sum(a.OpeningBalance) as OpeningBalance,
sum(a.OpeningBalanceValue) as OpeningBalanceValue,
sum(a.INq) as 'IN',
sum(a.InValue) as InValue,
sum(a.OUT) as OUT,
sum(a.OutValue) as OutValue,
((sum(a.OpeningBalance) + sum(a.INq)) - Sum(a.OUT)) as Closing ,
((sum(a.OpeningBalanceValue) + sum(a.InValue)) + Sum(a.OutValue)) as ClosingValue,
(Select i.InvntryUom from OITM i where i.ItemCode=a.Itemcode) as UOM
from( Select N1.Itemcode, (sum(N1.inqty)-sum(n1.outqty)) as OpeningBalance,
sum(N1.TransValue) as OpeningBalanceValue,
0 as INq,
0 as InValue,
0 as OUT,
0 as OutValue
From dbo.OINM N1
Where N1.DocDate < '2014-01-01'
Group By N1.ItemCode
Union All
select
N1.Itemcode,
0 as OpeningBalance,
0 as OpeningBalanceValue,
sum(N1.inqty) ,
sum(n1.TransValue) as InValue,
0 as OUT ,
0 as OutValue
From dbo.OINM N1
Where N1.DocDate >= '2014-01-01' and N1.DocDate <= '2014-02-01' and N1.Inqty >0
Group By N1.ItemCode
Union All
select
N1.Itemcode,
0 as OpeningBalance,
0 as OpeningBalanceValue,
0 ,
0,
sum(N1.outqty) as OUT,
sum(n1.TransValue) as OutValue
From dbo.OINM N1
Where N1.DocDate >= '2014-01-01' and N1.DocDate <='2014-02-01' and N1.OutQty > 0
Group By N1.ItemCode) a, dbo.OITM I1
where a.ItemCode=I1.ItemCode
Group By a.Itemcode
Having sum(a.OpeningBalance) + sum(a.INq) + sum(a.OUT) > 0 Order By a.Itemcode