首页 > 解决方案 > 使用按日期分组的百分比注释查询集

问题描述

假设我有以下模型:

class Order(models.Model):
    category = models.CharField(max_length=100, choices=CATEGORY_CHOICES, default=DEFAULT_CHOICE)
    created_at = models.DateTimeField(auto_now_add=True)

我需要使用按月分组Order的每个查询集的百分比category(基于created_at字段)来注释查询集。我设法编写了一个查询来计算Order按月分组的每个:

orders_per_month = (Order.objects
    .annotate(month=TruncMonth('created_at'))
    .values('month')
    .annotate(count=Count('id'))
    .order_by('month')
    .values('month', 'count')
)

将最后一个更改.values().values('month', 'category', 'count'),我可以得到按category和分组的计数month

category是否可以使用 Django 的 ORM 获得每个按月分组的百分比?例如,如果我有以下数据:

MONTH | CATEGORY
Jan   | 'A'
Jan   | 'B'
Feb   | 'A'

我想得到类似的东西:

[
    (Jan, 'A', 0.5),
    (Jan, 'B', 0.5),
    (Feb, 'A', 1),
]

提前致谢。

标签: djangopython-3.xdjango-modelsdjango-ormdjango-annotate

解决方案


正如@ac2001 在评论中所建议的那样,使用 Django 的Window 函数,我设法得到了我需要的东西。

使用示例模型并假设我希望每个category按月分组的百分比:

orders_per_month = (Order.objects
    .annotate(month=TruncMonth('created_at'))
    .values('month', 'category')
    .distinct()
    .annotate(month_total=Window(
        expression=Count('id'),
        partition_by=[F('month')],
    ))
    .annotate(month_category=Window(
        expression=Count('id'),
        partition_by=[F('month'), F('category')],
    ))
    .annotate(percentage=ExpressionWrapper(
        F('month_category') * 100.0 / F('month_total'),
        output_field=FloatField()
    ))
    .values('month', 'percentage', 'category')
)

欢迎任何有关进一步简化的建议。


推荐阅读