Forum: Ruby on Rails Grouping by multiple items

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
bjhess (Guest)
on 2007-04-03 18:53
(Received via mailing list)
I'm wondering if Rails fully supports grouping by multiple items?  I
have a situation where I'd like to get total amounts grouped by year,
then month.  Ideally, the result set would have the sum for the year
and for each month right there, although I imagine having the sum for
the year is probably a lot to ask.

In any case, I'm using MySQL.  This is the code I would expect to

    @totals = Purchase.sum(:amount,
                                    :group => "YEAR(updated_at),
                                    :order => "updated_at DESC" )

The grouping appears to happen correctly.  However, the result set is
restricted to months:

[["4", 37383437], ["3", 37506978], ["2", 84938573], ["1", 48391034],
["12", 48593943], ... ]

I could make some assumptions, counting back the years in my reporting
view.  But I'd rather have all the actual data in one fell swoop.

Any help?
This topic is locked and can not be replied to.