On Wed, Sep 8, 2010 at 12:51 AM, Don <[email protected]> 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.

This is a known limitation of the current aggregates. It's something
I'd like to address, since "aggregate X's that occurred on Monday" is
a pretty common pattern.

This is logged as ticket #10302. In the meantime, the discussion on
the ticket contains an extra()-based workaround that can be used in
some circumstances.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to