ActiveRecord / Postgres:PGError必须出现在GROUP BY子句中或用于聚合函数

时间:2011-05-04 08:56:39

标签: 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

控制器

# 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?

1 个答案:

答案 0 :(得分:2)

删除order by子句,因为你正在分组成单行,所以无论如何我都没用。

请注意 - 重新格式化您的查询,以便可见可读