Reputation: 6127
Given a date how can you sum the total of the week that date falls in and each of the 10 weeks previous?
For each day in the last 30 this can be accomplished by:
import datetime as dt
from django.db.models import Sum
last_30 = dt.date.today() - dt.timedelta(days=30)
order_data = Order.objects.filter(
date__gt=last_30).extra(
select={'day': 'date(date)'}.values('day').annotate(
total=Sum('total')
)
print(order_data)
<OrderQuerySet [{'day': datetime.date(2018, 8, 28), 'total': Decimal('50000.00'),
{'day': datetime.date(2018, 8, 29), 'total': Decimal('84000.00'),
'...(remaining elements truncated)...']>
I guess they could also somehow be grouped into weeks after the fact by looping through order_data
but I was wondering if there's another way.
Using postgresql if that makes a difference.
Upvotes: 1
Views: 462
Reputation: 477666
You can use Django's ExtractWeek
[Django-doc] to obtain the week number of a date. So we can first annotate the queryset, and then pass the correct values, like:
from django.db.models.functions import ExtractWeek
qs = Order.objects.filter(date__gt=last_70).annotate(
week=ExtractWeek('date')
).values('week').annotate(
week_total=Sum('total')
).order_by('week')
This will produce something like:
<QuerySet [
{ 'week': 14, 'week_total': 1425.0 }
{ 'week': 15, 'week_total': 1302.0 }
{ 'week': 17, 'week_total': 1993.0 }
]>
Note that
.order_by
is important, since otherwise the values will not "fold";Upvotes: 2