Grouping by multiple items


#1

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
work:

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

MONTH(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?