Rails/Postgres:"必须出现在GROUP BY子句中或用于聚合函数"

Ale*_*lex 4 ruby sql postgresql ruby-on-rails heroku

我正在使用这种方法:

  def self.lines_price_report(n)
    Income.group('date(filled_at)').having("date(filled_at) > ?", Date.today - n).sum(:lines_price)
  end
Run Code Online (Sandbox Code Playgroud)

我在Heroku中收到此错误:

PG::Error: ERROR:  column "incomes.filled_at" must appear in the GROUP BY clause 
or be used in an aggregate function
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?谢谢.

执行查询:

SELECT SUM("incomes"."lines_price") AS sum_lines_price, date(filled_at)
AS date_filled_at FROM "incomes"
HAVING (date(filled_at) > '2012-12-04')
GROUP BY date(filled_at) ORDER BY filled_at ASC
Run Code Online (Sandbox Code Playgroud)

预期结果

[["2012-12-04", SUM_FOR_DATE], ["2012-12-05", SUM_FOR_DATE], ...]
Run Code Online (Sandbox Code Playgroud)

suf*_*leR 6

你的错误是可能在默认范围内按顺序使用filled_at.

您可以使用unscoped修复它以消除默认范围:

Income.unscoped
 .group('date(filled_at)')
 .having("date(filled_at) > ?", Date.today - n)
 .sum(:lines_price)
Run Code Online (Sandbox Code Playgroud)

要么

Income.unscoped
   .group('date(filled_at)')
   .having("date(filled_at) > ?", Date.today - n)
   .sum(:lines_price)
   .order('date(filled_at) ASC')
Run Code Online (Sandbox Code Playgroud)

但我认为更好的方法是使用而不是拥有

Income.unscoped
  .where("date(filled_at) > TIMESTAMP ?", Date.today - n)
  .group('date(filled_at)')
  .sum(:lines_price)
  .order('date(filled_at) ASC')
Run Code Online (Sandbox Code Playgroud)

SQLFiddle

你必须小心使用TIMESTAMP,因为2012-12-04将成为2012-12-04 00:00:00所以如果你不希望这一天在结果中使用Date.today - (n - 1)

如果在filled_at列上创建索引

 create index incomes_filled_at on incomes(filled_at);
Run Code Online (Sandbox Code Playgroud)

移民:

 add_index :incomes, :filled_at
Run Code Online (Sandbox Code Playgroud)

并且你在这个表索引中有很多数据将用于过滤.所以查询应该快得多.

所以只需要写两个并测试哪个更快(如果你没有,你必须在filled_at上创建索引).