Skip to content

Latest commit

 

History

History
44 lines (29 loc) · 1.03 KB

mysql_group_by_date.md

File metadata and controls

44 lines (29 loc) · 1.03 KB

最近遇到这样一个业务需求: 要用图表显示每日注册用户数、每日交易量之类的

我马上联想到python::pandas的datarange分组,其实查阅资料发现ruby的日期遍历更简单

ActiveRecord

ActiveRecord.group("date(created_at)")

MySQL

SELECT
  SUM(created_at),
  DATE(created_at) AS daily
FROM
  users
GROUP BY
  daily

ruby daterange

@from.step(@to, step=1) do |date|
  day_data = Order.where('created_at between ? and ?',
                          date.beginning_of_day, date.end_of_day)
end

"MySQL_group_by_date"

相关链接

rails group records by date

pands dataframe group by date