views:

908

answers:

3

i have budgets table with emptype_id and calendar_id actual_head, estimated_head

when i do Budgets.sum(:actual_head ,:group=>"emptype_id,calendar_id") i do not get the result grouped by the above two columns but only by the emptype_id

however when i check the log the sql query is right

"SELECT sum(budgets.actual_head) AS sum_actual_head, emptype_id,calendar_id AS emptype_id_calendar_id FROM budgets GROUP BY emptype_id,calendar_id"

has 103 rows

i wanted to iterate through each emptype_id and calendar_id to get a sum of actual_head and do some calculations on it can someone please help

Thx

A: 

I'm not sure of this, buy try :group => [:emptype_id, :calendar_id]

Matt Grande
hi, that does not work throws the error below; hmm is there no way to group by two columns "ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'emptype_idcalendar_id' in 'field list': SELECT sum(`budgets`.actual_head) AS sum_actual_head, emptype_idcalendar_id AS emptype_idcalendar_id FROM `budgets` GROUP BY emptype_idcalendar_id"
philipth
+2  A: 

Grouping with multiple columns cannot be supported by rails. You have to use a regular find all:

budgets = Budgets.find(:all, :select => "emptype_id, calendar_id, sum(budgets.actual_head) AS sum_actual_head", :group => "emptype_id, calendar_id")

budgets.each { |budget| puts budget.sum_actual_head }

Lawrence Pit
A: 

I cheat. Do :group => ["emptype_id,calendar_id"].

Not want you nor I want, but this works at least.

Ben