In Rails / Ruby, how to group and aggregate a monthly measure based on a date column in a table.
I tried the following technique from Railscasts.
http://railscasts.com/episodes/29-group-by-month
So far my code looks like this
result = result.where(transaction_date: start..Date.current)
result = result.select(:transaction_date, :quantity)
result.group_by { |t| t.transaction_date.beginning_of_month }
I guess I need the Ruby equivalent of SQL GROUP BY to accumulate the amount.
The version of SQLite3 I'm looking for:
select strftime('%m', transaction_date)
, sum(quantity)
from transactions
group by strftime('%m', transaction_date)
source
share