5👍
✅
From Dima Kudosh’s answer and based on https://stackoverflow.com/a/5700744/2240489 I had to do the following:
I removed the reference to PARTITION BY
in the sql and replaced with ORDER BY
resulting in.
AModel.objects.annotate(
cumsum=Func(
Sum('a_number'),
template='%(expressions)s OVER (ORDER BY %(order_by)s)',
order_by="id"
)
).values('id', 'cumsum').order_by('id', 'cumsum')
This gives the following sql:
SELECT "amodel"."id",
SUM("amodel"."a_number")
OVER (ORDER BY id) AS "cumsum"
FROM "amodel"
GROUP BY "amodel"."id"
ORDER BY "amodel"."id" ASC, "cumsum" ASC
Dima Kudosh’s answer was not summing the results but the above does.
16👍
For reference, starting with Django 2.0 it is possible to use the Window
function to achieve this result:
AModel.objects.annotate(cumsum=Window(Sum('a_number'), order_by=F('id').asc()))\
.values('id', 'cumsum').order_by('id', 'cumsum')
- Celerybeat automatically disables periodic task
- Docker-compose to run django with mongodb
- Can't create django project using Windows command prompt
- Why does JSON returned from the django rest framework have forward slashes in the response?
2👍
For posterity, I found this to be a good solution for me. I didn’t need the result to be a QuerySet, so I could afford to do this, since I was just going to plot the data using D3.js:
import numpy as np
import datettime
today = datetime.datetime.date()
raw_data = MyModel.objects.filter('date'=today).values_list('a_number', flat=True)
cumsum = np.cumsum(raw_data)
1👍
You can try to do this with Func expression.
from django.db.models import Func, Sum
AModel.objects.annotate(cumsum=Func(Sum('a_number'), template='%(expressions)s OVER (PARTITION BY %(partition_by)s)', partition_by='id')).values('id', 'cumsum').order_by('id')
- Django serialize multiple objects in one call
- Heroku: No default language could be detected for this app for python even with runtime.txt
- Django JWT Authentication behavior different between local & mod_wsgi servers with Django REST framework
- Printing Receipt from Django Web Application
0👍
Check this
AModel.objects.order_by("id").extra(select={"cumsum":'SELECT SUM(m.a_number) FROM table_name m WHERE m.id <= table_name.id'}).values('id', 'cumsum')
where table_name
should be the name of table in database.
- Why am I getting the "MySQL server has gone away" exception in Django?
- Google Calendar Integration with Django
- How can I send e-mail from django using the google smtp server?
- Filter a Django form select element based on a previously selected element
Source:stackexchange.com