最近遇到这样一个业务需求: 要用图表显示每日注册用户数、每日交易量之类的
我马上联想到python::pandas的datarange分组,其实查阅资料发现ruby的日期遍历更简单
ActiveRecord.group("date(created_at)")
SELECT
SUM(created_at),
DATE(created_at) AS daily
FROM
users
GROUP BY
daily
@from.step(@to, step=1) do |date|
day_data = Order.where('created_at between ? and ?',
date.beginning_of_day, date.end_of_day)
end
相关链接