2017-03-03 26 views
0

這是來自我的Rails 5應用程序,並且在開發中針對Sqlite工作,但是我可以讓它在針對Postgres的生產中工作。我玩過並搜索了它等,但無法完成。在此先感謝:我如何讓strftime和GROUP和WHERE在Postgres中工作

作品SQLite中>

Dailylog.select("date").group("strftime('%m', date)").where(user_id: user) 

最近嘗試了在Postgres>

Dailylog.select("date").group(extract(month from "date")).where(user_id: user) 
+1

'strftime()'不是Postgres函數。您可以在文檔中查看Postgres日期/時間函數:https://www.postgresql.org/docs/current/static/functions-datetime.html。 –

回答

2

可以使用date_part功能的Postgres這樣

Dailylog.select("date_part('month', date)") 
.group("date_part('month', date)") 
.where(user_id: user) 

而且一些建議作爲旁註,請在開發中使用Postgres,它可以爲您節省許多麻煩。

+0

嗨 這有幫助,但現在我有一個不同的錯誤! (PG :: GroupingError:錯誤:列「dailylogs.date」必須出現在GROUP BY子句中或用於聚合函數 – Simonp27

+0

@ Simonp27請參閱我的更新 – Iceman