我在 Rails 中使用以下查询
grouped_link_counters = uattachment).link_counters.group("year(created_at), month(created_at)").count("created_at")
它使用mysql适配器在rails控制台中给出以下结果。
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/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:in `async_exec'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:in `exec_no_cache'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:662:in `block in exec_query'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activesupport-3.2.11/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `exec_query'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1248:in `select'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:289:in `execute_grouped_calculation'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:206:in `perform_calculation'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:159:in `calculate'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/activerecord-3.2.11/lib/active_record/relation/calculations.rb:58:in `count'
from (irb):63
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /home/nishutosh/.rvm/gems/ruby-1.9.3-p194@cdrop/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 获得相同的响应。提前致谢!