I need to do a sum of a certain column grouped by date and month. In SQL (postgres), it would look something like this:
select sum(amount) from somewhere group by extract(year from date), extract(month from date)
Can this be expressed as a Django QuerySet? Seems to me like it can’t, but I don’t really want to resort to plain old SQL. Any other ideas are welcome.
Seems like it might be possible using queryset.query.group_by, but I haven’t had any luck with that – a working example would be welcome.
You can use the
extramethod to add in the year and date values before doing the aggregation.