conditional.asciidoc 9.0 KB

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