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 ,'3月' ,30),(4 ,6 ,'4月' ,30),(5 ,8 ,'5月' ,30),(6 ,21 ,'6月' , 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_STOCK4 6 Apr 305 8 May 306 21 Jun 30
プレ>