Conditional Functions

    Syntax

    If the condition cond evaluates to a non-zero value, returns the result of the expression then, and the result of the expression else, if present, is skipped. If the cond is zero or NULL, then the result of the then expression is skipped and the result of the else expression, if present, is returned.

    Parameters

    • cond – The condition for evaluation that can be zero or not. The type is UInt8, Nullable(UInt8) or NULL.
    • then - The expression to return if condition is met.
    • else - The expression to return if condition is not met.

    Returned values

    The function executes then and else expressions and returns its result, depending on whether the condition cond ended up being zero or not.

    Example

    Query:

    1. SELECT if(1, plus(2, 2), plus(2, 6))

    Result:

    1. ┌─plus(2, 2)─┐
    2. 4
    3. └────────────┘

    Query:

    Result:

    1. ┌─plus(2, 6)─┐
    2. 8
    3. └────────────┘
    • then and else must have the lowest common type.

    Take this LEFT_RIGHT table:

    1. SELECT *
    2. FROM LEFT_RIGHT
    3. ┌─left─┬─right─┐
    4. ᴺᵁᴸᴸ 4
    5. 1 3
    6. 3 1
    7. 4 ᴺᵁᴸᴸ

    The following query compares left and right values:

    Note: NULL values are not used in this example, check NULL values in conditionals section.

    It works same as if function.

    Syntax: cond ? then : else

    Returns then if the cond evaluates to be true (greater than zero), otherwise returns else.

    • cond must be of type of UInt8, and then and else must have the lowest common type.

    • then and else can be NULL

    See also

    • .

    Allows you to write the CASE operator more compactly in the query.

    Parameters:

    • cond_N — The condition for the function to return then_N.
    • then_N — The result of the function when executed.
    • else — The result of the function if none of the conditions is met.

    The function accepts 2N+1 parameters.

    Returned values

    The function returns one of the values then_N or else, depending on the conditions cond_N.

    Example

    Again using LEFT_RIGHT table.

    1. SELECT
    2. left,
    3. right,
    4. multiIf(left < right, 'left is smaller', left > right, 'left is greater', left = right, 'Both equal', 'Null value') AS result
    5. FROM LEFT_RIGHT
    6. ┌─left─┬─right─┬─result──────────┐
    7. ᴺᵁᴸᴸ 4 Null value
    8. 2 2 Both equal
    9. 3 1 left is greater
    10. 4 ᴺᵁᴸᴸ Null value
    11. └──────┴───────┴─────────────────┘

    Conditionals always result to 0, 1 or NULL. So you can use conditional results directly like this:

    1. FROM LEFT_RIGHT
    2. ┌─is_small─┐
    3. ᴺᵁᴸᴸ
    4. 1
    5. 0
    6. 0
    7. ᴺᵁᴸᴸ
    8. └──────────┘

    When NULL values are involved in conditionals, the result will also be NULL.

    So you should construct your queries carefully if the types are Nullable.

    The following example demonstrates this by failing to add equals condition to multiIf.

    1. SELECT
    2. left,
    3. right,
    4. multiIf(left < right, 'left is smaller', left > right, 'right is smaller', 'Both equal') AS faulty_result
    5. FROM LEFT_RIGHT
    6. ┌─left─┬─right─┬─faulty_result────┐
    7. ᴺᵁᴸᴸ 4 Both equal
    8. 1 3 left is smaller
    9. 2 2 Both equal
    10. 3 1 right is smaller
    11. 4 ᴺᵁᴸᴸ Both equal