【发布时间】:2015-08-08 12:12:54
【问题描述】:
我在 Django 中有以下模型。
class StoreVideoEventSummary(models.Model):
Customer = models.ForeignKey(GlobalCustomerDirectory, null=True, db_column='CustomerID', blank=True, db_index=True)
Store = models.ForeignKey(Store, null=True, db_column='StoreID', blank=True, related_name="VideoEventSummary")
Timestamp = models.DateTimeField(null=True, blank=True, db_index=True)
PeopleCount = models.IntegerField(null=True, blank=True)
我想知道每小时进入商店的人数。
为了实现这一点,我尝试在 Timestamp 上按小时对行进行分组,并对 PeopleCount 列求和。
store_count_events = StoreVideoEventSummary.objects.filter(Timestamp__range=(start_time, end_time),
Customer__id=customer_id,
Store__StoreName=store)\
.order_by("Timestamp")\
.extra({
"hour": "date_part(\'hour\', \"Timestamp\")"
}).annotate(TotalPeople=Sum("PeopleCount"))
这似乎没有按小时对结果进行分组,它只是在查询集中的每一行中添加一个新列TotalPeople,其值与PeopleCount 相同。
【问题讨论】:
-
我错过了一些东西..“StartTime”来自哪里?
"hour": "date_part(\'hour\', \"StartTime\")" -
对不起,应该是
Timestamp。
标签: python django orm group-by