PostgreSQL 特有聚合函数Link to this heading

这些功能可以从 django.contrib.postgres.aggregates 模块中获得。在 PostgreSQL docs 中对这些功能有更详细的描述。

通用聚合函数Link to this heading

ArrayAggLink to this heading

class ArrayAgg(expression, distinct=False, filter=None, ordering=(), **extra)Link to this definition

返回一个值的列表,包括空值,连接成一个数组。

distinctLink to this definition

一个可选的布尔参数,用于确定数组值是否会被去重。默认值为 False

orderingLink to this definition

可选的字段名字符串(可选的 "-" 前缀表示降序)或表达式(或字符串和/或表达式的元组或列表),指定结果列表中元素的顺序。

举例:

Code
'some_field'
'-some_field'
from django.db.models import F
F('some_field').desc()

BitAndLink to this heading

class BitAnd(expression, filter=None, **extra)Link to this definition

返回所有非空输入值的位式 ANDint,如果所有值为空,则返回 None

BitOrLink to this heading

class BitOr(expression, filter=None, **extra)Link to this definition

返回所有非空输入值的位式 ORint,如果所有值为空,则返回 None

BoolAndLink to this heading

class BoolAnd(expression, filter=None, **extra)Link to this definition

如果所有输入值为真,返回 True,如果所有值为空或没有值,返回 None,否则返回 False

使用实例:

Code
class Comment(models.Model):
    body = models.TextField()
    published = models.BooleanField()
    rank = models.IntegerField()

>>> from django.db.models import BooleanField, Q
>>> from django.contrib.postgres.aggregates import BoolAnd
>>> Comment.objects.aggregate(booland=BoolAnd('published'))
{'booland': False}
>>> Comment.objects.aggregate(booland=BoolAnd(Q(rank__lt=100), output_field=BooleanField()))
{'booland': True}

BoolOrLink to this heading

class BoolOr(expression, filter=None, **extra)Link to this definition

如果所有输入值为真,返回 True,如果所有值为空或没有值,返回 None,否则返回 False

使用实例:

Code
class Comment(models.Model):
    body = models.TextField()
    published = models.BooleanField()
    rank = models.IntegerField()

>>> from django.db.models import BooleanField, Q
>>> from django.contrib.postgres.aggregates import BoolOr
>>> Comment.objects.aggregate(boolor=BoolOr('published'))
{'boolor': True}
>>> Comment.objects.aggregate(boolor=BoolOr(Q(rank__gt=2), output_field=BooleanField()))
{'boolor': False}

JSONBAggLink to this heading

class JSONBAgg(expressions, filter=None, **extra)Link to this definition

将输入值以 JSON 数组形式返回。

StringAggLink to this heading

class StringAgg(expression, delimiter, distinct=False, filter=None, ordering=())Link to this definition

返回连接成一个字符串的输入值,用 delimiter 字符串分隔。

delimiterLink to this definition

必要参数。需要是一个字符串。

distinctLink to this definition

一个可选的布尔参数,用于确定连接的值是否是不同的。默认值为 False

orderingLink to this definition

可选的字段名字符串(可选的 "-" 前缀表示降序)或表达式(或字符串和/或表达式的元组或列表),指定结果字符串中元素的顺序。

例子与 ArrayAgg.ordering 相同。

统计的聚合功能Link to this heading

yxLink to this heading

所有这些函数的参数 yx 可以是字段名或返回数值数据的表达式。这两个参数都是必须的。

CorrLink to this heading

class Corr(y, x, filter=None)Link to this definition

返回相关系数为 float,如果没有任何匹配的记录,则返回 None

CovarPopLink to this heading

class CovarPop(y, x, sample=False, filter=None)Link to this definition

返回总体方差的 float,如果没有任何匹配的行,则返回 None

包含一个可选参数:

sampleLink to this definition

默认情况下,CovarPop 返回一般总体协方差。但是,如果 sample=True,返回值将是样本总体协方差。

RegrAvgXLink to this heading

class RegrAvgX(y, x, filter=None)Link to this definition

返回自变量的平均值(sum(x)/N),作为 float,如果没有任何匹配的行,则返回 None

RegrAvgYLink to this heading

class RegrAvgY(y, x, filter=None)Link to this definition

返回因变量的平均数(sum(y)/N),作为 float,如果没有任何匹配的行,则返回 None

RegrCountLink to this heading

class RegrCount(y, x, filter=None)Link to this definition

返回两个表达式都不为空的输入行数的 int

RegrInterceptLink to this heading

class RegrIntercept(y, x, filter=None)Link to this definition

返回由 (x, y) 对确定的最小二乘拟合线性方程的 y 截距,如果没有任何匹配的行,则返回 None

RegrR2Link to this heading

class RegrR2(y, x, filter=None)Link to this definition

float `` 的形式返回相关系数的平方,如果没有任何匹配的行,则返回 ``None

RegrSlopeLink to this heading

class RegrSlope(y, x, filter=None)Link to this definition

返回由 (x, y) 对确定的最小二乘拟合线性方程的斜率,如果没有任何匹配的行,则返回 None

RegrSXXLink to this heading

class RegrSXX(y, x, filter=None)Link to this definition

返回 sum(x^2) - sum(x)^2/N (自变量的“平方和”)作为 float,如果没有任何匹配的行,则返回 None

RegrSXYLink to this heading

class RegrSXY(y, x, filter=None)Link to this definition

返回 sum(x*y) - sum(x) * sum(y)/N (自变量与因变量的乘积之和),如果没有任何匹配的行,则返回 float,或者 None

RegrSYYLink to this heading

class RegrSYY(y, x, filter=None)Link to this definition

返回 sum(y^2) - sum(y)^2/N (因变量的“平方和”)作为 float,如果没有任何匹配的行,则返回 None

使用实例:Link to this heading

我们将用这个例表:

Code
| FIELD1 | FIELD2 | FIELD3 |
|--------|--------|--------|
|    foo |      1 |     13 |
|    bar |      2 | (null) |
|   test |      3 |     13 |

下面是一些通用聚合函数的例子:

Code
>>> TestModel.objects.aggregate(result=StringAgg('field1', delimiter=';'))
{'result': 'foo;bar;test'}
>>> TestModel.objects.aggregate(result=ArrayAgg('field2'))
{'result': [1, 2, 3]}
>>> TestModel.objects.aggregate(result=ArrayAgg('field1'))
{'result': ['foo', 'bar', 'test']}

下一个例子显示了统计聚合函数的使用。基本的数学知识将不作说明(例如,你可以在 wikipedia ):

Code
>>> TestModel.objects.aggregate(count=RegrCount(y='field3', x='field2'))
{'count': 2}
>>> TestModel.objects.aggregate(avgx=RegrAvgX(y='field3', x='field2'),
...                             avgy=RegrAvgY(y='field3', x='field2'))
{'avgx': 2, 'avgy': 13}