2013-08-26 66 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即可。