0

In my database I'm storing data as below:

id    amt
--  ------- 
1    100 
2    -50  
3    100 
4   -100 
5    200 

I want to get output like below

id   amt  balance
-- ----- -------
1    100   100
2    -50    50
3    100   150
4   -100    50
5    200   250

How to do with in django orm

Russell
  • 1,624
  • 5
  • 23
  • 43
  • 2
    Possible duplicate of [Cumulative (running) sum with django orm and postgresql](https://stackoverflow.com/questions/43517901/cumulative-running-sum-with-django-orm-and-postgresql) – Ivan Starostin May 30 '19 at 11:01
  • does not work with sqlite – Russell May 30 '19 at 11:07
  • In plain sql, you'd use `sum()` as a window function: `SELECT id, amt, sum(amt) OVER (ORDER BY id) AS balance FROM yourtable` (At least in modern versions of sqlite). Dunno about with an ORM, though. – Shawn May 30 '19 at 12:07
  • You can try to use @Shawn answer in pair with `Model.objects.raw("some_sql")` – Nikita Tonkoskur May 30 '19 at 12:25

0 Answers0