Welcome to the forums.

Ever use SQL?

I suspect a function in SQL called AVG (Average) would do the trick for you.

http://www.w3schools.com/SQL/sql_func_avg.asp