Hi,
Try this:
select
Distinct b.Itemcode,b.Item_Description,b.UOM,
b.rate as Rate,b.Opening_Stock,b.Receipt,b.Issue,
b.Material_Revalue,b.Closing_Stock,b.Closing_Value
from
(select *,(case closing_stock when 0 then 0 else(Closing_Value/Closing_Stock)end) as Rate
from( SELECT T0.Itemcode as 'Itemcode' ,min(T0.Dscription) as 'Item_Description',
max(T1.InvntryUom) as UOM,
(isnull((Select sum(isnull(inqty,0)) from OINM O1
where O1.itemcode=T1.itemcode and O1.docdate<'[%0]' and O1.transtype in (58,59,20,16,67,-2)),0) -
isnull((Select sum(isnull(outqty,0)) from OINM O1
where O1.itemcode=T1.itemcode and
O1.docdate<'[%0]' and O1.transtype in (58,21,19,60,15,67,-2,13)),0)+
(isnull((Select sum(isnull(inqty,0)) from OINM O1 where O1.itemcode=T1.itemcode and
O1.docdate>='[%0]' and O1.docdate<='[%1]' and
O1.transtype in (-2)),0) -
isnull((Select sum(isnull(outqty,0))
from OINM O1 where O1.itemcode=T1.itemcode and
O1.docdate>='[%0]' and O1.docdate<='[%1]' and
O1.transtype in (-2)),0)))as Opening_Stock,
(isnull((Select sum(isnull(inqty,0)) from OINM O1 where O1.itemcode=T1.itemcode and
O1.docdate>='[%0]' and O1.docdate<='[%1]' and
O1.inqty>0 and O1.transtype in (20,18)),0) +
isnull((Select sum(isnull(inqty,0)) from OINM O1 where O1.itemcode=T1.itemcode and
O1.docdate>='[%0]' and O1.docdate<='[%1]' and
O1.inqty>0 and O1.transtype in (67)),0) +
isnull((Select sum(isnull(inqty,0)) from OINM O1 where O1.itemcode=T1.itemcode and
O1.docdate>='[%0]' and O1.docdate<='[%1]' and
O1.inqty>0 and
O1.transtype in (58,59,16,14)),0)) as Receipt,
(isnull((Select sum(isnull(outqty,0)) from OINM O1 where O1.itemcode=T1.itemcode and
O1.docdate>='[%0]' and O1.docdate<='[%1]' and O1.outqty>0 and
O1.transtype in (13,15)),0) +
isnull((Select sum(isnull(outqty,0))
from OINM O1 where O1.itemcode=T1.itemcode and
O1.docdate>='[%0]' and O1.docdate<='[%1]' and
O1.outqty>0 and
O1.transtype in (67)),0)+
isnull((Select sum(isnull(outqty,0))
from OINM O1 where O1.itemcode=T1.itemcode and
O1.docdate>='[%0]' and O1.docdate<='[%1]' and
O1.outqty>0 and O1.transtype in (58,60,21,19)),0)) as Issue,
isnull((select sum(Transvalue) from OINM O1
where O1.itemcode=T1.itemcode and
O1.docdate<='[%1]' and O1.transtype in (162)),0) as Material_Revalue,
(isnull((Select sum(isnull(inqty,0)) from OINM O1
where O1.itemcode=T1.itemcode and
O1.docdate<='[%1]' and O1.transtype in (58,59,20,16,14,19,67,-2)),0) -
isnull((Select sum(isnull(outqty,0)) from OINM O1
where O1.itemcode=T1.itemcode and
O1.docdate<='[%1]' and O1.transtype in (58,21,19,60,15,67,-2,13)),0))as Closing_Stock,
isnull((select sum(Transvalue) from OINM O1
where O1.itemcode=T1.itemcode and
O1.docdate<='[%1]' and
O1.transtype in (58,18,-2,67,59,20,16,14,15,13,21,19,60,69,162)),0) as Closing_Value
FROM OINM T0 INNER JOIN OITM T1 ON T0.ItemCode = T1.ItemCode INNER JOIN OITW T2
ON T1.ItemCode = T2.ItemCode INNER JOIN OITB B1 ON T1.ItmsGrpCod=B1.ItmsGrpCod
where ((B1.ItmsGrpNam>='[%2]' and
B1.ItmsGrpNam<='[%3]') or ('[%2]'='' and '[%3]'=''))
GROUP BY T1.itemcode,T0.Itemcode
)a
Where (a.Opening_Stock + a.Receipt + a.Issue + a.Material_Revalue +
a.Closing_Stock + a.Closing_Value) <> 0
)b
Thanks & Regards,
Nagarajan