| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 | [role="xpack"][testenv="basic"][[sql-functions-conditional]]=== Conditional Functions And ExpressionsFunctions that return one of their arguments by evaluating in an if-else manner.[[sql-functions-conditional-case]]==== `CASE`.Synopsis:[source, sql]----CASE WHEN condition THEN result    [WHEN ...]    [ELSE default_result]END----*Input*:One or multiple _WHEN *condition* THEN *result_* clauses are used and the expression can optionally havean _ELSE *default_result_* clause. Every *condition* should be a boolean expression.*Output*: one of the *result* expressions if the corresponding _WHEN *condition_* evaluates to `true` orthe *default_result* if all _WHEN *condition_* clauses evaluate to `false`. If the optional _ELSE *default_result_*clause is missing and all _WHEN *condition_* clauses evaluate to `false` then `null` is returned..DescriptionThe CASE expression is a generic conditional expression which simulates if/else statements of other programming languagesIf the condition’s result is true, the value of the result expression that follows the condition will be the returnedthe subsequent when clauses will be skipped and not processed.[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[case]----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[caseReturnNull]----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[caseWithElse]----As a variant, a case expression can be expressed with a syntax similar to *switch-case* of other programming languages:[source, sql]----CASE expression     WHEN value1 THEN result1    [WHEN value2 THEN result2]    [WHEN ...]    [ELSE default_result]END----In this case it's transformed internally to:[source, sql]----CASE WHEN expression = value1 THEN result1    [WHEN expression = value2 THEN result2]    [WHEN ...]    [ELSE default_result]END----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[caseWithOperand]----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[caseWithOperandAndElse]----[NOTE]===============================All result expressions must be of compatible data types. More specifically all resultexpressions should have a compatible data type with the 1st _non-null_ result expression.E.g.:for the following query:[source, sql]CASE WHEN a = 1 THEN null     WHEN a > 2 THEN 10     WHEN a > 5 THEN 'foo'ENDan error message would be returned, mentioning that *'foo'* is of data type *keyword*,which does not match the expected data type *integer* (based on result *10*).===============================[[sql-functions-conditional-case-groupby-custom-buckets]]===== Conditional bucketingCASE can be used as a GROUP BY key in a query to facilitate custom bucketingand assign descriptive names to those buckets. If, for example, the valuesfor a key are too many or, simply, ranges of those values are moreinteresting than every single value, CASE can create custom buckets as in thefollowing example:[source, sql]SELECT count(*) AS count,  CASE WHEN NVL(languages, 0) = 0 THEN 'zero'    WHEN languages = 1 THEN 'one'    WHEN languages = 2 THEN 'bilingual'    WHEN languages = 3 THEN 'trilingual'    ELSE 'multilingual'  END as lang_skillsFROM employeesGROUP BY lang_skillsORDER BY lang_skills;With this query, one can create normal grouping buckets for values _0, 1, 2, 3_ withdescriptive names, and every value _>= 4_ falls into the _multilingual_ bucket.[[sql-functions-conditional-coalesce]]==== `COALESCE`.Synopsis:[source, sql]----COALESCE(    expression, <1>    expression, <2>    ...)----*Input*:<1> 1st expression<2> 2nd expression...**N**th expressionCOALESCE can take an arbitrary number of arguments.*Output*: one of the expressions or `null`.DescriptionReturns the first of its arguments that is not null.If all arguments are null, then it returns `null`.[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[coalesceReturnNonNull]----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[coalesceReturnNull]----[[sql-functions-conditional-greatest]]==== `GREATEST`.Synopsis:[source, sql]----GREATEST(    expression, <1>    expression, <2>    ...)----*Input*:<1> 1st expression<2> 2nd expression...**N**th expressionGREATEST can take an arbitrary number of arguments andall of them must be of the same data type.*Output*: one of the expressions or `null`.DescriptionReturns the argument that has the largest value which is not null.If all arguments are null, then it returns `null`.[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[greatestReturnNonNull]----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[greatestReturnNull]----[[sql-functions-conditional-ifnull]]==== `IFNULL`.Synopsis:[source, sql]----IFNULL(    expression, <1>    expression) <2>----*Input*:<1> 1st expression<2> 2nd expression*Output*: 2nd expression if 1st expression is null, otherwise 1st expression..DescriptionVariant of <<sql-functions-conditional-coalesce>> with only two arguments.Returns the first of its arguments that is not null.If all arguments are null, then it returns `null`.[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[ifNullReturnFirst]----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[ifNullReturnSecond]----[[sql-functions-conditional-iif]]==== `IIF`.Synopsis:[source, sql]----IIF(    expression,   <1>    expression,   <2>    [expression]) <3>----*Input*:<1> boolean condition to check<2> return value if the boolean condition evaluates to `true`<3> return value if the boolean condition evaluates `false`; optional*Output*: 2nd expression if 1st expression (condition) evaluates to `true`. If it evaluates to `false`return 3rd expression. If 3rd expression is not provided return `null`..DescriptionConditional function that implements the standard _IF <condition> THEN <result1> ELSE <result2>_logic of programming languages. If the 3rd expression is not provided and the condition evaluates to `false`,`null` is returned.[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[iifWithDefaultValue]----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[iifWithoutDefaultValue]----[TIP]=================*IIF* functions can be combined to implement more complex logic simulating the <<sql-functions-conditional-case>>expression. E.g.:[source, sql]IIF(a = 1, 'one', IIF(a = 2, 'two', IIF(a = 3, 'three', 'many')))=================[[sql-functions-conditional-isnull]]==== `ISNULL`.Synopsis:[source, sql]----ISNULL(    expression, <1>    expression) <2>----*Input*:<1> 1st expression<2> 2nd expression*Output*: 2nd expression if 1st expression is null, otherwise 1st expression..DescriptionVariant of <<sql-functions-conditional-coalesce>> with only two arguments.Returns the first of its arguments that is not null.If all arguments are null, then it returns `null`.[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnFirst]----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[isNullReturnSecond]----[[sql-functions-conditional-least]]==== `LEAST`.Synopsis:[source, sql]----LEAST(    expression, <1>    expression, <2>    ...)----*Input*:<1> 1st expression<2> 2nd expression...**N**th expressionLEAST can take an arbitrary number of arguments andall of them must be of the same data type.*Output*: one of the expressions or `null`.DescriptionReturns the argument that has the smallest value which is not null.If all arguments are null, then it returns `null`.[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNonNull]----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[leastReturnNull]----[[sql-functions-conditional-nullif]]==== `NULLIF`.Synopsis:[source, sql]----NULLIF(    expression, <1>    expression) <2>----*Input*:<1> 1st expression<2> 2nd expression*Output*: `null` if the 2 expressions are equal, otherwise the 1st expression..DescriptionReturns `null` when the two input expressions are equal andif not, it returns the 1st expression.[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnFirst]----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[nullIfReturnNull]----[[sql-functions-conditional-nvl]]==== `NVL`.Synopsis:[source, sql]----NVL(    expression, <1>    expression) <2>----*Input*:<1> 1st expression<2> 2nd expression*Output*: 2nd expression if 1st expression is null, otherwise 1st expression..DescriptionVariant of <<sql-functions-conditional-coalesce>> with only two arguments.Returns the first of its arguments that is not null.If all arguments are null, then it returns `null`.[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnFirst]----[source, sql]----include-tagged::{sql-specs}/docs/docs.csv-spec[nvlReturnSecond]----
 |