【问题标题】:How can I sum on this query我如何总结这个查询
【发布时间】:2013-08-26 16:47:39
【问题描述】:

我正在使用 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...

知道我做错了什么以及构建此查询的正确方法是什么?

【问题讨论】:

    标签: ruby-on-rails postgresql


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 2017-06-10
      • 2011-03-30
      • 1970-01-01
      • 2015-01-08
      相关资源
      最近更新 更多