aggs.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[sql-functions-aggs]]
  4. === Aggregate Functions
  5. Functions for computing a _single_ result from a set of input values.
  6. {es-sql} supports aggregate functions only alongside <<sql-syntax-group-by,grouping>> (implicit or explicit).
  7. ==== General Purpose
  8. [[sql-functions-aggs-avg]]
  9. ===== `AVG`
  10. .Synopsis:
  11. [source, sql]
  12. --------------------------------------------------
  13. AVG(numeric_field<1>)
  14. --------------------------------------------------
  15. *Input*:
  16. <1> numeric field
  17. *Output*: `double` numeric value
  18. .Description:
  19. Returns the https://en.wikipedia.org/wiki/Arithmetic_mean[Average] (arithmetic mean) of input values.
  20. ["source","sql",subs="attributes,macros"]
  21. --------------------------------------------------
  22. include-tagged::{sql-specs}/docs.csv-spec[aggAvg]
  23. --------------------------------------------------
  24. [[sql-functions-aggs-count]]
  25. ===== `COUNT`
  26. .Synopsis:
  27. [source, sql]
  28. --------------------------------------------------
  29. COUNT(expression<1>)
  30. --------------------------------------------------
  31. *Input*:
  32. <1> a field name, wildcard (`*`) or any numeric value
  33. *Output*: numeric value
  34. .Description:
  35. Returns the total number (count) of input values.
  36. In case of `COUNT(*)` or `COUNT(<literal>)`, _all_ values are considered (including `null` or missing ones).
  37. In case of `COUNT(<field_name>)` `null` values are not considered.
  38. ["source","sql",subs="attributes,macros"]
  39. --------------------------------------------------
  40. include-tagged::{sql-specs}/docs.csv-spec[aggCountStar]
  41. --------------------------------------------------
  42. [[sql-functions-aggs-count-all]]
  43. ===== `COUNT(ALL)`
  44. .Synopsis:
  45. [source, sql]
  46. --------------------------------------------------
  47. COUNT(ALL field_name<1>)
  48. --------------------------------------------------
  49. *Input*:
  50. <1> a field name
  51. *Output*: numeric value
  52. .Description:
  53. Returns the total number (count) of all _non-null_ input values. `COUNT(<field_name>)` and `COUNT(ALL <field_name>)` are equivalent.
  54. ["source","sql",subs="attributes,macros"]
  55. --------------------------------------------------
  56. include-tagged::{sql-specs}/docs.csv-spec[aggCountAll]
  57. --------------------------------------------------
  58. [[sql-functions-aggs-count-distinct]]
  59. ===== `COUNT(DISTINCT)`
  60. .Synopsis:
  61. [source, sql]
  62. --------------------------------------------------
  63. COUNT(DISTINCT field_name<1>)
  64. --------------------------------------------------
  65. *Input*:
  66. <1> a field name
  67. *Output*: numeric value
  68. .Description:
  69. Returns the total number of _distinct non-null_ values in input values.
  70. ["source","sql",subs="attributes,macros"]
  71. --------------------------------------------------
  72. include-tagged::{sql-specs}/docs.csv-spec[aggCountDistinct]
  73. --------------------------------------------------
  74. [[sql-functions-aggs-first]]
  75. ===== `FIRST/FIRST_VALUE`
  76. .Synopsis:
  77. [source, sql]
  78. ----------------------------------------------
  79. FIRST(field_name<1>[, ordering_field_name]<2>)
  80. ----------------------------------------------
  81. *Input*:
  82. <1> target field for the aggregation
  83. <2> optional field used for ordering
  84. *Output*: same type as the input
  85. .Description:
  86. Returns the first **non-NULL** value (if such exists) of the `field_name` input column sorted by
  87. the `ordering_field_name` column. If `ordering_field_name` is not provided, only the `field_name`
  88. column is used for the sorting. E.g.:
  89. [cols="<,<"]
  90. |===
  91. s| a | b
  92. | 100 | 1
  93. | 200 | 1
  94. | 1 | 2
  95. | 2 | 2
  96. | 10 | null
  97. | 20 | null
  98. | null | null
  99. |===
  100. [source, sql]
  101. ----------------------
  102. SELECT FIRST(a) FROM t
  103. ----------------------
  104. will result in:
  105. [cols="<"]
  106. |===
  107. s| FIRST(a)
  108. | 1
  109. |===
  110. and
  111. [source, sql]
  112. -------------------------
  113. SELECT FIRST(a, b) FROM t
  114. -------------------------
  115. will result in:
  116. [cols="<"]
  117. |===
  118. s| FIRST(a, b)
  119. | 100
  120. |===
  121. ["source","sql",subs="attributes,macros"]
  122. -----------------------------------------------------------
  123. include-tagged::{sql-specs}/docs.csv-spec[firstWithOneArg]
  124. -----------------------------------------------------------
  125. ["source","sql",subs="attributes,macros"]
  126. --------------------------------------------------------------------
  127. include-tagged::{sql-specs}/docs.csv-spec[firstWithOneArgAndGroupBy]
  128. --------------------------------------------------------------------
  129. ["source","sql",subs="attributes,macros"]
  130. -----------------------------------------------------------
  131. include-tagged::{sql-specs}/docs.csv-spec[firstWithTwoArgs]
  132. -----------------------------------------------------------
  133. ["source","sql",subs="attributes,macros"]
  134. ---------------------------------------------------------------------
  135. include-tagged::{sql-specs}/docs.csv-spec[firstWithTwoArgsAndGroupBy]
  136. ---------------------------------------------------------------------
  137. `FIRST_VALUE` is a name alias and can be used instead of `FIRST`, e.g.:
  138. ["source","sql",subs="attributes,macros"]
  139. --------------------------------------------------------------------------
  140. include-tagged::{sql-specs}/docs.csv-spec[firstValueWithTwoArgsAndGroupBy]
  141. --------------------------------------------------------------------------
  142. [NOTE]
  143. `FIRST` cannot be used in a HAVING clause.
  144. [NOTE]
  145. `FIRST` cannot be used with columns of type <<text, `text`>> unless
  146. the field is also <<before-enabling-fielddata,saved as a keyword>>.
  147. [[sql-functions-aggs-last]]
  148. ===== `LAST/LAST_VALUE`
  149. .Synopsis:
  150. [source, sql]
  151. --------------------------------------------------
  152. LAST(field_name<1>[, ordering_field_name]<2>)
  153. --------------------------------------------------
  154. *Input*:
  155. <1> target field for the aggregation
  156. <2> optional field used for ordering
  157. *Output*: same type as the input
  158. .Description:
  159. It's the inverse of <<sql-functions-aggs-first>>. Returns the last **non-NULL** value (if such exists) of the
  160. `field_name`input column sorted descending by the `ordering_field_name` column. If `ordering_field_name` is not
  161. provided, only the `field_name` column is used for the sorting. E.g.:
  162. [cols="<,<"]
  163. |===
  164. s| a | b
  165. | 10 | 1
  166. | 20 | 1
  167. | 1 | 2
  168. | 2 | 2
  169. | 100 | null
  170. | 200 | null
  171. | null | null
  172. |===
  173. [source, sql]
  174. ------------------------
  175. SELECT LAST(a) FROM t
  176. ------------------------
  177. will result in:
  178. [cols="<"]
  179. |===
  180. s| LAST(a)
  181. | 200
  182. |===
  183. and
  184. [source, sql]
  185. ------------------------
  186. SELECT LAST(a, b) FROM t
  187. ------------------------
  188. will result in:
  189. [cols="<"]
  190. |===
  191. s| LAST(a, b)
  192. | 2
  193. |===
  194. ["source","sql",subs="attributes,macros"]
  195. -----------------------------------------------------------
  196. include-tagged::{sql-specs}/docs.csv-spec[lastWithOneArg]
  197. -----------------------------------------------------------
  198. ["source","sql",subs="attributes,macros"]
  199. -------------------------------------------------------------------
  200. include-tagged::{sql-specs}/docs.csv-spec[lastWithOneArgAndGroupBy]
  201. -------------------------------------------------------------------
  202. ["source","sql",subs="attributes,macros"]
  203. -----------------------------------------------------------
  204. include-tagged::{sql-specs}/docs.csv-spec[lastWithTwoArgs]
  205. -----------------------------------------------------------
  206. ["source","sql",subs="attributes,macros"]
  207. --------------------------------------------------------------------
  208. include-tagged::{sql-specs}/docs.csv-spec[lastWithTwoArgsAndGroupBy]
  209. --------------------------------------------------------------------
  210. `LAST_VALUE` is a name alias and can be used instead of `LAST`, e.g.:
  211. ["source","sql",subs="attributes,macros"]
  212. -------------------------------------------------------------------------
  213. include-tagged::{sql-specs}/docs.csv-spec[lastValueWithTwoArgsAndGroupBy]
  214. -------------------------------------------------------------------------
  215. [NOTE]
  216. `LAST` cannot be used in `HAVING` clause.
  217. [NOTE]
  218. `LAST` cannot be used with columns of type <<text, `text`>> unless
  219. the field is also <<before-enabling-fielddata,`saved as a keyword`>>.
  220. [[sql-functions-aggs-max]]
  221. ===== `MAX`
  222. .Synopsis:
  223. [source, sql]
  224. --------------------------------------------------
  225. MAX(field_name<1>)
  226. --------------------------------------------------
  227. *Input*:
  228. <1> a numeric field
  229. *Output*: same type as the input
  230. .Description:
  231. Returns the maximum value across input values in the field `field_name`.
  232. ["source","sql",subs="attributes,macros"]
  233. --------------------------------------------------
  234. include-tagged::{sql-specs}/docs.csv-spec[aggMax]
  235. --------------------------------------------------
  236. [NOTE]
  237. `MAX` on a field of type <<text, `text`>> or <<keyword, `keyword`>> is translated into
  238. <<sql-functions-aggs-last>> and therefore, it cannot be used in `HAVING` clause.
  239. [[sql-functions-aggs-min]]
  240. ===== `MIN`
  241. .Synopsis:
  242. [source, sql]
  243. --------------------------------------------------
  244. MIN(field_name<1>)
  245. --------------------------------------------------
  246. *Input*:
  247. <1> a numeric field
  248. *Output*: same type as the input
  249. .Description:
  250. Returns the minimum value across input values in the field `field_name`.
  251. ["source","sql",subs="attributes,macros"]
  252. --------------------------------------------------
  253. include-tagged::{sql-specs}/docs.csv-spec[aggMin]
  254. --------------------------------------------------
  255. [NOTE]
  256. `MIN` on a field of type <<text, `text`>> or <<keyword, `keyword`>> is translated into
  257. <<sql-functions-aggs-first>> and therefore, it cannot be used in `HAVING` clause.
  258. [[sql-functions-aggs-sum]]
  259. ===== `SUM`
  260. .Synopsis:
  261. [source, sql]
  262. --------------------------------------------------
  263. SUM(field_name<1>)
  264. --------------------------------------------------
  265. *Input*:
  266. <1> a numeric field
  267. *Output*: `bigint` for integer input, `double` for floating points
  268. .Description:
  269. Returns the sum of input values in the field `field_name`.
  270. ["source","sql",subs="attributes,macros"]
  271. --------------------------------------------------
  272. include-tagged::{sql-specs}/docs.csv-spec[aggSum]
  273. --------------------------------------------------
  274. ==== Statistics
  275. [[sql-functions-aggs-kurtosis]]
  276. ===== `KURTOSIS`
  277. .Synopsis:
  278. [source, sql]
  279. --------------------------------------------------
  280. KURTOSIS(field_name<1>)
  281. --------------------------------------------------
  282. *Input*:
  283. <1> a numeric field
  284. *Output*: `double` numeric value
  285. .Description:
  286. https://en.wikipedia.org/wiki/Kurtosis[Quantify] the shape of the distribution of input values in the field `field_name`.
  287. ["source","sql",subs="attributes,macros"]
  288. --------------------------------------------------
  289. include-tagged::{sql-specs}/docs.csv-spec[aggKurtosis]
  290. --------------------------------------------------
  291. [[sql-functions-aggs-percentile]]
  292. ===== `PERCENTILE`
  293. .Synopsis:
  294. [source, sql]
  295. --------------------------------------------------
  296. PERCENTILE(field_name<1>, numeric_exp<2>)
  297. --------------------------------------------------
  298. *Input*:
  299. <1> a numeric field
  300. <2> a numeric expression (must be a constant and not based on a field)
  301. *Output*: `double` numeric value
  302. .Description:
  303. Returns the nth https://en.wikipedia.org/wiki/Percentile[percentile] (represented by `numeric_exp` parameter)
  304. of input values in the field `field_name`.
  305. ["source","sql",subs="attributes,macros"]
  306. --------------------------------------------------
  307. include-tagged::{sql-specs}/docs.csv-spec[aggPercentile]
  308. --------------------------------------------------
  309. [[sql-functions-aggs-percentile-rank]]
  310. ===== `PERCENTILE_RANK`
  311. .Synopsis:
  312. [source, sql]
  313. --------------------------------------------------
  314. PERCENTILE_RANK(field_name<1>, numeric_exp<2>)
  315. --------------------------------------------------
  316. *Input*:
  317. <1> a numeric field
  318. <2> a numeric expression (must be a constant and not based on a field)
  319. *Output*: `double` numeric value
  320. .Description:
  321. Returns the nth https://en.wikipedia.org/wiki/Percentile_rank[percentile rank] (represented by `numeric_exp` parameter)
  322. of input values in the field `field_name`.
  323. ["source","sql",subs="attributes,macros"]
  324. --------------------------------------------------
  325. include-tagged::{sql-specs}/docs.csv-spec[aggPercentileRank]
  326. --------------------------------------------------
  327. [[sql-functions-aggs-skewness]]
  328. ===== `SKEWNESS`
  329. .Synopsis:
  330. [source, sql]
  331. --------------------------------------------------
  332. SKEWNESS(field_name<1>)
  333. --------------------------------------------------
  334. *Input*:
  335. <1> a numeric field
  336. *Output*: `double` numeric value
  337. .Description:
  338. https://en.wikipedia.org/wiki/Skewness[Quantify] the asymmetric distribution of input values in the field `field_name`.
  339. ["source","sql",subs="attributes,macros"]
  340. --------------------------------------------------
  341. include-tagged::{sql-specs}/docs.csv-spec[aggSkewness]
  342. --------------------------------------------------
  343. [[sql-functions-aggs-stddev-pop]]
  344. ===== `STDDEV_POP`
  345. .Synopsis:
  346. [source, sql]
  347. --------------------------------------------------
  348. STDDEV_POP(field_name<1>)
  349. --------------------------------------------------
  350. *Input*:
  351. <1> a numeric field
  352. *Output*: `double` numeric value
  353. .Description:
  354. Returns the https://en.wikipedia.org/wiki/Standard_deviations[population standard deviation] of input values in the field `field_name`.
  355. ["source","sql",subs="attributes,macros"]
  356. --------------------------------------------------
  357. include-tagged::{sql-specs}/docs.csv-spec[aggStddevPop]
  358. --------------------------------------------------
  359. [[sql-functions-aggs-sum-squares]]
  360. ===== `SUM_OF_SQUARES`
  361. .Synopsis:
  362. [source, sql]
  363. --------------------------------------------------
  364. SUM_OF_SQUARES(field_name<1>)
  365. --------------------------------------------------
  366. *Input*:
  367. <1> a numeric field
  368. *Output*: `double` numeric value
  369. .Description:
  370. Returns the https://en.wikipedia.org/wiki/Total_sum_of_squares[sum of squares] of input values in the field `field_name`.
  371. ["source","sql",subs="attributes,macros"]
  372. --------------------------------------------------
  373. include-tagged::{sql-specs}/docs.csv-spec[aggSumOfSquares]
  374. --------------------------------------------------
  375. [[sql-functions-aggs-var-pop]]
  376. ===== `VAR_POP`
  377. .Synopsis:
  378. [source, sql]
  379. --------------------------------------------------
  380. VAR_POP(field_name<1>)
  381. --------------------------------------------------
  382. *Input*:
  383. <1> a numeric field
  384. *Output*: `double` numeric value
  385. .Description:
  386. Returns the https://en.wikipedia.org/wiki/Variance[population variance] of input values in the field `field_name`.
  387. ["source","sql",subs="attributes,macros"]
  388. --------------------------------------------------
  389. include-tagged::{sql-specs}/docs.csv-spec[aggVarPop]
  390. --------------------------------------------------