Reputation: 2504
quick question because I'm getting crazy on this issue. I have a table where I save a lot of analytics data, and I have to put this data on a table.
Every record has this structure:
id: 1,
user_id: 4,
store_id: 490,
company_id: 1,
completed_courses_percentage: 87
At this moment I'm grouping the user by company and then I use the average method to create a table with the average completed course percentage grouped by company.
UserActivity.group(:company_id).average(:completed_courses_percentage)
If I have three person for a company and the averages are 0, 50 and 100 the total average now is 50%.
By the way I need to change the way I calculate this average: I must calculate the average of people that belong to a company with a completed_courses_percentage > 70.
I try to use
UserActivity.group(:company_id).having("completed_courses_percentage > 70")
but the result is 0.
Upvotes: 0
Views: 60
Reputation: 1403
You can try this.
UserActivity.where("completed_courses_percentage > 70").group(:company_id).average(:completed_courses_percentage)
P.S: Not tried this.
Upvotes: 4