条件函数

请注意:
下文中的一些示例引用自 ClickHouse 社区文档 并经过一定修改确保可以在 ByteHouse 中正常使用。

multiIf

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

Syntax

multiIf(cond_1, then_1, cond_2, then_2, ..., else)

Arguments:

  • 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

CREATE TABLE IF NOT EXISTS test.functionMultiIf (id UInt8, left Nullable(UInt8), right Nullable(UInt8)) ENGINE=CnchMergeTree ORDER BY id;
INSERT INTO test.functionMultiIf VALUES (1,NULL,4),(2,1,3),(3,2,2),(4,3,1),(5,4,NULL);
SELECT
    left,
    right,
    multiIf(left < right, 'left is smaller', left > right, 'left is greater', left = right, 'Both equal', 'Null value') AS result
FROM test.functionMultiIf
┌─left─┬─right─┬─result──────────┐
│ ᴺᵁᴸᴸ │ 4     │ Null value      │
│ 1    │ 3     │ left is smaller │
│ 2    │ 2     │ Both equal      │
│ 3    │ 1     │ left is greater │
│ 4    │ ᴺᵁᴸᴸ  │ Null value      │
└──────┴───────┴─────────────────┘