Flask-SQLALchemy对表中数据按时间进行统计
表结构如下:
class Status(db.Model):
id = db.Column(db.Integer, primary_key=True)
submit_time = db.Column(db.DateTime, default=datetime.now())
Status
表接受用户的提交,现在想对用户的提交情况按时间进行统计。例如过去24小时,每小时的提交次数;过去12个月,每个月的提交次数。
请教采用Flask-SQLALchemy该如何写这样的查询?
山田大魔王
11 years, 3 months ago
Answers
from datetime import datetime, timedelta
NOW = datetime.utcnow()
last_24h_submits_count = []
for h in xrange(1,25):
count = session.query(Status).filter(Status.submit_time.between(NOW - timedelta(seconds=h*3600-1), NOW - timedelta(hours=h-1))).count()
last_24h_submits_count.append(count)
aojian
answered 11 years, 3 months ago