You didn't say which version of Firebird you use. In FB 3.0+, the
following query with a window function will do the job (the example is
for 'BB' and 64):
with CumulativeLoc as (
select ITEMNO, LOCNO, QTY,
sum(QTY) over (partition by ITEMNO order by LOCNO) as CUMQTY
from LocationTable
)
select
ITEMNO, LOCNO,
iif(64 > CUMQTY, QTY, QTY - (CUMQTY - 64)) as QTY
from CumulativeLoc
where
ITEMNO = 'BB'
and 64 >= CUMQTY - QTY
In Firebird < 3.0, you'd probably need a stored procedure, iterating
over the rows and counting how many items you've already taken.
regards
Tomasz