2010-11-02 70 views
1

嗨 我改變了我的數據庫從MySQL到PostgreSQL和我每次使用查詢隨着時間的推移得到一個錯誤:爲了聲明Rails的PostgreSQL的問題,爲了聲明

例如,下面的代碼工作完全在MySQL

Hour.sum("working_hours",:conditions=>['project_id=? AND reported_date=?',project,h.reported_date],:order=>"reported_date 

但讓我在PostgreSQL的

PGError: ERROR: column "hours.reported_date" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...rted_date='2010-10-06 00:00:00.000000') ORDER BY reported_d..
: SELECT sum("hours".working_hours) AS sum_working_hours FROM "hours" WHERE (project_id=1 AND reported_date='2010-10-06 00:00:00.000000') ORDER BY reported_date

一個錯誤。如果我刪除的順序發言,然後查詢工程確定
我將非常感謝在這個問題上的任何幫助

回答

2

PostreSQL比MySQL更嚴格的SQL標準。

SQL指出如果您按某列的ORDER,該列必須爲SELECT ed並出現在GROUP BY子句中。

試試這個:

Hour.sum("working_hours",:conditions=>['project_id=? AND reported_date=?',project,h.reported_date], :order=>"reported_date", :group_by => "working_hours" 
+0

該查詢實際上返回資金的有序哈希,通過reported_date分組。如果他想要所有工作時間的總和,這不會給他正確的答案。 – 2010-11-02 14:59:14

+0

我試過但我仍然得到一個錯誤 – 2010-11-02 15:24:10

+0

PGError:錯誤:列「hours.reported_date」必須出現在 我試過了,但我仍然得到一個錯誤 GROUP BY子句或用於聚合函數 LINE 1: ...:00:00.000000')GROUP BY working_hours ORDER BY reported_d ... – 2010-11-02 15:24:33