Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I want help with the below

Background
There are two tables, Withdrawal and Deposit. Each store customerID, amount and date.

What is the best way to calculate customer balance
Option 1.
Add another table called customer_balance with fields customerID,balance and update balance field on every transaction.

sql triggers can be used

Option 2.
balance = sum of customer Deposits - sum of withdrawals

sql stored procudure or view can be used


I also want to know how banking and inventory management systems calculate balances

What I have tried:

balance = sum of customer Deposits - sum of withdrawals
Posted
Updated 18-May-18 7:30am

A system (in Financial Services) that I have worked on had an overnight batch job that would calculate all balances at that time based on the sum of transactions since the last time the process was run (usually daily but not always) and the balance at that time - the "opening balance". Updates to the system were turned off whilst these calculations were on-going.

The point was to find a balance (if you will excuse the pun) between the overhead of doing calculations from day 0 against the storage of data and updating it.

The key is that you must have some means of determining the "last time the balance was updated" - yes you could have a separate table with it's own ID but personally I would just store the "balance at date" and "last balance date" on the accounts table (in our system each customer could have many accounts).

I don't agree with
Quote:
There are two tables, Withdrawal and Deposit. Each store customerID, amount and date.
To me it makes more sense to have a "Transaction" table with CustomerID, Amount (which can be negative for Withdrawals) and Transaction Date. Transactions in the banking arena would also have a reference field (free format text) and a unique identifier.
 
Share this answer
 
Generally use the sum method first, until the data gets too large and indexes are not fast enough, then create a "cache" value for the balance and endure the cost of keeping that cache "in-sync" with the data input.
 
Share this answer
 
Comments
wizy@2020 18-May-18 4:23am    
Thanks for your submission
Your already off on the wrong foot:

"Withdrawls and Deposits" are simply "transactions" (credits and debits) that should have been implemented as "ONE" table (and not 2).

The transactions are posted to "accounts".

Accounts can be monetary (i.e. $) or "statistical" (i.e. unit counts); and are maintained in a "general ledger".

Detailed inventories, etc. are maintained in "sub-ledgers" with specific application data requirements; and relate details to the "general ledger" summary accounts.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900