if: Basic conditional branchingmultiIf: Multiple condition checkingcase: SQL-style conditional expressions
ClickHouse function reference
if
Performs conditional branching. Syntax:cond(UInt8,Nullable(UInt8)orNULL): 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.
The
then and else expressions must be of similar types.multiIf
Allows you to write the CASE operator more compactly in a query. Syntax:cond_N(Boolean): The N-th condition to evaluate.then_N(any): The result to return whencond_Nis 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.
greatest
Returns the greatest value across a list of values. Syntax: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.
All input values must be of comparable types. The return type may be promoted to accommodate the comparison (e.g., mixing integer and float types will return a float).
least
Returns the smallest value across a list of values. Syntax: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.
All input values must be of comparable types. The return type may be promoted to accommodate the comparison (e.g., mixing integer and float types will return a float).
multiIf
Allows you to write the CASE operator more compactly in a query. Syntax:cond_N(UInt8): The N-th evaluated condition which controls ifthen_Nis returned.then_N(Any): The result of the function whencond_Nis 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.
greatest
Returns the greatest value across a list of values. All of the list members must be of comparable types. Syntax:value1,value2, … (any): The values to compare.
max_numberreturns 5, the greatest numeric value.last_alphabeticallyreturns ‘fish’, which comes last alphabetically.latest_datereturns the most recent date.
When comparing different numeric types, type promotion may occur. For example, comparing an
Int32 with a Float32 will result in a Float32 return type.least
Returns the smallest value from a list of arguments. Syntax:a,b, … (any): The values to compare.
least returns the smaller value between num_tacos and num_sides for each order.
All arguments must be of comparable types. If the types are different, type conversion may occur.
clamp
Constrains a value between a minimum and maximum. Syntax:value(numeric): The input value to constrain.min(numeric): The lower bound.max(numeric): The upper bound.
- If
valueis less thanmin, returnsmin. - If
valueis greater thanmax, returnsmax. - Otherwise, returns
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.