1
0

conditional.asciidoc 9.0 KB

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