public giQueryStockID, giQueryStockSum ... m.lnCurrentStockBalance = ... Select StockID, InvNo, ItemNo, Qty, Cost, Date, Include(Cost, StockID, m.lnCurrentStockBalance) ; from LatestStockInDetails Where StockID=... ... procedure Include lparameters pnCost, piStockID, pnBalance if giQueryStockID <> piStockID giQueryStockID = piStockID giQueryStockSum = Cost else giQueryStockSum = giQueryStockSum + Cost endif return giQueryStockSum <= m.lnCurrentStockBalanceYou can use above function also for larger select statement when you select all stocks with joined detail items, but do not forget to force the order of join statements so stocks re selected first than items are joined to them, otherwice function will work incorrectly.
select L.StockID, L.InvNo, L.ItemNo, L.Qty, L.Cost, L.Date, ; (NVL(sum(L2.Cost),0) + L.Cost <= m.lnCurrentStockBalance) as Include ; from LatestStockInDetails L left join LatestStockInDetails L2 on L2.StockID=L.StockID AND L2.InvNo<L.InvNo Group By L.StockID, L.InvNo Where L.StockID=...HTH.
>Item A >Current Stock Balance : 50 > >Latest StockIn Details >InvNo ItemNo Qty Cost Date >------------------------------- >A01 ItemA 10 10 22/07/2001 - Include >A02 ItemA 10 20 21/07/2001 - Include >A03 ItemA 10 15 19/07/2001 - Include >A04 ItemA 10 10 18/07/2001 - Include >A05 ItemA 10 10 10/07/2001 - Include >A06 ItemA 10 10 08/07/2001 - Exclude since the total qty exceed 50 > >>How to use SQL to get the "included" stock in details for all of the items ??