aggregates.txt 5.4 KB

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