Skip to content Skip to sidebar Skip to footer

Difficulty With Activerecord Query That Does Percentage Calculation

I have an ActiveRecord schema, PurchaseSummary, that I inherited and THAT I CAN NOT CHANGE that looks like this: purchase_summaries date date_of_purchase integer number_of_

Solution 1:

Not th most optimized way, but you can do something like :

select date_of_purchase
      ,sum(number_of_purchases) / (select sum(subquery.number_of_purchases) 
                                     from purchase_summaries subquery 
                                    where subquery.date_of_purchase = query.date_of_purchase
                                      and subquery.coupon_used = 1) * 100 as percentage
  from purchase_summaries query
 group by date_of_purchase

Post a Comment for "Difficulty With Activerecord Query That Does Percentage Calculation"