Method: QuickBase::Client#average

Defined in:
lib/QuickBaseClient.rb

#average(dbid, fieldNames, query = nil, qid = nil, qname = nil, clist = nil, slist = nil, fmt = "structured", options = nil) ⇒ Object

Returns the average of the values for one or more fields in the records returned by a query. e.g. averagesHash = average(“dfdfafff”,[“Date Sent”,“Quantity”,“Part Name”])



4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
# File 'lib/QuickBaseClient.rb', line 4031

def average( dbid, fieldNames, query = nil, qid = nil, qname = nil, clist = nil, slist = nil, fmt = "structured", options = nil )
   count = {}
   fieldNames.each{|field| count[field]=0 }
   sum = {}
   iterateRecords(dbid,fieldNames,query,qid,qname,clist,slist,fmt,options){|record|
      fieldNames.each{|field|
         value = record[field]
         if value
            baseFieldType = lookupBaseFieldTypeByName(field)
            case baseFieldType
               when "int32","int64","bool" 
                  value = record[field].to_i
               when "float"   
                  value = record[field].to_f
            end
            if sum[field].nil?
               sum[field] = value
            else
               sum[field] = sum[field] + value
            end
            count[field] += 1
         end
      }
   }
   average = {}
   hasValues = false
   fieldNames.each{|field| 
      if sum[field] and count[field] > 0
         average[field] = (sum[field]/count[field]) 
         hasValues = true
      end
   }
   average = nil if not hasValues
   average
end