# Sum

**URL:** <https://www.ruby-forum.com/t/sum/86247>\
**Category:** Rails\
**Created:** [February 20, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/sum/86247 "2007-02-20T22:27:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [February 20, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/sum/86247/1 "2007-02-20T22:27:16Z")

</div>

Is there any way to get a sum of a column without doing a seperate  
query?

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?

Thanks,

Jason

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [February 20, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/sum/86247/2 "2007-02-20T22:31:08Z")

</div>

Jason [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [February 20, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/sum/86247/3 "2007-02-20T22:33:30Z")

</div>

That worked!

Thank you so much!

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [February 20, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/sum/86247/4 "2007-02-20T23:00:11Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [February 21, 2007, 2:30am UTC](https://www.ruby-forum.com/t/sum/86247/5 "2007-02-21T02:30:33Z")

</div>

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  
end

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  
it…)

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  
faster.

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.

YMMV

Regards

Dave M.
