【问题标题】:How to exclude value that is equal to zero while using Django aggregate queries for average or minimum如何在使用 Django 聚合查询获取平均值或最小值时排除等于零的值
【发布时间】:2020-12-25 05:58:14
【问题描述】:

我有一个数据库表,其中销售列中的某些值为零。

到目前为止,我的查询返回所有值的最小值。

Sale.objects.all().aggregate(minimum=Min('sale'))

我想在计算最小值或平均值时排除等于零的值。

所以我的问题是该查询是如何完成的?

【问题讨论】:

    标签: python django database django-orm


    【解决方案1】:

    试试:

    Sale.objects.filter(sales > 0).aggregate(minimum=Min('sale'))
    

    或:

    Sale.objects.exclude(sales = 0).aggregate(minimum=Min('sale'))
    

    【讨论】:

      猜你喜欢
      • 2015-04-12
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 2015-11-13
      • 2015-12-29
      • 2016-07-09
      相关资源
      最近更新 更多