conditional.asciidoc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[sql-functions-conditional]]
  4. === Conditional Functions And Expressions
  5. Functions that return one of their arguments by evaluating in an if-else manner.
  6. [[sql-functions-conditional-case]]
  7. ==== `CASE`
  8. .Synopsis:
  9. [source, sql]
  10. ----
  11. CASE WHEN condition THEN result
  12. [WHEN ...]
  13. [ELSE default_result]
  14. END
  15. ----
  16. *Input*:
  17. One or multiple _WHEN *condition* THEN *result_* clauses are used and the expression can optionally have
  18. an _ELSE *default_result_* clause. Every *condition* should be a boolean expression.
  19. *Output*: one of the *result* expressions if the corresponding _WHEN *condition_* evaluates to `true` or
  20. the *default_result* if all _WHEN *condition_* clauses evaluate to `false`. If the optional _ELSE *default_result_*
  21. clause is missing and all _WHEN *condition_* clauses evaluate to `false` then `null` is returned.
  22. *Description*: The CASE expression is a generic conditional expression which simulates if/else statements of other programming languages
  23. If the condition’s result is true, the value of the result expression that follows the condition will be the returned
  24. the subsequent when clauses will be skipped and not processed.
  25. [source, sql]
  26. ----
  27. include-tagged::{sql-specs}/docs/docs.csv-spec[case]
  28. ----
  29. [source, sql]
  30. ----
  31. include-tagged::{sql-specs}/docs/docs.csv-spec[caseReturnNull]
  32. ----
  33. [source, sql]
  34. ----
  35. include-tagged::{sql-specs}/docs/docs.csv-spec[caseWithElse]
  36. ----
  37. As a variant, a case expression can be expressed with a syntax similar to *switch-case* of other programming languages:
  38. [source, sql]
  39. ----
  40. CASE expression
  41. WHEN value1 THEN result1
  42. [WHEN value2 THEN result2]
  43. [WHEN ...]
  44. [ELSE default_result]
  45. END
  46. ----
  47. In this case it's transformed internally to:
  48. [source, sql]
  49. ----
  50. CASE WHEN expression = value1 THEN result1
  51. [WHEN expression = value2 THEN result2]
  52. [WHEN ...]
  53. [ELSE default_result]
  54. END
  55. ----
  56. [source, sql]
  57. ----
  58. include-tagged::{sql-specs}/docs/docs.csv-spec[caseWithOperand]
  59. ----
  60. [source, sql]
  61. ----
  62. include-tagged::{sql-specs}/docs/docs.csv-spec[caseWithOperandAndElse]
  63. ----
  64. [NOTE]
  65. ===============================
  66. All result expressions must be of compatible data types. More specifically all result
  67. expressions should have a compatible data type with the 1st _non-null_ result expression.
  68. E.g.:
  69. for the following query:
  70. [source, sql]
  71. CASE WHEN a = 1 THEN null
  72. WHEN a > 2 THEN 10
  73. WHEN a > 5 THEN 'foo'
  74. END
  75. an error message would be returned, mentioning that *'foo'* is of data type *keyword*,
  76. which does not match the expected data type *integer* (based on result *10*).
  77. ===============================
  78. [[sql-functions-conditional-case-groupby-custom-buckets]]
  79. ===== Conditional bucketing
  80. CASE can be used as a GROUP BY key in a query to facilitate custom bucketing
  81. and assign descriptive names to those buckets. If, for example, the values
  82. for a key are too many or, simply, ranges of those values are more
  83. interesting than every single value, CASE can create custom buckets as in the
  84. following example:
  85. [source, sql]
  86. SELECT count(*) AS count,
  87. CASE WHEN NVL(languages, 0) = 0 THEN 'zero'
  88. WHEN languages = 1 THEN 'one'
  89. WHEN languages = 2 THEN 'bilingual'
  90. WHEN languages = 3 THEN 'trilingual'
  91. ELSE 'multilingual'
  92. END as lang_skills
  93. FROM employees
  94. GROUP BY lang_skills
  95. ORDER BY lang_skills;
  96. With this query, one can create normal grouping buckets for values _0, 1, 2, 3_ with
  97. descriptive names, and every value _>= 4_ falls into the _multilingual_ bucket.
  98. [[sql-functions-conditional-coalesce]]
  99. ==== `COALESCE`
  100. .Synopsis:
  101. [source, sql]
  102. ----
  103. COALESCE(
  104. expression, <1>
  105. expression, <2>
  106. ...)
  107. ----
  108. *Input*:
  109. <1> 1st expression
  110. <2> 2nd expression
  111. ...
  112. **N**th expression
  113. COALESCE can take an arbitrary number of arguments.
  114. *Output*: one of the expressions or `null`
  115. *Description*: Returns the first of its arguments that is not null.
  116. If all arguments are null, then it returns `null`.
  117. [source, sql]
  118. ----
  119. include-tagged::{sql-specs}/docs/docs.csv-spec[coalesceReturnNonNull]
  120. ----
  121. [source, sql]
  122. ----
  123. include-tagged::{sql-specs}/docs/docs.csv-spec[coalesceReturnNull]
  124. ----
  125. [[sql-functions-conditional-greatest]]
  126. ==== `GREATEST`
  127. .Synopsis:
  128. [source, sql]
  129. ----
  130. GREATEST(
  131. expression, <1>
  132. expression, <2>
  133. ...)
  134. ----
  135. *Input*:
  136. <1> 1st expression
  137. <2> 2nd expression
  138. ...
  139. **N**th expression
  140. GREATEST can take an arbitrary number of arguments and
  141. all of them must be of the same data type.
  142. *Output*: one of the expressions or `null`
  143. *Description*: Returns the argument that has the largest value which is not null.
  144. If all arguments are null, then it returns `null`.
  145. [source, sql]
  146. ----
  147. include-tagged::{sql-specs}/docs/docs.csv-spec[greatestReturnNonNull]
  148. ----
  149. [source, sql]
  150. ----
  151. include-tagged::{sql-specs}/docs/docs.csv-spec[greatestReturnNull]
  152. ----
  153. [[sql-functions-conditional-ifnull]]
  154. ==== `IFNULL`
  155. .Synopsis:
  156. [source, sql]
  157. ----
  158. IFNULL(
  159. expression, <1>
  160. expression) <2>
  161. ----
  162. *Input*:
  163. <1> 1st expression
  164. <2> 2nd expression
  165. *Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
  166. *Description*: Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
  167. Returns the first of its arguments that is not null.
  168. If all arguments are null, then it returns `null`.
  169. [source, sql]
  170. ----
  171. include-tagged::{sql-specs}/docs/docs.csv-spec[ifNullReturnFirst]
  172. ----
  173. [source, sql]
  174. ----
  175. include-tagged::{sql-specs}/docs/docs.csv-spec[ifNullReturnSecond]
  176. ----
  177. [[sql-functions-conditional-iif]]
  178. ==== `IIF`
  179. .Synopsis:
  180. [source, sql]
  181. ----
  182. IIF(
  183. expression, <1>
  184. expression, <2>
  185. [expression]) <3>
  186. ----
  187. *Input*:
  188. <1> boolean condition to check
  189. <2> return value if the boolean condition evaluates to `true`
  190. <3> return value if the boolean condition evaluates `false`; optional
  191. *Output*: 2nd expression if 1st expression (condition) evaluates to `true`. If it evaluates to `false`
  192. return 3rd expression. If 3rd expression is not provided return `null`.
  193. *Description*: Conditional function that implements the standard _IF <condition> THEN <result1> ELSE <result2>_
  194. logic of programming languages. If the 3rd expression is not provided and the condition evaluates to `false`,
  195. `null` is returned.
  196. [source, sql]
  197. ----
  198. include-tagged::{sql-specs}/docs/docs.csv-spec[iifWithDefaultValue]
  199. ----
  200. [source, sql]
  201. ----
  202. include-tagged::{sql-specs}/docs/docs.csv-spec[iifWithoutDefaultValue]
  203. ----
  204. [TIP]
  205. =================
  206. *IIF* functions can be combined to implement more complex logic simulating the <<sql-functions-conditional-case>>
  207. expression. E.g.:
  208. [source, sql]
  209. IIF(a = 1, 'one', IIF(a = 2, 'two', IIF(a = 3, 'three', 'many')))
  210. =================
  211. [[sql-functions-conditional-isnull]]
  212. ==== `ISNULL`
  213. .Synopsis:
  214. [source, sql]
  215. ----
  216. ISNULL(
  217. expression, <1>
  218. expression) <2>
  219. ----
  220. *Input*:
  221. <1> 1st expression
  222. <2> 2nd expression
  223. *Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
  224. *Description*: Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
  225. Returns the first of its arguments that is not null.
  226. If all arguments are null, then it returns `null`.
  227. [source, sql]
  228. ----
  229. include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnFirst]
  230. ----
  231. [source, sql]
  232. ----
  233. include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnSecond]
  234. ----
  235. [[sql-functions-conditional-least]]
  236. ==== `LEAST`
  237. .Synopsis:
  238. [source, sql]
  239. ----
  240. LEAST(
  241. expression, <1>
  242. expression, <2>
  243. ...)
  244. ----
  245. *Input*:
  246. <1> 1st expression
  247. <2> 2nd expression
  248. ...
  249. **N**th expression
  250. LEAST can take an arbitrary number of arguments and
  251. all of them must be of the same data type.
  252. *Output*: one of the expressions or `null`
  253. *Description*: Returns the argument that has the smallest value which is not null.
  254. If all arguments are null, then it returns `null`.
  255. [source, sql]
  256. ----
  257. include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNonNull]
  258. ----
  259. [source, sql]
  260. ----
  261. include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNull]
  262. ----
  263. [[sql-functions-conditional-nullif]]
  264. ==== `NULLIF`
  265. .Synopsis:
  266. [source, sql]
  267. ----
  268. NULLIF(
  269. expression, <1>
  270. expression) <2>
  271. ----
  272. *Input*:
  273. <1> 1st expression
  274. <2> 2nd expression
  275. *Output*: `null` if the 2 expressions are equal, otherwise the 1st expression.
  276. *Description*: Returns `null` when the two input expressions are equal and
  277. if not, it returns the 1st expression.
  278. [source, sql]
  279. ----
  280. include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnFirst]
  281. ----
  282. [source, sql]
  283. ----
  284. include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnNull]
  285. ----
  286. [[sql-functions-conditional-nvl]]
  287. ==== `NVL`
  288. .Synopsis:
  289. [source, sql]
  290. ----
  291. NVL(
  292. expression, <1>
  293. expression) <2>
  294. ----
  295. *Input*:
  296. <1> 1st expression
  297. <2> 2nd expression
  298. *Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
  299. *Description*: Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
  300. Returns the first of its arguments that is not null.
  301. If all arguments are null, then it returns `null`.
  302. [source, sql]
  303. ----
  304. include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnFirst]
  305. ----
  306. [source, sql]
  307. ----
  308. include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnSecond]
  309. ----