1👍
As I said in my comment, what you did is working in my case. However, I wouldn’t recommend evaluating the query with the for
loop since this will load your queryset to memory (it actually has the same effect as using list
). Instead, I’d recommend adding an extra row to your query that would contain the running sum.
To do do that you should use the extra
queryset method to add the extra field to your queryset. To find out how to get the running total with SQL you can check the answer to this question: How to get running sum of a column in sql server
Also, since you mention that your queryset is large you should add pagination to your table — and you will not benefit from pagination if you evaluate your queryset with the for-loop
. Feel free to ask again if you have trouble implementing the extra()
method.
Update: To answer OP’s comment (I am not sure about the names of your tables & fields but I’ll make a guess:
SELECT amount, (
select sum(amount) FROM ImportFileEntry ife1 where ife1.date < ife.date
) + (
select sum(amount) FROM ImportFileEntry ife2 where ife2.date = ife.date and ife2.seq < ife.seq
) as running_total FROM ImportFileEntry ife order by ife.date, ife.seq
One (complex) query — the as running_total
would be what the extra()
queryset method would create 🙂