Django汇总仅计算True值 [英] Django aggregate Count only True values

查看:109
本文介绍了Django汇总仅计算True值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用聚合来获取一列布尔值的计数。我想要True值的数量。



DJANGO代码:

  count = Model.objects.filter(id = pk).aggregate(bool_col = Count('my_bool_col')

这将返回所有行的计数。



SQL查询应该是:



< pre class = lang-sql prettyprint-override> 选择计数(在my_bool_col然后为1否则为null的情况下)FROM< table_name>

这是我的实际代码:

  stats = Team.objects.filter(id = team.id).aggregate(
目标=总和('statistics__goals'),
助攻= Sum('statistics__assists'),
min_penalty = Sum('statistics__minutes_of_penalty'),
balance = Sum('statistics__balance'),
gwg = Count('statistics__gwg'),
gk_goals_avg = Sum('statistics__gk_goals_avg'),
gk_shutout = Count('statistics__gk_shutout'),
points = Sum('sta tistics__points'),

感谢 Peter DeGlopper 的建议使用 django-aggregate-if



这是解决方案:

 来自django.db.models import Sum来自django.db.models的
import Q
来自gregation_if import计数

统计信息= Team.objects.filter(id = team.id).aggregate(
目标= Sum('statistics__goals'),
辅助= Sum('statistics__assists'),
balance = Sum('statistics__balance'),
min_penalty = Sum('statistics__minutes_of_penalty'),
gwg = Count('statistics__gwg',only = Q(statistics__gwg = True)),
gk_goals_avg = Sum('statistics__gk_goals_avg'),
gk_shutout = Count('statistics__gk_shutout',only = Q(统计True)),
points = Sum('statistics__points'),


解决方案

更新:



自Django 1.10起,您可以:

  from django.db.models导入Count,Case,when 
query_set.aggregate(
bool_col = Count(
Case(When(my_bool_col = True,then = Value(1 )))


了解条件表达式类



旧答案。



似乎您想做的是某种条件聚合 。目前,聚合函数不支持诸如 filter exclude :fieldname__lt,fieldname__gt,...



因此,您可以尝试以下操作:



django-aggregate-if



说明


Django查询的条件聚合,就像Excel中著名的SumIf和CountIf一样。


您还可以先注释每个团队的期望值,我的意思是为每个团队计算您感兴趣的字段中 True 的数量。然后执行您要执行的所有聚合


I'm using aggregate to get the count of a column of booleans. I want the number of True values.

DJANGO CODE:

count = Model.objects.filter(id=pk).aggregate(bool_col=Count('my_bool_col')

This returns the count of all rows.

SQL QUERY SHOULD BE:

SELECT count(CASE WHEN my_bool_col THEN 1 ELSE null END) FROM <table_name>

Here is my actual code:

stats = Team.objects.filter(id=team.id).aggregate(
    goals=Sum('statistics__goals'),
    assists=Sum('statistics__assists'),
    min_penalty=Sum('statistics__minutes_of_penalty'),
    balance=Sum('statistics__balance'),
    gwg=Count('statistics__gwg'),
    gk_goals_avg=Sum('statistics__gk_goals_avg'),
    gk_shutout=Count('statistics__gk_shutout'),
    points=Sum('statistics__points'),
)

Thanks to Peter DeGlopper suggestion to use django-aggregate-if

Here is the solution:

from django.db.models import Sum
from django.db.models import Q
from aggregate_if import Count

stats = Team.objects.filter(id=team.id).aggregate(
    goals=Sum('statistics__goals'),
    assists=Sum('statistics__assists'),
    balance=Sum('statistics__balance'),
    min_penalty=Sum('statistics__minutes_of_penalty'),
    gwg=Count('statistics__gwg', only=Q(statistics__gwg=True)),
    gk_goals_avg=Sum('statistics__gk_goals_avg'),
    gk_shutout=Count('statistics__gk_shutout', only=Q(statistics__gk_shutout=True)),
    points=Sum('statistics__points'),
)

解决方案

Update:

Since Django 1.10 you can:

from django.db.models import Count, Case, When
query_set.aggregate(
    bool_col=Count(
        Case(When(my_bool_col=True, then=Value(1)))
    )
)

Read about the Conditional Expression classes

Old answer.

It seems what you want to do is some kind of "Conditional aggregation". Right now Aggregation functions do not support lookups like filter or exclude: fieldname__lt, fieldname__gt, ...

So you can try this:

django-aggregate-if

Description taken from the official page.

Conditional aggregates for Django queries, just like the famous SumIf and CountIf in Excel.

You can also first annotate the desired value for each team, I mean count for each team the ammount of True in the field you are interested. And then do all the aggregation you want to do.

这篇关于Django汇总仅计算True值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆