我正在使用的軌道MySQL查詢PostgreSQL裏不工作在軌3.2.11
grouped_link_counters = uattachment).link_counters.group("year(created_at), month(created_at)").count("created_at")
下面的查詢,並讓用mysql適配器按照軌道控制檯結果。
UserAttachment Load (0.2ms) SELECT `user_attachments`.* FROM `user_attachments` WHERE `user_attachments`.`id` = 132 LIMIT 1
(0.2ms) SELECT COUNT(`link_counters`.`created_at`) AS count_created_at, year(created_at), month(created_at) AS year_created_at_month_created_at FROM `link_counters` WHERE `link_counters`.`user_attachment_id` = 132 GROUP BY year(created_at), month(created_at)
=> {11=>9, 12=>15, 1=>1, 2=>1}
但是,當我使用PostgreSQL相同的命令拋出以下錯誤: -
SELECT COUNT("link_counters"."created_at") AS count_created_at, year(created_at), month(created_at) AS year_created_at_month_created_at FROM "link_counters" WHERE "link_counters"."user_attachment_id" = 1 GROUP BY year(created_at), month(created_at)
ActiveRecord::StatementInvalid: PG::Error: ERROR: function year(timestamp without time zone) does not exist
LINE 1: ...link_counters"."created_at") AS count_created_at, year(creat...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
: SELECT COUNT("link_counters"."created_at") AS count_created_at, year(created_at), month(created_at) AS year_created_at_month_created_at FROM "link_counters" WHERE "link_counters"."user_attachment_id" = 1 GROUP BY year(created_at), month(created_at)
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:in `async_exec'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:in `exec_no_cache'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:662:in `block in exec_query'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activesupport-3.2.11/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `exec_query'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1248:in `select'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:289:in `execute_grouped_calculation'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:206:in `perform_calculation'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:159:in `calculate'
from /home/nishutosh/.rvm/gems/[email protected]/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:58:in `count'
from (irb):63
from /home/nishutosh/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /home/nishutosh/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /home/nishutosh/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3p194 :064 >
誰能告訴我什麼可以做,以獲得從PostgreSQL同樣的反應。 在此先感謝!
閱讀消息:「錯誤:函數年(沒有時區的時間戳)不存在」。這個函數在PostgreSQL中不存在,但你可以自己創建它。 – 2013-02-12 13:25:15
請參閱:http://www.postgresql.org/docs/current/static/functions-datetime.html – 2013-02-12 13:28:17
使用'extract'。更多在這裏:http://stackoverflow.com/questions/4493705/mysql-to-postgresql-how-to-modify-this-sql-query – RAJ 2013-02-12 17:34:31