conditional.asciidoc 9.0 KB

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