作者:KNN | 来源:互联网 | 2023-08-13 17:25
While fetching details using while loop I am not able to get it as I want. What I want is to apply a loop and get details of each date. I am using SQL 2008.
在使用while循环获取细节时,我无法按照我的意愿获取它。我想要的是应用循环并获取每个日期的详细信息。我正在使用SQL 2008。
Set @i = 1;
While (@NoOfDays >= @i)
Begin
SET @ManufactureQty = (Select isnull(SUM(Quantity),0) as Manufactured from IGN1 inner join OIGN on (OIGN.DocEntry = IGN1.DocEntry)
inner join OITM on (OITM.ItemCode = IGN1.ItemCode)
where Convert(Date,Cast(OIGN.DocDate as DATE)) = Convert(Date,DATEADD(d, @i, @date)) and OITM.ItmsGrpCod = @ItemGroup)
Select @i as D,OpeningBalance = case
when @i > 1 then ((isnull(SUM(OINM.InQty),0)-isnull(SUM(OINM.OutQty),0)) + ((isnull(SUM(OINM.InQty),0)-isnull(SUM(OINM.OutQty),0))))
when @i = 1 then (isnull(SUM(OINM.InQty),0)-isnull(SUM(OINM.OutQty),0))
end
,
isnull(@ManufactureQty,0) as Manufacture
From OITM
Inner Join OINM On (OINM.ItemCode = OITM.ItemCode and Convert(Date,Cast(OINM.DocDate as DATE)) = Convert(Date,DATEADD(d, @i, @date))) --and Convert(Date,Cast(OINM.DocDate as DATE)) > Convert(Date,'2014-05-01'))
Where OITM.ItmsGrpCod = @ItemGroup group by OINM.DocDate
SET @i = @i + 1
End
It Gives
D OpeningBalance Manufacture
1 123 23
D OpeningBalance Manufacture
2 143 773
D OpeningBalance Manufacture
3 126 27
.
But I need it like
D OpeningBalance Manufacture
1 123 23
2 143 773
3 126 27
.
1 个解决方案