I like @Rubens answer, but ...
Your data structure may use some work.
Actually saving the word โcreditโ or โdebitโ for each transaction, not to mention the need to evaluate it with a case-case in order to turn it into a useful value (1 or -1), is expensive.
If your transaction table tracks the quantity, then you can use this instead of storing and evaluating "credit" and "debit." Positive quantities (for example, I bought 2 rakes, etc.); and Netagive amounts (for example, -1 rake, which means that you returned or received a loan for one of the rakes) can be summed up without making any case-statement evaluations.
source share