Declare @YourTable table (ID int,QTY int,DATE varchar(25), CURRENT_STOCK int)
Insert Into @YourTable values
(1 ,1 ,'Jan' ,30),
(2 ,1 ,'Feb' ,30),
(3 ,2 ,'Mar' ,30),
(4 ,6 ,'Apr' ,30),
(5 ,8 ,'May' ,30),
(6 ,21 ,'Jun' ,30)
Select A.*
From @YourTable A
Where ID>= (
Select LastID=max(ID)
From @YourTable A
Cross Apply (Select RT = sum(Qty) from @YourTable where ID>=A.ID) B
Where B.RT>=CURRENT_STOCK
)
Връщане
ID QTY DATE CURRENT_STOCK
4 6 Apr 30
5 8 May 30
6 21 Jun 30