aggs.asciidoc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[sql-functions-aggs]]
  4. === Aggregate Functions
  5. beta[]
  6. Functions for computing a _single_ result from a set of input values.
  7. {es-sql} supports aggregate functions only alongside <<sql-syntax-group-by,grouping>> (implicit or explicit).
  8. ==== General Purpose
  9. [[sql-functions-aggs-avg]]
  10. ===== `AVG`
  11. .Synopsis:
  12. [source, sql]
  13. --------------------------------------------------
  14. AVG(numeric_field<1>)
  15. --------------------------------------------------
  16. *Input*:
  17. <1> numeric field
  18. *Output*: `double` numeric value
  19. .Description:
  20. Returns the https://en.wikipedia.org/wiki/Arithmetic_mean[Average] (arithmetic mean) of input values.
  21. ["source","sql",subs="attributes,macros"]
  22. --------------------------------------------------
  23. include-tagged::{sql-specs}/docs.csv-spec[aggAvg]
  24. --------------------------------------------------
  25. [[sql-functions-aggs-count]]
  26. ===== `COUNT`
  27. .Synopsis:
  28. [source, sql]
  29. --------------------------------------------------
  30. COUNT(expression<1>)
  31. --------------------------------------------------
  32. *Input*:
  33. <1> a field name, wildcard (`*`) or any numeric value
  34. *Output*: numeric value
  35. .Description:
  36. Returns the total number (count) of input values.
  37. ["source","sql",subs="attributes,macros"]
  38. --------------------------------------------------
  39. include-tagged::{sql-specs}/docs.csv-spec[aggCountStar]
  40. --------------------------------------------------
  41. [[sql-functions-aggs-count-distinct]]
  42. ===== `COUNT(DISTINCT)`
  43. .Synopsis:
  44. [source, sql]
  45. --------------------------------------------------
  46. COUNT(DISTINCT field_name<1>)
  47. --------------------------------------------------
  48. *Input*:
  49. <1> a field name
  50. *Output*: numeric value
  51. .Description:
  52. Returns the total number of _distinct_ values in input values.
  53. ["source","sql",subs="attributes,macros"]
  54. --------------------------------------------------
  55. include-tagged::{sql-specs}/docs.csv-spec[aggCountDistinct]
  56. --------------------------------------------------
  57. [[sql-functions-aggs-max]]
  58. ===== `MAX`
  59. .Synopsis:
  60. [source, sql]
  61. --------------------------------------------------
  62. MAX(field_name<1>)
  63. --------------------------------------------------
  64. *Input*:
  65. <1> a numeric field
  66. *Output*: same type as the input
  67. .Description:
  68. Returns the maximum value across input values in the field `field_name`.
  69. ["source","sql",subs="attributes,macros"]
  70. --------------------------------------------------
  71. include-tagged::{sql-specs}/docs.csv-spec[aggMax]
  72. --------------------------------------------------
  73. [[sql-functions-aggs-min]]
  74. ===== `MIN`
  75. .Synopsis:
  76. [source, sql]
  77. --------------------------------------------------
  78. MIN(field_name<1>)
  79. --------------------------------------------------
  80. *Input*:
  81. <1> a numeric field
  82. *Output*: same type as the input
  83. .Description:
  84. Returns the minimum value across input values in the field `field_name`.
  85. ["source","sql",subs="attributes,macros"]
  86. --------------------------------------------------
  87. include-tagged::{sql-specs}/docs.csv-spec[aggMin]
  88. --------------------------------------------------
  89. [[sql-functions-aggs-sum]]
  90. ===== `SUM`
  91. .Synopsis:
  92. [source, sql]
  93. --------------------------------------------------
  94. SUM(field_name<1>)
  95. --------------------------------------------------
  96. *Input*:
  97. <1> a numeric field
  98. *Output*: `bigint` for integer input, `double` for floating points
  99. .Description:
  100. Returns the sum of input values in the field `field_name`.
  101. ["source","sql",subs="attributes,macros"]
  102. --------------------------------------------------
  103. include-tagged::{sql-specs}/docs.csv-spec[aggSum]
  104. --------------------------------------------------
  105. ==== Statistics
  106. [[sql-functions-aggs-kurtosis]]
  107. ===== `KURTOSIS`
  108. .Synopsis:
  109. [source, sql]
  110. --------------------------------------------------
  111. KURTOSIS(field_name<1>)
  112. --------------------------------------------------
  113. *Input*:
  114. <1> a numeric field
  115. *Output*: `double` numeric value
  116. .Description:
  117. https://en.wikipedia.org/wiki/Kurtosis[Quantify] the shape of the distribution of input values in the field `field_name`.
  118. ["source","sql",subs="attributes,macros"]
  119. --------------------------------------------------
  120. include-tagged::{sql-specs}/docs.csv-spec[aggKurtosis]
  121. --------------------------------------------------
  122. [[sql-functions-aggs-percentile]]
  123. ===== `PERCENTILE`
  124. .Synopsis:
  125. [source, sql]
  126. --------------------------------------------------
  127. PERCENTILE(field_name<1>, numeric_exp<2>)
  128. --------------------------------------------------
  129. *Input*:
  130. <1> a numeric field
  131. <2> a numeric expression
  132. *Output*: `double` numeric value
  133. .Description:
  134. Returns the nth https://en.wikipedia.org/wiki/Percentile[percentile] (represented by `numeric_exp` parameter)
  135. of input values in the field `field_name`.
  136. ["source","sql",subs="attributes,macros"]
  137. --------------------------------------------------
  138. include-tagged::{sql-specs}/docs.csv-spec[aggPercentile]
  139. --------------------------------------------------
  140. [[sql-functions-aggs-percentile-rank]]
  141. ===== `PERCENTILE_RANK`
  142. .Synopsis:
  143. [source, sql]
  144. --------------------------------------------------
  145. PERCENTILE_RANK(field_name<1>, numeric_exp<2>)
  146. --------------------------------------------------
  147. *Input*:
  148. <1> a numeric field
  149. <2> a numeric expression
  150. *Output*: `double` numeric value
  151. .Description:
  152. Returns the nth https://en.wikipedia.org/wiki/Percentile_rank[percentile rank] (represented by `numeric_exp` parameter)
  153. of input values in the field `field_name`.
  154. ["source","sql",subs="attributes,macros"]
  155. --------------------------------------------------
  156. include-tagged::{sql-specs}/docs.csv-spec[aggPercentileRank]
  157. --------------------------------------------------
  158. [[sql-functions-aggs-skewness]]
  159. ===== `SKEWNESS`
  160. .Synopsis:
  161. [source, sql]
  162. --------------------------------------------------
  163. SKEWNESS(field_name<1>)
  164. --------------------------------------------------
  165. *Input*:
  166. <1> a numeric field
  167. *Output*: `double` numeric value
  168. .Description:
  169. https://en.wikipedia.org/wiki/Skewness[Quantify] the asymmetric distribution of input values in the field `field_name`.
  170. ["source","sql",subs="attributes,macros"]
  171. --------------------------------------------------
  172. include-tagged::{sql-specs}/docs.csv-spec[aggSkewness]
  173. --------------------------------------------------
  174. [[sql-functions-aggs-stddev-pop]]
  175. ===== `STDDEV_POP`
  176. .Synopsis:
  177. [source, sql]
  178. --------------------------------------------------
  179. STDDEV_POP(field_name<1>)
  180. --------------------------------------------------
  181. *Input*:
  182. <1> a numeric field
  183. *Output*: `double` numeric value
  184. .Description:
  185. Returns the https://en.wikipedia.org/wiki/Standard_deviations[population standard deviation] of input values in the field `field_name`.
  186. ["source","sql",subs="attributes,macros"]
  187. --------------------------------------------------
  188. include-tagged::{sql-specs}/docs.csv-spec[aggStddevPop]
  189. --------------------------------------------------
  190. [[sql-functions-aggs-sum-squares]]
  191. ===== `SUM_OF_SQUARES`
  192. .Synopsis:
  193. [source, sql]
  194. --------------------------------------------------
  195. SUM_OF_SQUARES(field_name<1>)
  196. --------------------------------------------------
  197. *Input*:
  198. <1> a numeric field
  199. *Output*: `double` numeric value
  200. .Description:
  201. Returns the https://en.wikipedia.org/wiki/Total_sum_of_squares[sum of squares] of input values in the field `field_name`.
  202. ["source","sql",subs="attributes,macros"]
  203. --------------------------------------------------
  204. include-tagged::{sql-specs}/docs.csv-spec[aggSumOfSquares]
  205. --------------------------------------------------
  206. [[sql-functions-aggs-var-pop]]
  207. ===== `VAR_POP`
  208. .Synopsis:
  209. [source, sql]
  210. --------------------------------------------------
  211. VAR_POP(field_name<1>)
  212. --------------------------------------------------
  213. *Input*:
  214. <1> a numeric field
  215. *Output*: `double` numeric value
  216. .Description:
  217. Returns the https://en.wikipedia.org/wiki/Variance[population variance] of input values in the field `field_name`.
  218. ["source","sql",subs="attributes,macros"]
  219. --------------------------------------------------
  220. include-tagged::{sql-specs}/docs.csv-spec[aggVarPop]
  221. --------------------------------------------------