Reputation: 635
Hi I have a table which has two columns , say for example the table name is demo and columns are id and percentage
id percentage
1 50
2 74
1 66
Now I would like to group by id and then take sum of the percentage and then select records based on some condition say sum of percentage less than 40.
I have the following sql query, which gives me correct results.
SELECT id ,(SELECT SUM("demo"."percentage")
AS sum_allocation_percentage FROM "demo" GROUP BY id)as
sum_allocation_percentage from demo where sum_allocation_percentage < 100;
I need this query to be in rails 3.
Demo.group('id').sum(:percentage)
How should I proceed after this?
Thanks.
Upvotes: 0
Views: 411
Reputation: 1940
Hey you can used having
clause as
Demo.group('id').having("sum(percentage) < 100")
Upvotes: 1