Query to calculate balance