aggregates.txt 5.8 KB

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