Hi All
below query is working fine for the items (Quantity and value) where landed cost is not involved but giving wrong cost if the landed cost document is involved somewhere. please correct the same, its bit urgent.
------------------
Declare @FromDate Datetime
Declare @ToDate Datetime
Declare @Group nvarchar(10)
Declare @Whse nvarchar(10)
Set @FromDate = (Select min(S0.Docdate) from dbo.OINM S0 where S0.Docdate >='[%0]')
Set @ToDate = (Select max(S1.Docdate) from dbo.OINM s1 where S1.Docdate <='[%1]')
--Set @Group = (Select Max(s2.ItmsGrpCod) from dbo.OITB S2 Where S2.ItmsGrpNam = 'Mfg-Standard Board')
Set @Whse = (Select Max(s3.Warehouse) from dbo.OINM S3 Where S3.Warehouse = '[%2]' )
Select
Warehouse as 'Warehouse',max(b1.itmsgrpnam) as 'Item group name',
a.Itemcode,
max(a.Dscription) as 'Description'
,(max(a.[Opening Value]) /(case when (SUM([Opening Balance]))=0 then 1 else (SUM([Opening Balance])) end) ) as 'Price',
sum(a.[Opening Balance]) as [Opening Balance],
(sum(a.[Opening Value])) as 'Opening Value',
sum(a.[IN]) as [Receipt],
(sum(a.[IN Value])/ (case when (SUM([IN]))=0 then 1 else (SUM([IN])) end))as 'Receipt Qty Price',
(sum(a.[IN Value]) )as 'Receipt Qty Value',
sum(a.OUT)*-1 as [Issue],
(sum(a.[out Value])/ (case when (SUM([OUT]))=0 then 1 else (SUM([out])) end)) as 'Issue Qty Price',
(sum(a.[out Value]))*-1 as 'Issue Qty Value',
((sum(a.[Opening Balance]) + sum(a.[IN])) - Sum(a.OUT)) as Closing,
((sum(a.[Opening Value]) + sum(a.[IN Value])) - Sum(a.[out Value] ) )as ClosingValue,
((sum(a.[Opening Value]) + sum(a.[IN Value])) - Sum(a.[out Value] ) )/ (Case When (sum(a.[Opening Balance]) + sum(a.[IN])) - Sum(a.OUT) = 0 then 1 else (sum(a.[Opening Balance]) + sum(a.[IN]))End) as ClosingPrice
from(
Select
N1.Warehouse,
N1.Itemcode,
N1.Dscription,
(sum(N1.inqty)-sum(n1.outqty)) as [Opening Balance],
(sum(N1.inqty*N1.CalcPrice)-sum(n1.outqty*CalcPrice)) as [Opening Value],
0 as [IN],
0 as [IN Value],
0 as OUT,
0 AS [out Value]
From dbo.OINM N1
Where
N1.DocDate < @FromDate and N1.Warehouse = isnull(@Whse,N1.Warehouse)
Group By
N1.Warehouse,N1.ItemCode,N1.Dscription,N1.Price
Union All
select
N1.Warehouse,
N1.Itemcode,
N1.Dscription,
0 as [Opening Balance],
0 [Opening Value],
sum(N1.inqty) as [IN],
sum(N1.inqty*CalcPrice) as [IN Value],
0 as OUT,
0 AS [out Value]
From dbo.OINM N1
Where
N1.DocDate >= @FromDate and N1.DocDate <= @ToDate and
N1.Inqty >0
and N1.Warehouse = isnull(@Whse,N1.Warehouse)
Group By
N1.Warehouse,N1.ItemCode,N1.Dscription,N1.price
Union All
select
N1.Warehouse,
N1.Itemcode,
N1.Dscription,
0 as [Opening Balance],
0 [Opening Value],
0 as [IN],
0 as [IN Value],
sum(N1.outqty) as OUT,
sum(N1.outqty*CalcPrice) as [out Value]
From dbo.OINM N1
Where
N1.DocDate >= @FromDate and N1.DocDate <=@ToDate and
N1.OutQty > 0
and N1.Warehouse = isnull(@Whse,N1.Warehouse)
Group By
N1.Warehouse,N1.ItemCode,N1.Dscription,N1.price) a, dbo.OITM I1,dbo.oitb b1
where a.ItemCode=I1.ItemCode and
I1.itmsgrpcod = b1.itmsgrpcod
--and I1.ItmsGrpCod = @Group
Group By
a.Warehouse,
a.Itemcode
Order By a.Itemcode
-----------------------------------------------
Regards
D. K. Tyagi