Rails / Postgres:“必须出现在GROUP BY子句中或用于聚合函数中” [英] Rails / Postgres: “must appear in the GROUP BY clause or be used in an aggregate function”

查看:375
本文介绍了Rails / Postgres:“必须出现在GROUP BY子句中或用于聚合函数中”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这种方法:

  def self.lines_price_report(n)
Income.group(' date(today_today_n).sum(:lines_price)
end
$ b $我在Heroku中遇到这个错误:

pre pre $ p $ :错误:错误:列incomes.filled_at必须出现在GROUP BY子句
中或用于汇总函数

我该如何解决这个问题?
$ b

执行查询:

  SELECT SUM(incomes。lines_price)AS sum_lines_price,date(filled_at)
AS date_filled_at FROMincomes
HAVING(date(filled_at)>'2012-12-04')
GROUP BY日期(filled_at)ORDER BY filled_at ASC

预期结果 strong>

  [[2012-12-04,SUM_FOR_DATE],[2012-12-05,SUM_FOR_DATE] ,...] 


解决方案

你的错误是使用filled_at您可以使用unscoped修复它以消除默认范围:

<$ p $ ($ date(filled_at)')
.having(date(filled_at)>?,Date.today - n)
.sum(:lines_price)

  Income.unscoped 
.group('date(filled_at)')
.having(date(filled_at)>? ,Date.today - n)
.sum(:lines_price)
.order('date(filled_at)ASC')

但我认为最好的方法是使用where而不是

 收入.unscoped 
.where(date(filled_at)> TIMESTAMP?,Date.today - n)
.group('date(filled_at)')
.sum(:lines_price)
.order('date(filled_at)ASC')

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); 

迁移:

  add_index:incomes,:filled_at 

并且您在这个表格索引中会有很多数据用于过滤,所以查询应该快得多。



因此,只需编写两者并测试哪一个更快(如果没有,您必须在filled_at上创建索引)。


I'm using this method:

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

I'm getting this error in Heroku:

PG::Error: ERROR:  column "incomes.filled_at" must appear in the GROUP BY clause 
or be used in an aggregate function

How can I fix this? Thank you.

Executed query:

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

Expected result

[["2012-12-04", SUM_FOR_DATE], ["2012-12-05", SUM_FOR_DATE], ...]

解决方案

Your mistake was to use filled_at in order by probably in default scope.

You can fix it using unscoped to eliminate default scopes:

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

or

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

but I think that better will be to use where instead of having

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

SQLFiddle

You have to be careful about using TIMESTAMP because 2012-12-04 will become 2012-12-04 00:00:00 so if you don't want this day in result use Date.today - (n - 1)

If you create index on filled_at column

 create index incomes_filled_at on incomes(filled_at);

migration:

 add_index :incomes, :filled_at

and you have a lot of data in this table index will be used in filtering. So query should be much faster.

So just write both and test which is faster (you have to create index on filled_at if you don't have one).

这篇关于Rails / Postgres:“必须出现在GROUP BY子句中或用于聚合函数中”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆