ruby-on-rails - 事件记录/Postgres : PGError must appear in the GROUP BY clause or be used in an aggregate function

标签 ruby-on-rails ruby-on-rails-3 postgresql activerecord group-by

在我的模型中,我有几个查询可以一个接一个地使用(和重复使用)。其中之一应该是总金额。这在 SQLite 上工作正常并在 Postgres 上抛出错误:

ActiveRecord::StatementInvalid (PGError: ERROR:  column "entries.date" must appear in the GROUP BY clause or be used in an aggregate function
: SELECT sum(case when joint = false then amount else amount / 2 end) as total, sum(case when joint = false then amount else 0 end) as sum_personal, sum(case when joint = true and user_id = 1 then amount / 2 else 0 end) as sum_user_joint, sum(case when joint = true and user_id = 2 then amount / 2 else 0 end) as sum_partner_joint  FROM "entries" WHERE (1 = entries.user_id OR (2 = entries.user_id AND entries.joint = 't')) AND ('2011-04-01' <= entries.date AND entries.date <= '2011-04-30') AND (amount_calc > 0 AND compensation = 'f') ORDER BY date asc)

Model.rb相关部分

  # all entries of one month
  def self.all_entries_month(year, month, user_id, partner_id)
    mydate = Date.new(year, month, 1)

    where(':user_id = entries.user_id OR (:partner_id = entries.user_id AND entries.joint = :true)', {
        :user_id => user_id,
        :partner_id => partner_id,
        :true => true
    }).
    where(':first_day <= entries.date AND entries.date <= :last_day', { 
        :first_day => mydate,
        :last_day => mydate.at_end_of_month
    })
  end

  def self.income
    where('amount_calc > 0 AND compensation = ?', false)
  end

  def self.cost
    where('amount_calc <= 0 AND compensation = ?', false)
  end

  def self.order_by_date
    order('date asc')
  end

  # group by tag and build sum of groups named group_sum
  def self.group_by_tag(order)
    group('tag').
    select('tag, ' +
           'sum(case when joint = "f" then amount else amount / 2 end) as tag_sum'
          ).
    order('tag_sum ' + order)
  end

  def self.multiple_sums(user_id, partner_id)
    case ActiveRecord::Base.connection.adapter_name  
      when 'SQLite'
        select('sum(case when joint = "f" then amount else amount / 2 end) as total, ' +
               'sum(case when joint = "f" then amount else 0 end) as sum_personal, ' + 
               'sum(case when joint = "t" and user_id = ' + user_id.to_s + ' then amount / 2 else 0 end) as sum_user_joint, ' + 
               'sum(case when joint = "t" and user_id = ' + partner_id.to_s + ' then amount / 2 else 0 end) as sum_partner_joint ' 
        )
      when 'PostgreSQL'
        select('sum(case when joint = false then amount else amount / 2 end) as total, ' +
               'sum(case when joint = false then amount else 0 end) as sum_personal, ' + 
               'sum(case when joint = true and user_id = ' + user_id.to_s + ' then amount / 2 else 0 end) as sum_user_joint, ' + 
               'sum(case when joint = true and user_id = ' + partner_id.to_s + ' then amount / 2 else 0 end) as sum_partner_joint ' 
        )
    else
      raise 'Query not implemented for this DB adapter'
    end  
  end

Controller

# get all entries of given month
@cost = Entry.all_entries_month(@year, @month, current_user.id, current_partner.id).cost

# group cost by categories
@group_cost = @cost.group_by_tag('asc')

# still need to sort by date
@cost = @cost.order_by_date

@calc_cost = @cost.multiple_sums(current_user.id, current_partner.id)[0]  

如何在不破坏其他查询的情况下更改我的查询 multiple_sums?或者我是否需要在不使用现有的情况下从地面实现 multiple_sums?

最佳答案

删除 order by 子句,据我所知这是无用的,因为您正在分组为单行。

请重新格式化您的查询,使其可见可读

关于ruby-on-rails - 事件记录/Postgres : PGError must appear in the GROUP BY clause or be used in an aggregate function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5880947/

相关文章:

ruby-on-rails - 为什么我应该在 Rails 中使用 RSpec 或 shoulda?

ruby-on-rails - 为什么 alias_method 在 Rails 模型中失败

ruby-on-rails - 未创建 Rolify 表

postgresql - 如何将 Docker Web 应用程序容器连接到 Docker PostgreSQL 容器?

arrays - 改进基于输入数组的 UPSERT 函数

c++ - 使用可为 Null 的参数调用 PL/PGSQL 函数

ruby-on-rails - 有_许多 :through association error 'could not find the source association'

ruby-on-rails - 使用 Rails 配置 Neo4j 以适应不同的环境

json - 导轨 3 : How to return errors in a JSON request?

ruby-on-rails-3 - 如何按两列分组并提供分组项目的总和