Method: WeightedAverage::ArelSelectManagerInstanceMethods#weighted_average

Defined in:
lib/weighted_average/arel_select_manager_instance_methods.rb

#weighted_average(data_column_names, options = {}) ⇒ Float?

Calculate the weighted average of column(s).

Examples:

Weighted average of load factor in flight stage data

Arel::Table.new(:flight_segments).weighted_average(:load_factor, :weighted_by => :passengers)

Parameters:

  • data_column_names (Symbol, Array<Symbol>)

    One or more column names whose average should be calculated. Added together before being multiplied by the weighting if more than one.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :weighted_by (Symbol)

    The name of the weighting column if it’s not :weighting (the default)

  • :disaggregate_by (Symbol)

    The name of a column to disaggregate by. Usually not necessary.

Returns:

  • (Float, nil)

See Also:



17
18
19
20
# File 'lib/weighted_average/arel_select_manager_instance_methods.rb', line 17

def weighted_average(data_column_names, options = {})
  weighted_average = @engine.connection.select_value(weighted_average_relation(data_column_names, options).to_sql)
  weighted_average.nil? ? nil : weighted_average.to_f
end