【发布时间】:2019-10-21 07:57:00
【问题描述】:
我的任务是获取一周内每小时平均一些数据。
{'hour': 0, 'count': 70}
{'hour': 1, 'count': 92}
{'hour': 2, 'count': 94}
{'hour': 3, 'count': 88}
{'hour': 4, 'count': 68}
{'hour': 5, 'count': 69}
{'hour': 6, 'count': 70}
{'hour': 7, 'count': 82}
{'hour': 8, 'count': 91}
{'hour': 9, 'count': 67}
{'hour': 10, 'count': 92}
{'hour': 11, 'count': 100}
{'hour': 12, 'count': 92}
{'hour': 13, 'count': 55}
{'hour': 14, 'count': 61}
{'hour': 15, 'count': 47}
{'hour': 16, 'count': 36}
{'hour': 17, 'count': 19}
{'hour': 18, 'count': 11}
{'hour': 19, 'count': 6}
{'hour': 20, 'count': 3}
{'hour': 21, 'count': 9}
{'hour': 22, 'count': 27}
{'hour': 23, 'count': 47}
以上数据是本次查询的结果
result = Device.objects.filter(station__in=stations, created_at__range=(start_date, end_date)) \
.extra({'hour': 'hour(created_at)'}) \
.values('hour').annotate(count=Count('id')).order_by('hour')
结果是按 7 天范围查询的,我想做的是获取 7 天内每小时的平均值,例如第 0 小时的计数总数为 70,然后我需要从 7 天开始平均。
有什么建议吗?
【问题讨论】:
-
@DeepakMahakale 这与我需要的东西无关,有什么建议可以用 django orm 来做吗?
-
不完全是,但您必须使用从日期中提取的小时数的平均函数并按小时分组。尝试在 django orm 中进行相同的转换
标签: python django django-models django-rest-framework