2012-12-26 29 views
1

我正在處理ROR應用程序,並且我使用帶有PostgreSQL數據庫的Hstore,並且問我是否可以通過我的表上的Hstore鍵進行組?Group by Hstore在PostgreSQL和ROR應用程序中鍵入

我這樣做:

select state , count(*) from infractions group by details -> 'commune';` 

,但我得到這個錯誤:

column "infractions.state" must appear in the GROUP BY clause or be used in an aggregate function 

細節是Hstore列,她是我的細節的例子:

"adress"=>"", "commune"=>"14", "province"=>"6", "description"=>"Ce fichier est sous licence Creative Commons" 

感謝

+1

檢查錯誤消息:列「違規.state「必須出現在GROUP BY子句中。你必須先解決這個問題。 –

+0

謝謝,但我怎麼能解決這個問題? –

+0

「狀態」必須是組中的一部分,它在您的選擇中沒有聚合函數。 –

回答

2

人們當他們從MySQL遷移時偶爾遇到這種情況。 MySQL允許從GROUP BY子句中刪除列。 PostgreSQL不會。你的實際問題是你缺少狀態欄。你有兩個選擇。如果你想拉只是一種狀態,你可以把它放在這樣一個總:

select max(state) , count(*) from infractions group by details -> 'commune'; 

如果你想組的狀態也可以:

select state , count(*) from infractions group by state, details -> 'commune';