2013-08-26 33 views
0

我正在使用Postgres。这将返回按用户分组的所有付款,这很好地工作。我如何求和这个查询

@users_with_payments = Payment.all(select: 'distinct(users.*)', joins: :user) 

我需要将每个用户的总付款金额总和为该金额。当尝试这个,我得到这个错误

@users_with_payments = Payment.all(select: 'distinct(users.*), SUM(payments.amount_cents)', joins: :user) 

PG::Error: ERROR: column "users.id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT distinct(users.*), SUM(payments.amount_cents) FROM "p...

任何想法,我做错了什么是构建此查询的正确方法?

回答

0

因为您使用的是postgres由错误column "users.id" must appear in the GROUP BY clause报告。您只需在请求结束时添加Group by users.id即可。