mercredi 6 mai 2015

SQLite Efficient Running Total

I have a table of transactions in SQLite

number date Category Amount runningBalance

I want the running balance column to have a running sum of the amount column after the table is sorted by Date first and number second.

I can do this with a select when reading. But this table has the potential to get very large and I don't want to recalculate every time. I want to make a trigger where all the transactions following (by date then number) the inserted/edited transaction have their runningBalance value updated.

This will mean that the calculations are reduced... as more recent transactions are likely to be edited more often, and older ones rarely. It also will spread the computation over writes so that reads are near instant.

Can anyone provide assistance on how to set up such a trigger?

so far this is what I have but it does not give desired results. And recalculates all every time. Not just the ones following the change.

UPDATE Transactions SET RunningBalance = (SELECT sum(Transactions.Amount) FROM Transactions WHERE Date <= Transactions.Date);

Thanks!

Aucun commentaire:

Enregistrer un commentaire