aggregates.txt 6.1 KB

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