Dear,
I have face one issue from below query.
how can i add date field from this query when any PO or GRPO Created.
Right now i am using GetDate() which get me current date instead of i want GRPO Date or PO date.
Declare @FromDate Datetime
Declare @ToDate Datetime
Declare @Whse nvarchar(10)
Declare @Itmgroup nvarchar(20)
Declare @time Datetime
Declare @Brand nvarchar(10)
select @Itmgroup = T2.ItmsGrpNam from OITB T2
select @FromDate = min(S0.Docdate) from OINM S0 where S0.Docdate >='[%0]'
select @ToDate = max(S1.Docdate) from OINM s1 where S1.Docdate <='[%1]'
select @Whse = Max(s2.Warehouse) from OINM S2 Where S2.Warehouse = '[%2]'
select @time = GetDate()
select @Brand = 'ZK'
Select T2.ItmsGrpnam as 'Product Line', a.Itemcode as 'Model',@Brand as 'Brand',sum(a.OpeningBalance) as 'Last Balance', sum(a.OUT) as '(Yesterday) Total Dispatch Quantity',
((sum(a.OpeningBalance) + sum(a.INq)) - Sum(a.OUT)) as '(Today) Current Balance',@time as 'Entry Time'
from( Select T2.ItmsGrpNam,N1.Warehouse, N1.Itemcode, N1.Dscription, (sum(N1.inqty)-sum(n1.outqty))
as OpeningBalance, 0 as INq, 0 as OUT From OINM N1 INNER JOIN OITM I1 ON N1.ItemCode = I1.ItemCode INNER JOIN OITB T2 ON I1.ItmsGrpCod = T2.ItmsGrpCod
Where N1.DocDate < @FromDate and N1.Warehouse = @Whse Group By T2.ItmsGrpNam,N1.Warehouse,N1.ItemCode,
N1.Dscription
Union All
select T2.ItmsGrpNam,N1.Warehouse, N1.Itemcode, N1.Dscription, 0 as OpeningBalance,
sum(N1.inqty) , 0 as OUT From OINM N1 INNER JOIN OITM I1 ON N1.ItemCode = I1.ItemCode INNER JOIN OITB T2 ON I1.ItmsGrpCod = T2.ItmsGrpCod Where N1.DocDate >= @FromDate and N1.DocDate <= @ToDate
and N1.Inqty >0 and N1.Warehouse = @Whse Group By T2.ItmsGrpNam,N1.Warehouse,N1.ItemCode,N1.Dscription
Union All
select T2.ItmsGrpNam, N1.Warehouse, N1.Itemcode, N1.Dscription, 0 as OpeningBalance, 0 , sum(N1.outqty) as OUT
From dbo.OINM N1 INNER JOIN OITM I1 ON N1.ItemCode = I1.ItemCode INNER JOIN OITB T2 ON I1.ItmsGrpCod = T2.ItmsGrpCod Where N1.DocDate >= @FromDate and N1.DocDate <=@ToDate and N1.OutQty > 0
and N1.Warehouse = @Whse Group By T2.ItmsGrpNam,N1.Warehouse,N1.ItemCode,N1.Dscription) a, OITM I1 LEFT JOIN OITB T2 ON I1.[ItmsGrpCod] = T2.[ItmsGrpCod]
where a.ItemCode=I1.ItemCode
Group By T2.ItmsGrpNam,a.Itemcode Having sum(a.OpeningBalance) + sum(a.INq) + sum(a.OUT) > 0 Order By a.Itemcode
Thanks & Regards,
Nikunj Mehta