How to do select with sum?

Is there a way to rewrite the following Model method avoiding

class LineOrder < ActivateRecord::Base
belongs_to :auction
belongs_to :operation

def self.amounts_by_year(search)
  search_company = ""
  if search
     search_company = "inner join auctions on where company LIKE "
       + "'%#{search}%'"
  connection.select_all("select year(date) as YD,
    sum(if(operation_id != 2, gross_price, NULL)) as BGP,
    sum(if(operation_id != 2, costs, NULL)) as BC,
    sum(if(operation_id != 2, net_price, NULL)) as BNP,
    sum(if(operation_id = 2, gross_price, NULL)) as SGP,
    sum(if(operation_id = 2, costs, NULL)) as SC,
    sum(if(operation_id = 2, net_price, NULL)) as SNP
    from line_orders " + "#{search_company} " + "group by year(date)")

To fix the idea:

The method is used in the index view to show at user's will the
amounts by year either of a given auction or of all auctions,
depending if the user has chosen to restrict the list to a particular
auction or not.

search is then the variable which is passed from the view to the model
to get the scope on which the method will be applied.

company is a field of the auction Model.

gross_price, costs, net_price are entered unsigned for user's
convenience. Hence they need to be separated afterwards depending on
the operation_id.

I am interested in this also.
Sorry don’t have any ideas as I am to new to RoR myself