Forum: Ruby on Rails Sum

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.
74bd868b70df3590bf94cf7abe563ec4?d=identicon&s=25 Jason (Guest)
on 2007-02-20 22:27
Is there any way to get a sum of a column without doing a seperate

I have the following that works, but I wonder if it can be done without
performing the query twice:

@sales_orders = SalesOrder.find(:all, :conditions => ['month(ship_date)
= ? and     year(ship_date) = ?',@month,@year])

@total = SalesOrder.find(:all, :conditions => ['month(ship_date) = ? and
year(ship_date) = ?',@month,@year]).sum(&:amount)

Is there any way I can use something like @sales_orders.sum('amount') or
something like that?


52cb4115a870ff1942e7e1ae96f4306f?d=identicon&s=25 Tyler MacDonald (Guest)
on 2007-02-20 22:31
(Received via mailing list)
Jason <> wrote:
> Is there any way I can use something like @sales_orders.sum('amount') or
> something like that?

If I am reading this right, yes, you should just be able to do:

@sales_orders = SalesOrder.find(:all, :conditions => ['month(ship_date)
= ? and     year(ship_date) = ?',@month,@year])
@total = @sales_orders.sum(&:amount)

  - Tyler
74bd868b70df3590bf94cf7abe563ec4?d=identicon&s=25 Jason (Guest)
on 2007-02-20 22:33
That worked!

Thank you so much!
20b1836f8acccd107b4992a1a45a14bd?d=identicon&s=25 chrisfarms (Guest)
on 2007-02-20 23:00
(Received via mailing list)
Hi ... I think it can be done in one step with:

SalesOrder.sum('amount', :conditions => ['month(ship_date) = ? and
year(ship_date) = ?',@month,@year])

nice if you can let SQL do the math(s)
F3dc06f587d1ff4c7366b102bfda9204?d=identicon&s=25 David Mitchell (Guest)
on 2007-02-21 02:30
(Received via mailing list)
I'd normally let the database do the work, and create the appropriate
SQL statement within the controller.  Off the top of my head, within
your SalesOrder controller:

def ship_date_sum(@month, @year)
  sum = SalesOrder.find_by_sql("SELECT SUM(amount) FROM SalesOrders
WHERE month(ship_date) = ? AND year(ship_date) = ?", @month, @year)
  return sum

Then just call it as SalesOrder.ship_date_sum(@month,@year) from
wherever you want to use it.

(Yep, I'm almost certain that isn't 100% correct, but I'm typing this
while I'm on hold for a phone call and hopefully you get the gist of

Doing the SELECT SUM(amount) ... in SQL will force the database to do
the work rather than your app, and will return a minimal amount of
data to your app.  It makes it easier to optimise your database later
(e.g. work out where to put indexes, in this case), gives you easier
scalability, consumes less memory on your Rails server, ...  If you're
really pushed for performance, you could even create the SELECT SUM...
as a stored proc on your database server and get the result back

I'm all for getting the database to do as much work as possible, and
making sure my Rails code has as small a footprint as possible.  Other
people prefer the Rails app to do all the work - Google for the pros
and cons of each approach if you're interested.



Dave M.
This topic is locked and can not be replied to.