aggregates.txt 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. =========================================
  2. PostgreSQL specific aggregation functions
  3. =========================================
  4. .. module:: django.contrib.postgres.aggregates
  5. :synopsis: PostgreSQL specific aggregation functions
  6. These functions are described in more detail in the `PostgreSQL docs
  7. <https://www.postgresql.org/docs/current/static/functions-aggregate.html>`_.
  8. .. note::
  9. All functions come without default aliases, so you must explicitly provide
  10. one. For example::
  11. >>> SomeModel.objects.aggregate(arr=ArrayAgg('somefield'))
  12. {'arr': [0, 1, 2]}
  13. General-purpose aggregation functions
  14. =====================================
  15. ``ArrayAgg``
  16. ------------
  17. .. class:: ArrayAgg(expression, distinct=False, filter=None, **extra)
  18. Returns a list of values, including nulls, concatenated into an array.
  19. .. attribute:: distinct
  20. .. versionadded:: 2.0
  21. An optional boolean argument that determines if array values
  22. will be distinct. Defaults to ``False``.
  23. ``BitAnd``
  24. ----------
  25. .. class:: BitAnd(expression, filter=None, **extra)
  26. Returns an ``int`` of the bitwise ``AND`` of all non-null input values, or
  27. ``None`` if all values are null.
  28. ``BitOr``
  29. ---------
  30. .. class:: BitOr(expression, filter=None, **extra)
  31. Returns an ``int`` of the bitwise ``OR`` of all non-null input values, or
  32. ``None`` if all values are null.
  33. ``BoolAnd``
  34. -----------
  35. .. class:: BoolAnd(expression, filter=None, **extra)
  36. Returns ``True``, if all input values are true, ``None`` if all values are
  37. null or if there are no values, otherwise ``False`` .
  38. ``BoolOr``
  39. ----------
  40. .. class:: BoolOr(expression, filter=None, **extra)
  41. Returns ``True`` if at least one input value is true, ``None`` if all
  42. values are null or if there are no values, otherwise ``False``.
  43. ``JSONBAgg``
  44. ------------
  45. .. class:: JSONBAgg(expressions, filter=None, **extra)
  46. .. versionadded:: 1.11
  47. Returns the input values as a ``JSON`` array. Requires PostgreSQL ≥ 9.5.
  48. ``StringAgg``
  49. -------------
  50. .. class:: StringAgg(expression, delimiter, distinct=False, filter=None)
  51. Returns the input values concatenated into a string, separated by
  52. the ``delimiter`` string.
  53. .. attribute:: delimiter
  54. Required argument. Needs to be a string.
  55. .. attribute:: distinct
  56. .. versionadded:: 1.11
  57. An optional boolean argument that determines if concatenated values
  58. will be distinct. Defaults to ``False``.
  59. Aggregate functions for statistics
  60. ==================================
  61. ``y`` and ``x``
  62. ---------------
  63. The arguments ``y`` and ``x`` for all these functions can be the name of a
  64. field or an expression returning a numeric data. Both are required.
  65. ``Corr``
  66. --------
  67. .. class:: Corr(y, x, filter=None)
  68. Returns the correlation coefficient as a ``float``, or ``None`` if there
  69. aren't any matching rows.
  70. ``CovarPop``
  71. ------------
  72. .. class:: CovarPop(y, x, sample=False, filter=None)
  73. Returns the population covariance as a ``float``, or ``None`` if there
  74. aren't any matching rows.
  75. Has one optional argument:
  76. .. attribute:: sample
  77. By default ``CovarPop`` returns the general population covariance.
  78. However, if ``sample=True``, the return value will be the sample
  79. population covariance.
  80. ``RegrAvgX``
  81. ------------
  82. .. class:: RegrAvgX(y, x, filter=None)
  83. Returns the average of the independent variable (``sum(x)/N``) as a
  84. ``float``, or ``None`` if there aren't any matching rows.
  85. ``RegrAvgY``
  86. ------------
  87. .. class:: RegrAvgY(y, x, filter=None)
  88. Returns the average of the dependent variable (``sum(y)/N``) as a
  89. ``float``, or ``None`` if there aren't any matching rows.
  90. ``RegrCount``
  91. -------------
  92. .. class:: RegrCount(y, x, filter=None)
  93. Returns an ``int`` of the number of input rows in which both expressions
  94. are not null.
  95. ``RegrIntercept``
  96. -----------------
  97. .. class:: RegrIntercept(y, x, filter=None)
  98. Returns the y-intercept of the least-squares-fit linear equation determined
  99. by the ``(x, y)`` pairs as a ``float``, or ``None`` if there aren't any
  100. matching rows.
  101. ``RegrR2``
  102. ----------
  103. .. class:: RegrR2(y, x, filter=None)
  104. Returns the square of the correlation coefficient as a ``float``, or
  105. ``None`` if there aren't any matching rows.
  106. ``RegrSlope``
  107. -------------
  108. .. class:: RegrSlope(y, x, filter=None)
  109. Returns the slope of the least-squares-fit linear equation determined
  110. by the ``(x, y)`` pairs as a ``float``, or ``None`` if there aren't any
  111. matching rows.
  112. ``RegrSXX``
  113. -----------
  114. .. class:: RegrSXX(y, x, filter=None)
  115. Returns ``sum(x^2) - sum(x)^2/N`` ("sum of squares" of the independent
  116. variable) as a ``float``, or ``None`` if there aren't any matching rows.
  117. ``RegrSXY``
  118. -----------
  119. .. class:: RegrSXY(y, x, filter=None)
  120. Returns ``sum(x*y) - sum(x) * sum(y)/N`` ("sum of products" of independent
  121. times dependent variable) as a ``float``, or ``None`` if there aren't any
  122. matching rows.
  123. ``RegrSYY``
  124. -----------
  125. .. class:: RegrSYY(y, x, filter=None)
  126. Returns ``sum(y^2) - sum(y)^2/N`` ("sum of squares" of the dependent
  127. variable) as a ``float``, or ``None`` if there aren't any matching rows.
  128. Usage examples
  129. ==============
  130. We will use this example table::
  131. | FIELD1 | FIELD2 | FIELD3 |
  132. |--------|--------|--------|
  133. | foo | 1 | 13 |
  134. | bar | 2 | (null) |
  135. | test | 3 | 13 |
  136. Here's some examples of some of the general-purpose aggregation functions::
  137. >>> TestModel.objects.aggregate(result=StringAgg('field1', delimiter=';'))
  138. {'result': 'foo;bar;test'}
  139. >>> TestModel.objects.aggregate(result=ArrayAgg('field2'))
  140. {'result': [1, 2, 3]}
  141. >>> TestModel.objects.aggregate(result=ArrayAgg('field1'))
  142. {'result': ['foo', 'bar', 'test']}
  143. The next example shows the usage of statistical aggregate functions. The
  144. underlying math will be not described (you can read about this, for example, at
  145. `wikipedia <https://en.wikipedia.org/wiki/Regression_analysis>`_)::
  146. >>> TestModel.objects.aggregate(count=RegrCount(y='field3', x='field2'))
  147. {'count': 2}
  148. >>> TestModel.objects.aggregate(avgx=RegrAvgX(y='field3', x='field2'),
  149. ... avgy=RegrAvgY(y='field3', x='field2'))
  150. {'avgx': 2, 'avgy': 13}