PostgreSQL特定的聚合函数

这些功能在PostgreSQL文档中有更详细的描述。

所有功能都没有默认别名,所以你必须明确提供一个。 像这样:

>>> SomeModel.objects.aggregate(arr=ArrayAgg('somefield'))
{'arr': [0, 1, 2]}

通用聚合函数

ArrayAgg

ArrayAgg表达式** extra[source]

返回一个值列表,包括null,并入数组。

BitAnd

BitAnd表达式** extra[source]

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

BitOr

BitOr表达式** extra[source]

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

BoolAnd

BoolAnd表达式** extra[source]

Returns True, if all input values are true, None if all values are null or if there are no values, otherwise False .

BoolOr

BoolOr表达式** extra[source]

Returns True if at least one input value is true, None if all values are null or if there are no values, otherwise False.

JSONBAgg

JSONBAgg表达式** extra[source]
Django中的新功能1.11。

返回输入值作为JSON数组。 需要PostgreSQL≥9.5。

StringAgg

StringAgg表达式分隔符distinct = False[source] ¶ T6>

将输入值连接到一个由delimiter字符串分隔的字符串中。

定界符 T0> ¶ T1>

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

不同 T0> ¶ T1>
Django中的新功能1.11。

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

统计功能

yx

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

Corr

Corryx[source]

如果没有匹配的行,则返回相关系数为float,或None

CovarPop

CovarPopyxsample = False[source] ¶ T6>

如果没有匹配的行,则返回总体协方差为floatNone

有一个可选参数:

样品 T0> ¶ T1>

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

RegrAvgX

RegrAvgXyx[source]

Returns the average of the independent variable (sum(x)/N) as a float, or None if there aren’t any matching rows.

RegrAvgY

RegrAvgYyx[source]

如果没有匹配的行,则返回因变量(sum(y)/N))的平均值,作为floatNone

RegrCount

RegrCountyx[source]

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

RegrIntercept

RegrInterceptyx[source]

返回由(x, y) t>对确定的最小二乘拟合线性方程的y截距,作为floatNone,如果没有匹配的行。

RegrR2

RegrR2yx[source]

如果没有任何匹配的行,返回相关系数的平方为float,或None

RegrSlope

RegrSlopeyx[source]

Returns the slope of the least-squares-fit linear equation determined by the (x, y) pairs as a float, or None if there aren’t any matching rows.

RegrSXX

RegrSXXyx[source]

Returns sum(x^2) - sum(x)^2/N (“sum of squares” of the independent variable) as a float, or None if there aren’t any matching rows.

RegrSXY

RegrSXYyx[source]

Returns sum(x*y) - sum(x) * sum(y)/N (“sum of products” of independent times dependent variable) as a float, or None if there aren’t any matching rows.

RegrSYY

RegrSYYyx[source]

Returns sum(y^2) - sum(y)^2/N (“sum of squares” of the dependent variable) as a float, or None if there aren’t any matching rows.

使用示例

我们将使用此示例表:

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

以下是一些通用聚合功能的一些示例:

>>> 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']}

下一个示例显示统计聚合函数的用法。 下面的数学将不会被描述(你可以阅读这个例子,例如在维基百科):

>>> 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}