Tuesday, September 7, 2010

Aggregate on part of a date field?

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-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate