在rails控制台中运行的查询不能用作方法

时间:2016-08-04 01:17:12

标签: ruby-on-rails

我希望得到这样的加权平均值:

1    + %dummy(x,"j1=input(j,anydtdtm.); drop j; rename j1=j;k1=input(k,anydtdtm.); drop k; rename
k1=k;l1=input(l,anydtdtm.); drop l; rename l1=l;")
MPRINT(DUMMY):   data x2;
MPRINT(DUMMY):   set x;
MPRINT(DUMMY):   j1=input(j,anydtdtm.);
MPRINT(DUMMY):   drop j;
MPRINT(DUMMY):   rename j1=j;
MPRINT(DUMMY):  k1=input(k,anydtdtm.);
MPRINT(DUMMY):   drop k;
MPRINT(DUMMY):   rename k1=k;
MPRINT(DUMMY):  l1=input(l,anydtdtm.);
MPRINT(DUMMY):   drop l;
MPRINT(DUMMY):   rename l1=l;
MPRINT(DUMMY):   run;

工作正常。但是当我创建一个方法并调用它时,它最终会出现这样的错误:

c = Company.select('sum(pbr * market_cap) / sum(market_cap) as foo')
[
    [0] #<Company:0x007f8d97819620> {
        :id => nil
    }
]
c.first.foo
1.912815559952786983747597339216657074310395339

当我使用方法链时也会发生错误。

  Company Load (0.7ms)  SELECT  sum(pbr * market_cap) / sum(market_cap) as foo FROM "companies"  ORDER BY "companies"."id" ASC LIMIT 1
PG::GroupingError: ERROR:  column "companies.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...sum(market_cap) as foo FROM "companies"  ORDER BY "companies...
                                                             ^
: SELECT  sum(pbr * market_cap) / sum(market_cap) as foo FROM "companies"  ORDER BY "companies"."id" ASC LIMIT 1
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:  column "companies.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...sum(market_cap) as foo FROM "companies"  ORDER BY "companies...
                                                             ^
: SELECT  sum(pbr * market_cap) / sum(market_cap) as foo FROM "companies"  ORDER BY "companies"."id" ASC LIMIT 1
from /Users/ironsand/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-mini-profiler-0.9.6/lib/patches/db/pg.rb:90:in `exec'

为什么会发生此错误?以及如何解决它?

0 个答案:

没有答案