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

4
  • 2
    Possible duplicate of Cumulative (running) sum with django orm and postgresql Commented May 30, 2019 at 11:01
  • does not work with sqlite Commented May 30, 2019 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. Commented May 30, 2019 at 12:07
  • You can try to use @Shawn answer in pair with Model.objects.raw("some_sql") Commented May 30, 2019 at 12:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.