Best way in MySQL or Rails to get AVG per day within a specific date range

前端 未结 4 579
谎友^
谎友^ 2021-02-06 14:41

I\'m trying to make a graph in Rails, for example the avg sales amount per day for each day in a given date range

Say I have a products_sold model which has a \"sales_pr

4条回答
  •  旧巷少年郎
    2021-02-06 15:07

    To dry up a bit:

    ave_prices = Merchant.find(1).products.average(:sales_price, :group => "DATE(created_at)")
    date_aves = (from_date..to_date).map{|dt| [dt, ave_prices[dt.strftime "%Y-%m-%d"] || 0]}
    

提交回复
热议问题