time-spans.asciidoc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. [[esql-time-spans]]
  2. === {esql} time spans
  3. ++++
  4. <titleabbrev>Time spans</titleabbrev>
  5. ++++
  6. Time spans represent intervals between two datetime values. There are currently two supported types of time spans:
  7. * `DATE_PERIOD` specifies intervals in years, quarters, months, weeks and days
  8. * `TIME_DURATION` specifies intervals in hours, minutes, seconds and milliseconds
  9. A time span requires two elements: an integer value and a temporal unit.
  10. Time spans work with grouping functions such as <<esql-bucket, BUCKET>>, scalar functions such as <<esql-date_trunc, DATE_TRUNC>> and arithmetic operators such as <<esql-add, `+`>> and <<esql-subtract, `-`>>. Convert strings to time spans using <<esql-to_dateperiod, TO_DATEPERIOD>>, <<esql-to_timeduration, TO_TIMEDURATION>>, or the cast operators `::DATE_PERIOD`, `::TIME_DURATION`.
  11. [discrete]
  12. [[esql-time-spans-examples]]
  13. ==== Examples of using time spans in {esql}
  14. With `BUCKET`:
  15. [source.merge.styled,esql]
  16. ----
  17. include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogramWithSpan]
  18. ----
  19. [%header.monospaced.styled,format=dsv,separator=|]
  20. |===
  21. include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogramWithSpan-result]
  22. |===
  23. With `DATE_TRUNC`:
  24. [source.merge.styled,esql]
  25. ----
  26. include::{esql-specs}/date.csv-spec[tag=docsDateTrunc]
  27. ----
  28. [%header.monospaced.styled,format=dsv,separator=|]
  29. |===
  30. include::{esql-specs}/date.csv-spec[tag=docsDateTrunc-result]
  31. |===
  32. With `+` and/or `-`:
  33. [source.merge.styled,esql]
  34. ----
  35. include::{esql-specs}/date.csv-spec[tag=docsNowWhere]
  36. ----
  37. [%header.monospaced.styled,format=dsv,separator=|]
  38. |===
  39. include::{esql-specs}/date.csv-spec[tag=docsNowWhere-result]
  40. |===
  41. When a time span is provided as a named parameter in string format, `TO_DATEPERIOD`, `::DATE_PERIOD`, `TO_TIMEDURATION` or `::TIME_DURATION` can be used to convert to its corresponding time span value for arithmetic operations like `+` and/or `-`.
  42. [source, esql]
  43. ----
  44. POST /_query
  45. {
  46. "query": """
  47. FROM employees
  48. | EVAL x = hire_date + ?timespan::DATE_PERIOD, y = hire_date - TO_DATEPERIOD(?timespan)
  49. """,
  50. "params": [{"timespan" : "1 day"}]
  51. }
  52. ----
  53. When a time span is provided as a named parameter in string format, it can be automatically converted to its corresponding time span value in grouping functions and scalar functions, like `BUCKET` and `DATE_TRUNC`.
  54. [source, esql]
  55. ----
  56. POST /_query
  57. {
  58. "query": """
  59. FROM employees
  60. | WHERE hire_date >= "1985-01-01T00:00:00Z" AND hire_date < "1986-01-01T00:00:00Z"
  61. | STATS hires_per_week = COUNT(*) BY week = BUCKET(hire_date, ?timespan)
  62. | SORT week
  63. """,
  64. "params": [{"timespan" : "1 week"}]
  65. }
  66. ----
  67. [source, esql]
  68. ----
  69. POST /_query
  70. {
  71. "query": """
  72. FROM employees
  73. | KEEP first_name, last_name, hire_date
  74. | EVAL year_hired = DATE_TRUNC(?timespan, hire_date)
  75. """,
  76. "params": [{"timespan" : "1 year"}]
  77. }
  78. ----
  79. [discrete]
  80. [[esql-time-spans-table]]
  81. ==== Supported temporal units
  82. [%header.monospaced.styled,format=dsv,separator=|]
  83. |===
  84. Temporal Units|Valid Abbreviations
  85. year|y, yr, years
  86. quarter|q, quarters
  87. month|mo, months
  88. week|w, weeks
  89. day|d, days
  90. hour|h, hours
  91. minute|min, minutes
  92. second|s, sec, seconds
  93. millisecond|ms, milliseconds
  94. |===