I am new in rails,
I Have a Profile table and profile history table. Profile has many profile history. Profile table has id and current_count column and profile history has profile_id, and count column
i want to calculate all the profiles which have current_count greater than the average of the count value of the respective profile history
How can i achieve this in rails 3.
The appropriate sql query is
SELECT p.id, MIN(current_count), AVG(ph.count)
FROM profiles p
INNER JOIN profile_histories ph on p.id=ph.profile_id
GROUP BY ph.profile_id, p.id
HAVING MIN(current_count) > AVG(ph.count)
Lets construct the rails way:
Profile.joins(:profile_histories)
.select('profiles.id, MIN(profiles.current_count), AVG(profile_histories.count)')
.group('profile_histories.profile_id, profiles.id')
.having('MIN(profiles.current_count) > AVG(profile_histories.count)')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.