Cumulative Sum per month

405 views
Skip to first unread message

Mario Bisiani

unread,
Mar 3, 2020, 3:14:01 PM3/3/20
to Django users
Hi!

I would like to know the best way to have the cumulative sum per month plottable to chartjs for example. My models.py has one field for date and one for decimal number. 

Thanks to you all!

krnrrr

unread,
Mar 5, 2020, 7:42:18 AM3/5/20
to Django users
what do you mean by "cumulative"? if we have aggregate sums like

jan = 100
feb = 150
mar = 120

you want to have output as

jan = 100
feb = 250
mar = 370

?

Mario Bisiani

unread,
Mar 11, 2020, 11:27:43 AM3/11/20
to Django users
Yes. I am using Chart.js. Any help?

Michael Macintosh

unread,
Mar 11, 2020, 11:32:23 AM3/11/20
to django...@googlegroups.com

You might want to use window functions

https://docs.djangoproject.com/en/3.0/ref/models/expressions/#window-functions

to calculate the sum per month.  Haven't personally used them yet though.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3bb00f84-7a7c-408c-9b17-8cdd51f7bb1f%40googlegroups.com.

Mario Bisiani

unread,
Mar 11, 2020, 11:42:57 AM3/11/20
to Django users
Thank you Micheal. This function is the OVER clause of sql. I think the running sum in a chart is a very common thing and I would like to know if someone has a snippet or piece of library to go "easily".

Thank you for your support.


Il giorno mercoledì 11 marzo 2020 16:32:23 UTC+1, Michael Macintosh ha scritto:

You might want to use window functions

https://docs.djangoproject.com/en/3.0/ref/models/expressions/#window-functions

to calculate the sum per month.  Haven't personally used them yet though.

On 3/11/2020 8:27 AM, Mario Bisiani wrote:
Yes. I am using Chart.js. Any help?

Il giorno giovedì 5 marzo 2020 13:42:18 UTC+1, krnrrr ha scritto:
what do you mean by "cumulative"? if we have aggregate sums like

jan = 100
feb = 150
mar = 120

you want to have output as

jan = 100
feb = 250
mar = 370

?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Mario Bisiani

unread,
Mar 14, 2020, 4:06:31 AM3/14/20
to Django users
Hi, I resoleved with :


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)
Reply all
Reply to author
Forward
0 new messages