IF THEN ELSE

IF THEN ELSE is a conditional statement that returns one of two possible values, or performs one of two possible computations, based on the condition met. Since the statement is used inside a metric, only use numerical values in the THEN part of the statement.

The defined condition follows the keyword IF and can be constructed by using any of the filtering keywords or relational operators (IN, NOT IN, BETWEEN, NOT BETWEEN, =, < , <=, >, >=, <>).

The first possible outcome follows the keyword THEN, and the second possible outcome follows the keyword ELSE. The outcomes that are returned can be numerical values or arithmetic operations. All IF THEN ELSE statements conclude with the END keyword.

Syntax

``````SELECT IF … THEN … ELSE … END
SELECT IF condition THEN number ELSE number END
SELECT IF condition THEN arithmetic_operation ELSE arithmetic_operation END
``````

Examples

``````SELECT IF SUM(Amount)>= AVG(Amount) THEN 10 ELSE 0 END
SELECT IF SUM(Duration) - AVG(Duration) > 2000  THEN 0 ELSE 1 END
SELECT IF AVG(Probability) > 0.5 THEN SUM(Amount) * 10 ELSE SUM(Amount) / 10 END
``````