case.asciidoc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. [discrete]
  2. [[esql-case]]
  3. === `CASE`
  4. *Syntax*
  5. [source,esql]
  6. ----
  7. CASE(condition1, value1[, ..., conditionN, valueN][, default_value])
  8. ----
  9. *Parameters*
  10. `conditionX`::
  11. A condition.
  12. `valueX`::
  13. The value that's returned when the corresponding condition is the first to
  14. evaluate to `true`.
  15. `default_value`::
  16. The default value that's is returned when no condition matches.
  17. *Description*
  18. Accepts pairs of conditions and values. The function returns the value that
  19. belongs to the first condition that evaluates to `true`.
  20. If the number of arguments is odd, the last argument is the default value which
  21. is returned when no condition matches. If the number of arguments is even, and
  22. no condition matches, the function returns `null`.
  23. *Example*
  24. Determine whether employees are monolingual, bilingual, or polyglot:
  25. [source,esql]
  26. [source.merge.styled,esql]
  27. ----
  28. include::{esql-specs}/docs.csv-spec[tag=case]
  29. ----
  30. [%header.monospaced.styled,format=dsv,separator=|]
  31. |===
  32. include::{esql-specs}/docs.csv-spec[tag=case-result]
  33. |===
  34. Calculate the total connection success rate based on log messages:
  35. [source,esql]
  36. [source.merge.styled,esql]
  37. ----
  38. include::{esql-specs}/conditional.csv-spec[tag=docsCaseSuccessRate]
  39. ----
  40. [%header.monospaced.styled,format=dsv,separator=|]
  41. |===
  42. include::{esql-specs}/conditional.csv-spec[tag=docsCaseSuccessRate-result]
  43. |===
  44. Calculate an hourly error rate as a percentage of the total number of log
  45. messages:
  46. [source,esql]
  47. [source.merge.styled,esql]
  48. ----
  49. include::{esql-specs}/conditional.csv-spec[tag=docsCaseHourlyErrorRate]
  50. ----
  51. [%header.monospaced.styled,format=dsv,separator=|]
  52. |===
  53. include::{esql-specs}/conditional.csv-spec[tag=docsCaseHourlyErrorRate-result]
  54. |===