Implement if-then-else logic in queries.
if
: Basic conditional branchingmultiIf
: Multiple condition checkingcase
: SQL-style conditional expressionscond
(UInt8
, Nullable(UInt8)
or NULL
): The condition to evaluate.then
(any): The expression to return if the condition is true.else
(any): The expression to return if the condition is false or NULL.then
or else
expression, depending on the condition cond
.
Example:
if
categorizes orders as ‘Large Order’ if they have more than 5 tacos, and ‘Small Order’ otherwise.
then
and else
expressions must be of similar types.cond_N
(Boolean
): The N-th condition to evaluate.then_N
(any): The result to return when cond_N
is true.else
(any): The result to return if none of the conditions are true.then
expression, or the else
expression if no conditions are true.
Example:
multiIf
categorizes taco orders into different sizes based on the number of tacos ordered.
value1
, value2
, … (any comparable types): The values to compare.greatest
determines the most popular type of taco for each order based on the quantity ordered.
value1
, value2
, … (any comparable types): The values to compare.least
determines the least popular type of taco for each order based on the quantity ordered.
cond_N
(UInt8
): The N-th evaluated condition which controls if then_N
is returned.then_N
(Any
): The result of the function when cond_N
is true.else
(Any
): The result of the function if none of the conditions are true.then_N
or else
expressions, depending on the conditions cond_N
.
Example:
multiIf
categorizes taco orders based on the number of tacos ordered.
value1
, value2
, … (any): The values to compare.max_number
returns 5, the greatest numeric value.last_alphabetically
returns ‘fish’, which comes last alphabetically.latest_date
returns the most recent date.Int32
with a Float32
will result in a Float32
return type.a
, b
, … (any): The values to compare.least
returns the smaller value between num_tacos
and num_sides
for each order.
value
(numeric): The input value to constrain.min
(numeric): The lower bound.max
(numeric): The upper bound.value
is less than min
, returns min
.value
is greater than max
, returns max
.value
.clamp
ensures that the number of tacos per order is between 1 and 5. Orders with more than 5 tacos are capped at 5, and orders with less than 1 taco are increased to 1.