rails 3 group by date datetime列 [英] rails 3 group by date a datetime column

查看:155
本文介绍了rails 3 group by date datetime列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

我如何按日期分组? start_at列是一个datetime列

解决方案

这应该工作(rails 3):

  Foo.order(:start_at).group(DATE(start_at))。count 

编辑:如果您使用PostgreSQL,查询应该是

  Foo.order(DATE(start_at))。group(DATE(start_at))。count 

或者您将收到错误

 (PGError:ERROR:columnfoos.start_at必须出现在GROUP BY子句或用于聚合函数)

根据



绘制新的用户按日期在Rails应用程序中使用Seer





http://www.pastbedti。我/ 2009/11 / g rouping-a-timestamp-field-by-date-in-ruby-on-rails-postgresql /


Foo.group(:start_at).count(:id)

How I can group this by date ? the "start_at" column is an datetime column

解决方案

This should work (rails 3):

Foo.order(:start_at).group("DATE(start_at)").count

edit: if you're using PostgreSQL, the query should be

Foo.order("DATE(start_at)").group("DATE(start_at)").count

or you'll get an error

("PGError: ERROR:  column "foos.start_at" must appear in the GROUP BY clause or be used in an aggregate function")

Based on

Graphing new users by date in a Rails app using Seer

and

http://www.pastbedti.me/2009/11/grouping-a-timestamp-field-by-date-in-ruby-on-rails-postgresql/

这篇关于rails 3 group by date datetime列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆