是否可以直接在ActiveRecord/Rails中按小时/分钟/季度小时进行分组? [英] Is it possible to group by hour/minute/quarter hour directly in ActiveRecord/Rails?
本文介绍了是否可以直接在ActiveRecord/Rails中按小时/分钟/季度小时进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按小时/天,甚至是四分之一小时对数据进行分组.
I want to group my data by the hour/day possibly even to the quarter hour.
我可以直接在ActiveRecord中执行此操作吗?还是应该只获取所有数据并手动进行汇总?
Can I do this directly in ActiveRecord or should I just grab all the data and do the aggregation manually?
Table "Checkin":
Time NumBikes ChangeBikes
09:00 5 5
09:05 6 1
09:10 10 4
10:00 6 4
10:05 8 2
10:10 16 8
Looking to get something like:
Time Sum(ChangeBikes)
09:00 10
10:00 14
提前谢谢克里斯.
推荐答案
我寻求的解决方案是在我所追求的时间段内在表上创建其他列.对于上面的示例,我将添加一个Time_Hour列,并将时间四舍五入到适当的小时.然后,我可以对该列进行分组/分析,以按小时获取统计信息.
The solution I went for was to create additional columns on the table for the time periods I am after. For the example above, I would add a Time_Hour column with the time rounded down to the appropriate hour. I could then do groupings/analysis on that column to get stats by the hour.
这篇关于是否可以直接在ActiveRecord/Rails中按小时/分钟/季度小时进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文