嗨,我有一个有两列的表,例如,表名是演示,列是id和百分比。
id percentage
1 50
2 74
1 66
现在,我想按id分组,然后取百分比的和,然后根据某些条件选择记录,例如百分比之和小于40。
我有下面的sql查询,它给出了正确的结果。
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;
我需要这个查询在rails 3中。
Demo.group(‘id’).sum(:百分比)
在这之后我该怎么做?
谢谢。
发布于 2015-12-09 12:07:23
嘿,你可以用having
子句作为
Demo.group('id').having("sum(percentage) < 100")
https://stackoverflow.com/questions/34178196
复制相似问题