case()case()

计算谓词列表并返回其谓词满足条件的第一个结果表达式Evaluates a list of predicates and returns the first result expression whose predicate is satisfied.

如果两个谓词都没有返回 true,则返回最后一个表达式 (else) 的结果。If neither of the predicates return true, the result of the last expression (the else) is returned. 所有奇数参数(从 1 开始计数)必须是计算结果为 boolean 值的表达式。All odd arguments (count starts at 1) must be expressions that evaluate to a boolean value. 所有偶数参数 (then) 和最后一个参数 (else) 的类型必须相同。All even arguments (the thens) and the last argument (the else) must be of the same type.

语法Syntax

case(predicate_1, then_1, predicate_2, then_2, predicate_3, then_3, else)case(predicate_1, then_1, predicate_2, then_2, predicate_3, then_3, else)

参数Arguments

  • predicate_i:一个计算结果为 boolean 值的表达式。predicate_i: An expression that evaluates to a boolean value.
  • then_i:一个表达式,在进行计算时,如果 predicate_i 是计算结果为 true 的第一个谓词,则从函数返回其值。then_i: An expression that gets evaluated and its value is returned from the function if predicate_i is the first predicate that evaluates to true.
  • else:一个表达式,在进行计算时,如果 predicate_i 的计算结果都不是 true,则从函数返回其值。else: An expression that gets evaluated and its value is returned from the function if neither of the predicate_i evaluate to true.

返回Returns

第一个其 predicate_i 的计算结果为 true 的 then_i 的值;如果谓词都得不到满足,则为 else 的值。The value of the first then_i whose predicate_i evaluates to true, or the value of else if neither of the predicates are satisfied.

示例Example

range Size from 1 to 15 step 2
| extend bucket = case(Size <= 3, "Small", 
                       Size <= 10, "Medium", 
                       "Large")
大小Size Bucketbucket
11 小型Small
33 小型Small
55 中型Medium
77 中型Medium
99 中型Medium
1111 大型Large
1313 大型Large
1515 大型Large