jeudi 2 mai 2019

Sql - Correct Sum of field of base table with left outer join

I have this method which returns the final result for me to show, I have amount field in order table After this query I want that sum of amount field but at the same time I can't change get_raw_records because left outer join & filters is required

def get_raw_records
  Order.includes(:customers, :line_items).where(aasm_state: ['completed']).filers(.....)
end

get_raw_records.sum(:amount)

This sum is not correct as it has duplicate orders because of left outer join with line items

I am trying some other ways like but it distinct the amount not order records

get_raw_records.distinct.sum(:amount)

Any help to do this?, I can't change get_raw_records base query

Aucun commentaire:

Enregistrer un commentaire