aggregates.txt 5.4 KB

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