# Grouping by multiple items

**URL:** <https://www.ruby-forum.com/t/grouping-by-multiple-items/90987>\
**Category:** Rails\
**Created:** [April 3, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/grouping-by-multiple-items/90987 "2007-04-03T16:53:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjhess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@bjhess](https://www.ruby-forum.com/u/bjhess)\
**Post date:** [April 3, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/grouping-by-multiple-items/90987/1 "2007-04-03T16:53:29Z")

</div>

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?
