Hello Frnds,Currently the below code is working but not works for Rest qty column, because i think the when inserting into temp table is inserting data in not grouping manner.so when looping case is not porperly, and rest qty col brings wrong data.IF OBJECT_ID('spFluctuationPaidBill') IS NOT NULL DROP PROC spFluctuationPaidBillGOCREATE PROCEDURE spFluctuationPaidBill @FromDate DateTime, @ToDate DateTimeASBEGINSET NOCOUNT ON;CREATE TABLE #FluctuationTable(ID INT IDENTITY(1,1),ItemCode nvarchar(100),ItemDesc nvarchar(100),OnHandQty int,GRPONo int,GRPODate datetime,ICRate decimal(18,12),GRPOQty int,RestQty int,RatePerQty decimal(18,12),APInvNo int,APInvDate datetime,OGRate decimal(18,12))INSERT INTO #FluctuationTableSELECT OITM.ItemCode,OITM.ItemName,sum(OITW.onhand) as onhand,abc.basenum as GRPONo, abc.docdate as GRPODate,(select top 1 docrate from opdn where docentry = abc.baseentry and docnum = abc.basenum) as GRPORate,(select sum(quantity) from pdn1 where docentry = abc.baseentry and abc.itemcode = itemcode) As GRPOQty,null,(select top 1 price from pdn1 where docentry = abc.baseentry and abc.itemcode = itemcode) As GRPOUnitPrice, (select distinct top 1 opch.docnum from opch inner join pch1 on opch.docentry = pch1.docentry where pch1.baseentry = abc.baseentry) as APInvNo,(select distinct top 1 opch.docdate from opch inner join pch1 on opch.docentry = pch1.docentry where pch1.baseentry = abc.baseentry) as APInvDt,(select distinct top 1 opch.u_dodamt from opch inner join pch1 on opch.docentry = pch1.docentrywhere pch1.baseentry = abc.baseentry) as APInvDocRateFROM OITW INNER JOIN OITM ON OITW.ItemCode = OITM.ItemCode INNER JOIN OITB ON OITM.ItmsGrpCod = OITB.ItmsGrpCod left join sri1 as abc on OITW.ItemCode = abc.ItemCodeWHERE(OITW.OnHand <> 0 Or oitw.iscommited <> 0 Or oitw.onorder <> 0 Or oitw.minstock <> 0)AND (OITW.ItemCode IN (SELECT pdn1.ItemCode FROM Opdn inner join pdn1 on opdn.docentry = pdn1.docentry where opdn.series = 314 or opdn.series = 94 or opdn.series = 663))and (abc.Direction = 0) AND (abc.LineNum =(SELECT MAX(LineNum) AS Expr1 FROM SRI1WHERE (ItemCode = abc.ItemCode) AND (SysSerial = abc.SysSerial))) AND basetype = 20and abc.docdate >= (@FromDate) and abc.docdate <= (@ToDate)group by OITM.ItemCode,OITM.ItemName, oitm.suppcatnum, abc.basenum, abc.sysserial, abc.baseentry, abc.basenum, abc.docdate, oitm.ItmsGrpcod, oitb.ItmsGrpNam, abc.baseentry, abc.SysSerial, abc.ItemCode,oitw.itemcodeorder by OITM.ItemName,abc.basenumDECLARE @ID INTDECLARE @COUNT INTDECLARE @ITEM VARCHAR(20)DECLARE @GRPO INTDECLARE @TOTAL INTDECLARE @SUM INTSELECT @COUNT = COUNT(*) FROM #FluctuationTableSET @ID =1select @COUNTWHILE (@ID <= @COUNT)BEGIN SELECT @ITEM=ItemDesc, @GRPO=GRPOQty,@TOTAL=OnHandQty FROM #FluctuationTable WHERE ID=@ID IF (@TOTAL > @GRPO) UPDATE #FluctuationTable SET RestQty = @GRPO WHERE ID=@ID ELSE BEGIN select SUM(GRPOQty) FROM #FluctuationTable WHERE ItemDesc=@ITEM AND OnHandQty=@TOTAL AND GRPOQty<>@GRPO AND ID > @IDselect SUM(GRPOQty) FROM #FluctuationTable where OnHandQty=28 and GRPOQty<>40 AND ID > @ID UPDATE #FluctuationTable SET RestQty = (OnHandQty-@SUM) WHERE ID=@ID END SET @ID = @ID + 1ENDselect itemcode,ItemDesc,OnHandQty,GRPONo,GRPOQty,RestQty from #FluctuationTablegroup by itemcode,ItemDesc,OnHandQty,GRPONo,GRPOQty,RestQty order by itemcode,GRPONoENDGOEXEC spFluctuationPaidBill '05/01/2009','12/01/2009'Based upon item wise i have displayed Total stock qty andthen item wise GRPO details with Qty data.I have created Stored procedure for this.Now i have kept one column RestQty which is basically used todisplay rest quantity based upon the data of Total Stock Qty and GRPO quantity.To Implement this logic i have used loop condition but its not returning correctqty on Rest Qty column.Rest Qty should work like this:-Item aaaaaIf Total Quantity = 28GRPO Quantity = 40(First GRPO Qty), 7(second last GRPO Qty), & 3 (Last GRPO Quantity)Now Total quantity always first check with Last GRPo Qty,it means 28 > 3 then 3 comes to Rest Qty col.then Total quantity check with second Last GRPo Qty,it means 28 > 7 + 3(Last GRPo Qty) then 7 comes to Rest qty col.then Total quantity first check with first GRPo Qty,it means 28 > 40(first grpo qty) + 7 + 3, its not satisfied,hence it should display 28 - (7+3) = 18 in rest qty col.if we sum Rest qty column for Item aaaa then it should equal to Total Qty for particular item.so its should work for Item aaaa, based upon this other items should work like this.so i only need to correct the data of Rest Qty col, other are ok.Please help me to correct this query.Regards,