esql-processing-commands.asciidoc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. [[esql-processing-commands]]
  2. == ESQL processing commands
  3. Processing commands change an input table by adding, removing, or changing rows
  4. and columns.
  5. image::images/esql/processing-command.svg[A processing command changes an input table,align="center"]
  6. [discrete]
  7. [[esql-limit]]
  8. === `LIMIT`
  9. The `LIMIT` processing command enables you to limit the number of rows:
  10. [source,esql]
  11. ----
  12. FROM employees
  13. | LIMIT 5
  14. ----
  15. [discrete]
  16. [[esql-project]]
  17. === `PROJECT`
  18. The `PROJECT` command enables you to change:
  19. * the columns that are returned,
  20. * the order in which they are returned,
  21. * and the name with which they are returned.
  22. To limit the columns that are returned, use a comma-separated list of column
  23. names. The columns are returned in the specified order:
  24. [source,esql]
  25. ----
  26. FROM employees
  27. | PROJECT first_name, last_name, height
  28. ----
  29. Rather than specify each column by name, you can use wildcards to return all
  30. columns with a name that matches a pattern:
  31. [source,esql]
  32. ----
  33. FROM employees
  34. | PROJECT h*
  35. ----
  36. The asterisk wildcard (`*`) by itself translates to all columns that do not
  37. match the other arguments. This query will first return all columns with a name
  38. that starts with an h, followed by all other columns:
  39. [source,esql]
  40. ----
  41. FROM employees
  42. | PROJECT h*, *
  43. ----
  44. Use a dash to specify columns you do not want returned:
  45. [source,esql]
  46. ----
  47. FROM employees
  48. | PROJECT -h*
  49. ----
  50. Use `=` to rename columns:
  51. [source,esql]
  52. ----
  53. FROM employees
  54. | PROJECT current_employee = still_hired, *
  55. ----
  56. [discrete]
  57. [[esql-eval]]
  58. === `EVAL`
  59. `EVAL` enables you to add new columns to the end of the table:
  60. [source,esql]
  61. ----
  62. FROM employees
  63. | PROJECT first_name, last_name, height
  64. | EVAL height_feet = height * 3.281, height_cm = height * 100
  65. ----
  66. If the specified column already exists, the existing column will be dropped, and
  67. the new column will be appended to the table:
  68. [source,esql]
  69. ----
  70. FROM employees
  71. | PROJECT first_name, last_name, height
  72. | EVAL height = height * 3.281
  73. ----
  74. [discrete]
  75. ==== Functions
  76. `EVAL` supports various functions for calculating values. Refer to
  77. <<esql-functions,Functions>> for more information.
  78. [discrete]
  79. [[esql-sort]]
  80. === `SORT`
  81. Use the `SORT` command to sort rows on one or more fields:
  82. [source,esql]
  83. ----
  84. FROM employees
  85. | PROJECT first_name, last_name, height
  86. | SORT height
  87. ----
  88. The default sort order is ascending. Set an explicit sort order using `ASC` or
  89. `DESC`:
  90. [source,esql]
  91. ----
  92. FROM employees
  93. | PROJECT first_name, last_name, height
  94. | SORT height desc
  95. ----
  96. If two rows have the same sort key, the original order will be preserved. You
  97. can provide additional sort expressions to act as tie breakers:
  98. [source,esql]
  99. ----
  100. FROM employees
  101. | PROJECT first_name, last_name, height
  102. | SORT height desc, first_name ASC
  103. ----
  104. [discrete]
  105. ==== `null` values
  106. By default, `null` values are treated as being larger than any other value. With
  107. an ascending sort order, `null` values are sorted last, and with a descending
  108. sort order, `null` values are sorted first. You can change that by providing
  109. `NULLS FIRST` or `NULLS LAST`:
  110. [source,esql]
  111. ----
  112. FROM employees
  113. | PROJECT first_name, last_name, height
  114. | SORT first_name ASC NULLS FIRST
  115. ----
  116. [discrete]
  117. [[esql-where]]
  118. === `WHERE`
  119. Use `WHERE` to produce a table that contains all the rows from the input table
  120. for which the provided condition evaluates to `true`:
  121. [source,esql]
  122. ----
  123. FROM employees
  124. | PROJECT first_name, last_name, still_hired
  125. | WHERE still_hired == true
  126. ----
  127. Which, because `still_hired` is a boolean field, can be simplified to:
  128. [source,esql]
  129. ----
  130. FROM employees
  131. | PROJECT first_name, last_name, still_hired
  132. | WHERE still_hired
  133. ----
  134. [discrete]
  135. ==== Operators
  136. These comparison operators are supported:
  137. * equality: `==`
  138. * inequality: `!=`
  139. * comparison:
  140. ** less than: `<`
  141. ** less than or equal: `<=`
  142. ** larger than: `>`
  143. ** larger than or equal: `>=`
  144. You can use the following boolean operators:
  145. * `AND`
  146. * `OR`
  147. * `NOT`
  148. [source,esql]
  149. ----
  150. FROM employees
  151. | PROJECT first_name, last_name, height, still_hired
  152. | WHERE height > 2 AND NOT still_hired
  153. ----
  154. [discrete]
  155. ==== Functions
  156. `WHERE` supports various functions for calculating values. Refer to
  157. <<esql-functions,Functions>> for more information.
  158. [source,esql]
  159. ----
  160. FROM employees
  161. | PROJECT first_name, last_name, height
  162. | WHERE length(first_name) < 4
  163. ----
  164. [discrete]
  165. [[esql-stats-by]]
  166. === `STATS ... BY`
  167. Use `STATS ... BY` to group rows according to a common value and calculate one
  168. or more aggregated values over the grouped rows.
  169. [source,esql]
  170. ----
  171. FROM employees
  172. | STATS count = COUNT(languages) BY languages
  173. ----
  174. If `BY` is omitted, the output table contains exactly one row with the
  175. aggregations applied over the entire dataset:
  176. [source,esql]
  177. ----
  178. FROM employees
  179. | STATS avg_lang = AVG(languages)
  180. ----
  181. It's possible to calculate multiple values:
  182. [source,esql]
  183. ----
  184. FROM employees
  185. | STATS avg_lang = AVG(languages), max_lang = MAX(languages)
  186. ----
  187. It's also possible to group by multiple values (only supported for long and
  188. keyword family fields):
  189. [source,esql]
  190. ----
  191. FROM employees
  192. | EVAL hired = DATE_FORMAT(hire_date, "YYYY")
  193. | STATS avg_salary = AVG(salary) BY hired, languages.long
  194. | EVAL avg_salary = ROUND(avg_salary)
  195. | SORT hired, languages.long
  196. ----
  197. The following aggregation functions are supported:
  198. * `AVG`
  199. * `COUNT`
  200. * `MAX`
  201. * `MEDIAN`
  202. * `MEDIAN_ABSOLUTE_DEVIATION`
  203. * `MIN`
  204. * `SUM`