Conditional functions
Implement if-then-else logic in queries.
Conditional functions in ClickHouse allow you to implement if-then-else logic directly in your queries.
Key conditional functions include:
if
: Basic conditional branchingmultiIf
: Multiple condition checkingcase
: SQL-style conditional expressions
ClickHouse function reference
if
Performs conditional branching.
Syntax:
Alternatively, you can use the ternary operator:
Arguments:
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.
Returns:
The result of either the then
or else
expression, depending on the condition cond
.
Example:
Result:
In this 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:
Arguments:
cond_N
(Boolean
): The N-th condition to evaluate.then_N
(any): The result to return whencond_N
is true.else
(any): The result to return if none of the conditions are true.
Returns:
The result of the first true condition’s corresponding then
expression, or the else
expression if no conditions are true.
Example:
Result:
In this 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:
Arguments:
value1
,value2
, … (any comparable types): The values to compare.
Returns:
The greatest value among the input arguments.
Example:
Result:
In this example, 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:
Arguments:
value1
,value2
, … (any comparable types): The values to compare.
Returns:
The smallest value among the input arguments.
Example:
Result:
In this example, 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:
Arguments:
cond_N
(UInt8
): The N-th evaluated condition which controls ifthen_N
is returned.then_N
(Any
): The result of the function whencond_N
is true.else
(Any
): The result of the function if none of the conditions are true.
Returns:
The result of either any of the then_N
or else
expressions, depending on the conditions cond_N
.
Example:
Result:
In this 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:
Arguments:
value1
,value2
, … (any): The values to compare.
Returns:
The greatest value among the input arguments. The return type is determined by the types of the input arguments.
Example:
Result:
In this example:
max_number
returns 5, the greatest numeric value.last_alphabetically
returns ‘fish’, which comes last alphabetically.latest_date
returns 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:
Arguments:
a
,b
, … (any): The values to compare.
Returns:
The smallest value among the arguments. The return type is determined by the types of the input arguments.
Example:
Result:
In this example, 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:
Arguments:
value
(numeric): The input value to constrain.min
(numeric): The lower bound.max
(numeric): The upper bound.
Returns:
- If
value
is less thanmin
, returnsmin
. - If
value
is greater thanmax
, returnsmax
. - Otherwise, returns
value
.
Example:
Result:
In this example, 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.
Was this page helpful?