0

我正在 MySQL 上开发并已部署到使用 Postgres 的 Heroku。看来我的一些查询需要重写才能与 Postgres 一起使用。任何人都可以通过以下问题协助修复我的小组吗?

Heroku 日志错误:

ActiveRecord::StatementInvalid (PGError: ERROR:  column "itunes_data.artist_show" must appear in the GROUP BY clause or be used in an aggregate function

从我的控制器:

def itunes_top_tracks
 @itunes_top_tracks = ItunesData.all(:select => "artist_show, title, label_studio_network, isrc, SUM(units) as unitsum", :group => :isrc, :order => "unitsum DESC", :limit => 10)
end

我知道问题出在哪里,并且知道我将如何在直接 SQL 中做到这一点,我只是不确定 Rails 方式吗?

谢谢大家

4

2 回答 2

1
def itunes_top_tracks
 @itunes_top_tracks = ItunesData.all(:select => "artist_show, title, label_studio_network, isrc, SUM(units) as unitsum", 
    :group => [:artist_show, :title, :label_studio_network, :isrc], :order => "unitsum DESC", :limit => 10)
end
于 2012-04-16T11:38:31.417 回答
0

看看这个问题 - PostgreSQL - GROUP BY 子句或在聚合函数中使用它为在 Postgres 中使用 group bys 的答案提供了方向。

或者考虑在 Heroku 的 mySQL 插件上使用,不要担心尝试使用 Postgres。

于 2012-04-16T11:38:12.710 回答