# activeRecord Sum

**URL:** <https://www.ruby-forum.com/t/activerecord-sum/88241>\
**Category:** Rails\
**Created:** [March 9, 2007, 3:07am UTC](https://www.ruby-forum.com/t/activerecord-sum/88241 "2007-03-09T03:07:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 3:07am UTC](https://www.ruby-forum.com/t/activerecord-sum/88241/1 "2007-03-09T03:07:07Z")

</div>

Hi All,

I’m trying to get the sum of 3 database fields.

Instead of calling find, I’m using sum like this:

```
@itemsum = Order.sum(:item_total)
@shipsum = Order.sum(:shipping_total)
@taxsum = Order.sum(:tax_total)

```

But, would rather not make 3 database calls to do so.  
I can’t find any documentation on sum as a finder, so was wondering if  
anyone has a solution for getting the sum of multiple collumns this way.

The end sql result I’m trying to achieve is:

SELECT sum(tax\_total) AS sum\_tax\_total, sum(item\_total) AS  
sum\_item\_total, sum(shipping\_total) AS sum\_shipping\_total FROM orders

Of course I could just do a find\_by\_sql, but was just trying something  
new.

Thanks!

Dave

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 4:12am UTC](https://www.ruby-forum.com/t/activerecord-sum/88241/2 "2007-03-09T04:12:57Z")

</div>

afaik, there’s no special finder for collecting together three sums in  
a single query. You’ll need to get your hands mildly dirty with SQL.  
Javier points out easiest solution with one tweak – drop the colons:

sums = Order.find(:all, :select=\>‘sum(item\_total) AS item,  
sum(shipping\_total) AS ship, sum(tax\_total) AS tax’).first  
@itemsum = sums[:item]  
@shipsum = sums[:ship]  
@taxsum = sums[:tax]

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 3:24am UTC](https://www.ruby-forum.com/t/activerecord-sum/88241/3 "2007-03-09T03:24:50Z")

</div>

> I’m trying to get the sum of 3 database fields.
> 
> Instead of calling find, I’m using sum like this:
> 
> ```
> @itemsum = Order.sum(:item_total)
> @shipsum = Order.sum(:shipping_total)
> @taxsum = Order.sum(:tax_total)
> 
> ```
> 
> But, would rather not make 3 database calls to do so.

maybe you can try

Order.find(:all,:select=\>‘sum(:item\_total),sum(:shipping\_total),sum(:tax\_total)’)

in case you need it you can also add ,group=\>:my\_grouping\_field

regards,

javier ramirez

* * *

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 9:00am UTC](https://www.ruby-forum.com/t/activerecord-sum/88241/4 "2007-03-09T09:00:15Z")

</div>

> afaik, there’s no special finder for collecting together three sums in  
> a single query. You’ll need to get your hands mildly dirty with SQL.  
> Javier points out easiest solution with one tweak – drop the colons:

right… lazy me… i c/pasted the fields into the sentence and i didn’t  
realize i was taking the colons with me 😉

regards

javier

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/activerecord-sum/88241/5 "2007-03-09T16:04:22Z")

</div>

Eden Li wrote:

> afaik, there’s no special finder for collecting together three sums in  
> a single query. You’ll need to get your hands mildly dirty with SQL.  
> Javier points out easiest solution with one tweak – drop the colons:
> 
> sums = Order.find(:all, :select=\>‘sum(item\_total) AS item,  
> sum(shipping\_total) AS ship, sum(tax\_total) AS tax’).first  
> @itemsum = sums[:item]  
> @shipsum = sums[:ship]  
> @taxsum = sums[:tax]

So, this approach gives me an inaccurate number:

* * *

sums = Order.find(:all, :select=\>‘sum(item\_total) AS item,  
sum(shipping\_total) AS ship, sum(tax\_total) AS tax’).first

@were\_rich = sums[:item].to\_i + sums[:ship].to\_i + sums[:tax].to\_i

## In this case, @were\_rich = 197,018.00 (rounded because it’s a Float)

But, if I get the sum this way it’s accurate:

@sums = Order.find(:all)  
@were\_rich = @sums.sum(&:item\_total) + @sums.sum(&:shipping\_total) +  
@sums.sum(&:tax\_total)

Here, @were\_rich = 197,018.77

* * *

So the first approach, I lost my 77 cents.

I guess the difference in the 2 approaches is that approach 1 lets mySQL  
do the math were approach 2 lets Ruby do the math.

Thanks for your help!

Dave

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/activerecord-sum/88241/6 "2007-03-09T16:49:42Z")

</div>

> So the first approach, I lost my 77 cents.
> 
> I guess the difference in the 2 approaches is that approach 1 lets mySQL  
> do the math were approach 2 lets Ruby do the math.

I think you’re losing the 77 cents because of this line:

> @were\_rich = sums[:item].to\_i + sums[:ship].to\_i + sums[:tax].to\_i

Change those .to\_i’s to .to\_f’s.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/activerecord-sum/88241/7 "2007-03-09T17:05:44Z")

</div>

David C. wrote:

> @were\_rich = sums[:item].to\_i + sums[:ship].to\_i + sums[:tax].to\_i
> 
> In this case, @were\_rich = 197,018.00 (rounded because it’s a Float)

Uhh… #to\_i means “convert to integer”. You probably meant to use  
#to\_f?

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/activerecord-sum/88241/8 "2007-03-09T17:59:08Z")

</div>

Eden Li wrote:

> David C. wrote:
> 
> > @were\_rich = sums[:item].to\_i + sums[:ship].to\_i + sums[:tax].to\_i
> > 
> > In this case, @were\_rich = 197,018.00 (rounded because it’s a Float)
> 
> Uhh… #to\_i means “convert to integer”. You probably meant to use  
> #to\_f?

works much better that way. 🙂  
Thanks!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 9:32pm UTC](https://www.ruby-forum.com/t/activerecord-sum/88241/9 "2007-03-09T21:32:42Z")

</div>

Just so you know you could try something like this also

find\_by\_sql(“select (item\_total + shipping\_total + tax\_total) as  
grandtotal from tablename group by id”)

this method is more efficient

David C. wrote:

> Eden Li wrote:
> 
> > David C. wrote:
> > 
> > > @were\_rich = sums[:item].to\_i + sums[:ship].to\_i + sums[:tax].to\_i
> > > 
> > > In this case, @were\_rich = 197,018.00 (rounded because it’s a Float)
> > 
> > Uhh… #to\_i means “convert to integer”. You probably meant to use  
> > #to\_f?
> 
> works much better that way. 🙂  
> Thanks!
