Điều này sẽ hoạt động
select transaction_id, customer_id, date, credit, debit,
abs(sum(ifnull(credit,0)) over (partition by customer_id order by date,credit,transaction_id ) - sum(ifnull(debit,0)) over(partition by customer_id order by date,debit,transaction_id)) as balance
from ledger
order by transaction_id;