PostgreSQL特定的聚合函数
这些功能在PostgreSQL文档中有更详细的描述。
注
所有功能都没有默认别名,所以你必须明确提供一个。 像这样:
>>> SomeModel.objects.aggregate(arr=ArrayAgg('somefield'))
{'arr': [0, 1, 2]}
通用聚合函数
ArrayAgg
-
类
ArrayAgg
(表达式,** extra)[source]
返回一个值列表,包括null,并入数组。
BitAnd
-
类
BitAnd
(表达式,** extra)[source]
返回所有非空输入值的按位AND
的int
,如果所有值都为null,则返回None
。
BitOr
-
类
BitOr
(表达式,** extra)[source]
返回所有非空输入值的按位OR
的int
,如果所有值都为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]
将输入值连接到一个由delimiter
字符串分隔的字符串中。
-
定界符 T0>
必需参数。 需要是一个字符串。
-
不同 T0>
Django中的新功能1.11。
一个可选的布尔参数,用于确定连接值是否不同。 默认为False
。
统计功能
y
和x
所有这些函数的参数y
和x
可以是返回数值数据的字段或表达式的名称。 两者都是必需的。
Corr
-
类
Corr
(y,x)[source]
如果没有匹配的行,则返回相关系数为float
,或None
。
CovarPop
-
类
CovarPop
(y,x,sample = False)[source]
如果没有匹配的行,则返回总体协方差为float
或None
。
有一个可选参数:
-
样品 T0>
默认情况下,CovarPop
返回一般群体协方差。
然而,如果sample=True
,则返回值将是样本群体协方差。
RegrAvgX
-
类
RegrAvgX
(y,x)[source]
Returns the average of the independent variable (sum(x)/N
) as a
float
, or None
if there aren’t any matching rows.
RegrAvgY
-
类
RegrAvgY
(y,x)[source]
如果没有匹配的行,则返回因变量(sum(y)/N
))的平均值,作为float
或None
。
RegrCount
-
类
RegrCount
(y,x)[source]
返回两个表达式不为空的输入行数的int
。
RegrIntercept
-
类
RegrIntercept
(y,x)[source]
返回由(x, y) t>
对确定的最小二乘拟合线性方程的y截距,作为float
或None
,如果没有匹配的行。
RegrR2
-
类
RegrR2
(y,x)[source]
如果没有任何匹配的行,返回相关系数的平方为float
,或None
。
RegrSlope
-
类
RegrSlope
(y,x)[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
-
类
RegrSXX
(y,x)[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
-
类
RegrSXY
(y,x)[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
-
类
RegrSYY
(y,x)[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}