# Need to get average of specific fields in MySQL

**URL:** <https://www.ruby-forum.com/t/need-to-get-average-of-specific-fields-in-mysql/145970>\
**Category:** Ruby\
**Created:** [August 15, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/need-to-get-average-of-specific-fields-in-mysql/145970 "2008-08-15T17:37:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [August 15, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/need-to-get-average-of-specific-fields-in-mysql/145970/1 "2008-08-15T17:37:08Z")

</div>

Hi Forum,

My Active Record query works fine and pull all records where  
ipadd=‘10.1.1.1’ and print out the current\_connection\_count values but I  
need to get the average of those numbers.

## here is my Active Record config:

class Pix  
end

## device = Pix.find(:all, :conditions =\> “pixes.ipadd = ‘10.1.1.1’”) device.each do |line| puts line.current\_connection\_count end

Is it best to somehow change my sql query to get the average of these  
numbers or is it better to try do it after the sql query (which I don’t  
know how to do either :-)) ?

any help would be appreciated.  
thanks  
jackster

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [August 15, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/need-to-get-average-of-specific-fields-in-mysql/145970/2 "2008-08-15T17:45:23Z")

</div>

jackster the jackle wrote:

> device = Pix.find(:all, :conditions =\> “pixes.ipadd = ‘10.1.1.1’”)  
> device.each do |line|  
> puts line.current\_connection\_count  
> end

According to the very first cite at…

[activerecord average - Google Search](http://www.google.com/search?q=activerecord+average)

…it should be:

average = Pix.average(:current\_connection\_count,  
:conditions =\> “ipadd = ‘10.1.1.1’”)

Please consider [http://groups.google.com/group/rubyonrails-talk/](http://groups.google.com/group/rubyonrails-talk/) for RoR  
questions. This group is for old-timers who missed the RoR boat, and  
questions  
about it make us cranky here. (-:
