Here's the psuedo code to do it if it helps:
Code:
# run sql to get total_amount for that id
# if total_amount > sales_amount
# while sales_amount > 0
# pull oldest date for this id where amount > 0
# pull amount for this date
# if amount > sales_amount
# update amount = amount - sales_amount where this id and date
# sales_amount = 0
# else
# sales_amount = sales_amount - amount
# update amount = 0 where this id and date
# else
# error as not enough
Mike