python - 你能对 Django 查询集中的聚合值做额外的数学运算吗

标签 python django math django-queryset

我试图避免重复聚合我已经计算出的值。我已经得到年度总计,但我还想要季度总计

什么会起作用:

django.db.models import Value

props_obj = PropertiesUsers.objects\
    .aggregate(returns_coc_a=Sum(F('subscribe_amount')
               * (F('deal_coc') / Value(100))),
               returns_coc_q=Sum(F('subscribe_amount')
               * (F('deal_coc') / Value(100))) / Value(4))

我想知道我是否/如何做(重复使用年度金额):

props_obj = PropertiesUsers.objects\
    .aggregate(returns_coc_a=Sum(F('subscribe_amount')
               * (F('deal_coc') / Value(100))),
               returns_coc_q=returns_coc_a / Value(4))

最佳答案

为什么不将年度总和分配给变量并重新使用它?像这样:

django.db.models import F, Sum, Value

annual = Sum(F('subscribe_amount') * (F('deal_coc') / Value(100)))
quaterly = annual / Value(4)

props_obj = PropertiesUsers.objects\
    .aggregate(returns_coc_a=annual,
               returns_coc_q=quaterly)

关于python - 你能对 Django 查询集中的聚合值做额外的数学运算吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58158345/

相关文章:

python - 根据唯一条目的数量按组计算比例

python - LightGBM 使用自定义 eval 函数和内置损失函数提前停止

python - SQLALCHEMY or_ 列表理解

python - 健全性检查 : date driven event handler with django

django - 如何开始构建 django 网站以及 django 如何构建页面?

python - 通过 Python 使用 Watson Assistant/Watson Conversation

html - 表格背景图片未在谷歌浏览器中设置

C++ TTmath : Power of?

c++ - 是否可以根据眼睛和嘴巴的位置确定面部的偏航、俯仰和滚动? (含图片)

python - 自适应网格细化 - Python