I created a library to address this kind of problems : http://code.google.com/p/django-cube/
It bases the aggregation on a multidimensional view of your data (a cube), so basically you can group_by in any way you want ("any way you want" = "on all the dimensions that you want", so you have to declare a cube and its dimensions). Furthermore, if I understood your query, you want to group by <month/ year> (which is not possible in Django yet), and this is something I implemented in "django-cube". However, the fact that it is based on something that you have to declare (in a very similar fashion to declaring Django models), introduces a small overhead, which means that it might not be a good solution if you are not going to calculate more aggregations than this single one with it (it was originally meant for data visualization purposes). This is still beta, and the documentation is unclear, so if you want to use it, feel free to ask me any question. Yours, Sébastien On Sep 7, 6:51 pm, Don <don.laid...@gmail.com> wrote: > I need to create a query that aggregates values by the year of a date > field. I have not been able to discover a way to do this yet, perhaps > someone can help. I can issue the following raw query, which gives the > results I want: > > cursor.execute("""SELECT year(oac_date) as year, month(oac_date) as > month, sum(oac_actualconsumption) as consumption > FROM actualconsumption > WHERE oac_object = %s AND oac_commodity = %s AND > year(oac_date) = %s > GROUP BY year(oac_date), month(oac_date) > ORDER BY month(oac_date)""", [object_id, commodity, year]) > > Where oac_object is the pk for the table. > > This summarizes the oac_actualconsumption by year. Does anyone have an > idea how to do this without resorting to a raw query? I have not been > able to get the aggregates to work over parts of a date field in the > database, only on the whole field. > > Thanks! -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.