我在PostGreSQL中遇到困難組。我下面PosgreSQL ActiveRecord :: StatementInvalid:PG :: GroupingError:錯誤:必須出現在GROUP BY
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,...
UPDATE
感謝@slicedpan的解決方案。我有像下面的代碼更新。
Invoice.select("customer_id, MAX(due_date), SUM(balance) AS total_balance").group(:customer_id)
我已經閱讀了很多關於這方面的文章,但它仍然顯示錯誤。謝謝
謝謝。我想先嚐試一下。 – akbarbin
感謝您的建議。它正在工作。 – akbarbin