aggs.asciidoc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. In case of `COUNT(*)` or `COUNT(<literal>)`, _all_ values are considered (including `null` or missing ones).
  38. In case of `COUNT(<field_name>)` `null` values are not considered.
  39. ["source","sql",subs="attributes,macros"]
  40. --------------------------------------------------
  41. include-tagged::{sql-specs}/docs.csv-spec[aggCountStar]
  42. --------------------------------------------------
  43. [[sql-functions-aggs-count-all]]
  44. ===== `COUNT(ALL)`
  45. .Synopsis:
  46. [source, sql]
  47. --------------------------------------------------
  48. COUNT(ALL field_name<1>)
  49. --------------------------------------------------
  50. *Input*:
  51. <1> a field name
  52. *Output*: numeric value
  53. .Description:
  54. Returns the total number (count) of all _non-null_ input values. `COUNT(<field_name>)` and `COUNT(ALL <field_name>)` are equivalent.
  55. ["source","sql",subs="attributes,macros"]
  56. --------------------------------------------------
  57. include-tagged::{sql-specs}/docs.csv-spec[aggCountAll]
  58. --------------------------------------------------
  59. [[sql-functions-aggs-count-distinct]]
  60. ===== `COUNT(DISTINCT)`
  61. .Synopsis:
  62. [source, sql]
  63. --------------------------------------------------
  64. COUNT(DISTINCT field_name<1>)
  65. --------------------------------------------------
  66. *Input*:
  67. <1> a field name
  68. *Output*: numeric value
  69. .Description:
  70. Returns the total number of _distinct non-null_ values in input values.
  71. ["source","sql",subs="attributes,macros"]
  72. --------------------------------------------------
  73. include-tagged::{sql-specs}/docs.csv-spec[aggCountDistinct]
  74. --------------------------------------------------
  75. [[sql-functions-aggs-max]]
  76. ===== `MAX`
  77. .Synopsis:
  78. [source, sql]
  79. --------------------------------------------------
  80. MAX(field_name<1>)
  81. --------------------------------------------------
  82. *Input*:
  83. <1> a numeric field
  84. *Output*: same type as the input
  85. .Description:
  86. Returns the maximum value across input values in the field `field_name`.
  87. ["source","sql",subs="attributes,macros"]
  88. --------------------------------------------------
  89. include-tagged::{sql-specs}/docs.csv-spec[aggMax]
  90. --------------------------------------------------
  91. [[sql-functions-aggs-min]]
  92. ===== `MIN`
  93. .Synopsis:
  94. [source, sql]
  95. --------------------------------------------------
  96. MIN(field_name<1>)
  97. --------------------------------------------------
  98. *Input*:
  99. <1> a numeric field
  100. *Output*: same type as the input
  101. .Description:
  102. Returns the minimum value across input values in the field `field_name`.
  103. ["source","sql",subs="attributes,macros"]
  104. --------------------------------------------------
  105. include-tagged::{sql-specs}/docs.csv-spec[aggMin]
  106. --------------------------------------------------
  107. [[sql-functions-aggs-sum]]
  108. ===== `SUM`
  109. .Synopsis:
  110. [source, sql]
  111. --------------------------------------------------
  112. SUM(field_name<1>)
  113. --------------------------------------------------
  114. *Input*:
  115. <1> a numeric field
  116. *Output*: `bigint` for integer input, `double` for floating points
  117. .Description:
  118. Returns the sum of input values in the field `field_name`.
  119. ["source","sql",subs="attributes,macros"]
  120. --------------------------------------------------
  121. include-tagged::{sql-specs}/docs.csv-spec[aggSum]
  122. --------------------------------------------------
  123. ==== Statistics
  124. [[sql-functions-aggs-kurtosis]]
  125. ===== `KURTOSIS`
  126. .Synopsis:
  127. [source, sql]
  128. --------------------------------------------------
  129. KURTOSIS(field_name<1>)
  130. --------------------------------------------------
  131. *Input*:
  132. <1> a numeric field
  133. *Output*: `double` numeric value
  134. .Description:
  135. https://en.wikipedia.org/wiki/Kurtosis[Quantify] the shape of the distribution of input values in the field `field_name`.
  136. ["source","sql",subs="attributes,macros"]
  137. --------------------------------------------------
  138. include-tagged::{sql-specs}/docs.csv-spec[aggKurtosis]
  139. --------------------------------------------------
  140. [[sql-functions-aggs-percentile]]
  141. ===== `PERCENTILE`
  142. .Synopsis:
  143. [source, sql]
  144. --------------------------------------------------
  145. PERCENTILE(field_name<1>, numeric_exp<2>)
  146. --------------------------------------------------
  147. *Input*:
  148. <1> a numeric field
  149. <2> a numeric expression (must be a constant and not based on a field)
  150. *Output*: `double` numeric value
  151. .Description:
  152. Returns the nth https://en.wikipedia.org/wiki/Percentile[percentile] (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[aggPercentile]
  157. --------------------------------------------------
  158. [[sql-functions-aggs-percentile-rank]]
  159. ===== `PERCENTILE_RANK`
  160. .Synopsis:
  161. [source, sql]
  162. --------------------------------------------------
  163. PERCENTILE_RANK(field_name<1>, numeric_exp<2>)
  164. --------------------------------------------------
  165. *Input*:
  166. <1> a numeric field
  167. <2> a numeric expression (must be a constant and not based on a field)
  168. *Output*: `double` numeric value
  169. .Description:
  170. Returns the nth https://en.wikipedia.org/wiki/Percentile_rank[percentile rank] (represented by `numeric_exp` parameter)
  171. of input values in the field `field_name`.
  172. ["source","sql",subs="attributes,macros"]
  173. --------------------------------------------------
  174. include-tagged::{sql-specs}/docs.csv-spec[aggPercentileRank]
  175. --------------------------------------------------
  176. [[sql-functions-aggs-skewness]]
  177. ===== `SKEWNESS`
  178. .Synopsis:
  179. [source, sql]
  180. --------------------------------------------------
  181. SKEWNESS(field_name<1>)
  182. --------------------------------------------------
  183. *Input*:
  184. <1> a numeric field
  185. *Output*: `double` numeric value
  186. .Description:
  187. https://en.wikipedia.org/wiki/Skewness[Quantify] the asymmetric distribution of input values in the field `field_name`.
  188. ["source","sql",subs="attributes,macros"]
  189. --------------------------------------------------
  190. include-tagged::{sql-specs}/docs.csv-spec[aggSkewness]
  191. --------------------------------------------------
  192. [[sql-functions-aggs-stddev-pop]]
  193. ===== `STDDEV_POP`
  194. .Synopsis:
  195. [source, sql]
  196. --------------------------------------------------
  197. STDDEV_POP(field_name<1>)
  198. --------------------------------------------------
  199. *Input*:
  200. <1> a numeric field
  201. *Output*: `double` numeric value
  202. .Description:
  203. Returns the https://en.wikipedia.org/wiki/Standard_deviations[population standard deviation] of input values in the field `field_name`.
  204. ["source","sql",subs="attributes,macros"]
  205. --------------------------------------------------
  206. include-tagged::{sql-specs}/docs.csv-spec[aggStddevPop]
  207. --------------------------------------------------
  208. [[sql-functions-aggs-sum-squares]]
  209. ===== `SUM_OF_SQUARES`
  210. .Synopsis:
  211. [source, sql]
  212. --------------------------------------------------
  213. SUM_OF_SQUARES(field_name<1>)
  214. --------------------------------------------------
  215. *Input*:
  216. <1> a numeric field
  217. *Output*: `double` numeric value
  218. .Description:
  219. Returns the https://en.wikipedia.org/wiki/Total_sum_of_squares[sum of squares] of input values in the field `field_name`.
  220. ["source","sql",subs="attributes,macros"]
  221. --------------------------------------------------
  222. include-tagged::{sql-specs}/docs.csv-spec[aggSumOfSquares]
  223. --------------------------------------------------
  224. [[sql-functions-aggs-var-pop]]
  225. ===== `VAR_POP`
  226. .Synopsis:
  227. [source, sql]
  228. --------------------------------------------------
  229. VAR_POP(field_name<1>)
  230. --------------------------------------------------
  231. *Input*:
  232. <1> a numeric field
  233. *Output*: `double` numeric value
  234. .Description:
  235. Returns the https://en.wikipedia.org/wiki/Variance[population variance] of input values in the field `field_name`.
  236. ["source","sql",subs="attributes,macros"]
  237. --------------------------------------------------
  238. include-tagged::{sql-specs}/docs.csv-spec[aggVarPop]
  239. --------------------------------------------------