【发布时间】:2017-05-08 09:24:49
【问题描述】:
我在 PostGreSQL 中有困难组。下面是我的代码
Invoice.select("customer_id, due_date, sum(balance) AS total_balance, total_mount").group(:customer_id)
我有错误
Invoice Load (1.8ms) SELECT customer_id, due_date, sum(balance) AS total_balance, total_amount FROM "records" WHERE "records"."type" IN ('Invoice') GROUP BY "records"."customer_id"
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "records.due_date" must appear in the GROUP BY clause or be used in an aggregate function
enter code here
LINE 1: SELECT customer_id, due_date, sum(balance) AS total_balance,...
更新
感谢@slicedpan 的解决方案。我有更新如下代码。
Invoice.select("customer_id, MAX(due_date), SUM(balance) AS total_balance").group(:customer_id)
我已经阅读了很多关于此的文章,但它仍然显示错误。
【问题讨论】:
标签: ruby-on-rails postgresql activerecord