please help me with a problem.
i need to have an script that will calculate FIFO the balance of a card. The card can have multiple transaction, each transaction will add or remove an certain amount of money from the card, and i need to mach the transaction id's depending if the removing transaction is consuming the entire add transaction or not. Here is an example:
The initial table:
and the result must be something like:
To explain better:
for card with ID = 1
- transaction with ID = 3 remove from transaction with ID = 1 the amount and remains 25 (100-75=25) and the TranIdADD is updated with 1
- transaction with ID = 4 remove from transaction with ID = 1 all amount and remains 0 and also from transaction with ID = 2 the rest of 25. On the transaction with ID = 1 remains 0 and on the transaction with ID = 2 remains 25. For the transaction with Id = 4, the TranIdAdd is updated with 2 (because that transaction was covering the entire amount). For the transaction with the ID = 1, the TranIdSUB is updated with 4 (the transaction id that finished the entire amount). The transaction with ID = 2 remains with TranIdSub = null/nothing because it has amount, and the amount (25) represents the amount of the card