CASE

    Logical functions

    Syntax

    Version 1:

    CASE expression
        WHEN value_1 THEN result_1
        [ WHEN value_2 THEN result_2
          ... ]
        ELSE default_result
    END
    

    Version 2:

    CASE(
        expression,
        value_1, result_1,
      [ value_2, result_2,
        ... ]
        default_result
    )
    

    Description

    Compares expression to value_1, value_2, ... consecutively and returns the corresponding result for the first match. If no match is found, it returns default_result.

    Argument types:

    • expressionAny
    • value_1Any
    • result_1Any
    • value_2Any
    • result_2Any
    • default_resultAny

    Return type: Same type as (result_1, result_2, default_result)

    Note

    Arguments (result_1, result_2, default_result) must be of the same type.

    Examples

    CASE (
        [country],
        "BY", "Белоруссия",
        "KZ", "Казахстан",
        "RU", "Россия",
        "TR", "Турция",
        "UZ", "Узбекистан",
        "Другая страна"
    )
    
    CASE [country]
        WHEN "BY" THEN "Белоруссия"
        WHEN "KZ" THEN "Казахстан"
        WHEN "RU" THEN "Россия"
        WHEN "TR" THEN "Турция"
        WHEN "UZ" THEN "Узбекистан"
        ELSE "Другая страна"
    END
    

    Data source support

    Materialized Dataset, ClickHouse 1.1, Microsoft SQL Server 2017 (14.0), MySQL 5.6, PostgreSQL 9.3.