Django: Sum on an date attribute grouped by month/year
Posted
by Sébastien Piquemal
on Stack Overflow
See other posts from Stack Overflow
or by Sébastien Piquemal
Published on 2010-05-31T11:53:26Z
Indexed on
2010/05/31
12:13 UTC
Read the original article
Hit count: 250
Hello, I'd like to put this query from SQL to Django:
"select date_format(date, '%Y-%m') as month, sum(quantity) as hours from hourentries group by date_format(date, '%Y-%m') order by date;"
The part that causes problem is to group by month when aggregating. I tried this (which seemed logical), but it didn't work :
HourEntries.objects.order_by("date").values("date__month").aggregate(Sum("quantity"))
© Stack Overflow or respective owner