Newer Version Available
case
Syntax—Searched Case Expression
1case
2 when search_condition then result_expr
3 [ when search_condition2 then result_expr2 … ]
4 [ else default_expr ]
5end- case...end
- The case and end keywords begin and close the expression.
- when...then
- The when and then keywords define a conditional statement. A case expression can contain one or more conditional statement.
-
-
search_condition—Any logical expression that can be
evaluated to true or false. This expression may be constructed using any
values, identifiers, logical operator, comparison operator, or scalar functions
(including date and math functions) supported by SAQL. Examples of valid
search_condition syntax:
- xInt < 5
- price > 1000 and price <= 2000
- units*round(price_per_unit) < abs(revenue)
-
result_expr—Any
expression that can be evaluated by the SAQL engine. May contain values,
identifiers, and scalar functions (including date and math functions). The
expression may evaluate to any data type. However, this data type must be consistent
among all conditional expressions. That is, if result_expr is of NUMERIC type, then result_expr2 … result_exprN must be of NUMERIC type.
Examples of valid result_expr syntax:
- xInt
- toString('orderDate', "dd/MM/yyyy")
- "abc"
-
search_condition—Any logical expression that can be
evaluated to true or false. This expression may be constructed using any
values, identifiers, logical operator, comparison operator, or scalar functions
(including date and math functions) supported by SAQL. Examples of valid
search_condition syntax:
- else
- (Optional)—Allows a default expression to be specified. The else statement must follow the conditional when/then statement. There can be only one else statement.
-
-
default_expr—Any expression that can be evaluated by the SAQL engine. May contain values, identifiers, and scalar functions (including date and math functions). The data type must be consistent with the data type of result_expr specified in the preceding conditional statements.
-
Usage—Searched Case Expression
Conditional statements are evaluated on a row by row basis in the order in which they are given. If a search_condition evaluates as true, the corresponding result_expr is returned for that row. Therefore, if more than one of the conditional statements returns true, only the first one is evaluated. At least one when/then statement must be provided. An unlimited number of when/then statements may be provided.
A default_expr may be set with the optional else statement. If none of the search_condition expressions evaluate to true, the default_expr expression is returned. If no else statement is specified, null is returned as the default.
Syntax—Simple Case Expression
1case primary_expr
2 when test_expr then result_expr
3 [ when test_expr2 then result_expr2 … ]
4 [ else default_expr ]
5end- case...end
- The case and end keywords begin and close the expression.
-
-
primary_expr—Any
scalar expression that can be evaluated by the SAQL engine. May contain values,
identifiers, and scalar functions (including date and math functions). The
expression may evaluate to any comparable data type (NUMERIC, STRING, or DATE).
Examples of valid primary_expr syntax:
- xInt % 3
- date('year', 'month', 'day')
- "abc"
-
primary_expr—Any
scalar expression that can be evaluated by the SAQL engine. May contain values,
identifiers, and scalar functions (including date and math functions). The
expression may evaluate to any comparable data type (NUMERIC, STRING, or DATE).
Examples of valid primary_expr syntax:
- when...then
- The when and then keywords define a conditional statement. A case expression can contain one or more conditional statements.
-
-
test_expr—Any
scalar expression that can be evaluated by the SAQL engine. This expression may be
constructed using any values, identifiers, and scalar functions (including date and
math functions), but must evaluate to the same data type as the primary_expr. Examples of valid
test_expr syntax:
- 5
- "abc"
- abs(profit)
-
result_expr—Any
scalar expression that can be evaluated by the SAQL engine. May contain values,
identifiers, and scalar functions (including date and math functions). The
expression may evaluate to any data type. However, this data type must be consistent
among all conditional statements. That is, if result_expr is of NUMERIC type, then result_expr2...result_exprN must be of
NUMERIC type. Examples of result_expr syntax:
- xInt
- toString('orderDate', "dd/MM/yyyy")
- "abc"
-
test_expr—Any
scalar expression that can be evaluated by the SAQL engine. This expression may be
constructed using any values, identifiers, and scalar functions (including date and
math functions), but must evaluate to the same data type as the primary_expr. Examples of valid
test_expr syntax:
- else
- (Optional) The else keyword allows a default expression to be specified. The else statement must follow conditional when/then statements. There can be only one else statement.
-
- default_expr—Any scalar expression that can be evaluated by the SAQL engine. May contain values, identifiers, and scalar functions (including date and math functions). The data type must be consistent with the data type of result_expr specified in the preceding conditional statements.
Usage—Simple Case Expression
A default_expr may be set with the optional else statement. If primary_expr doesn’t equal any of the test_expr conditions, the default_expr is returned. If no else statement is specified, null is returned as the default.
Using case Statements
Use case expressions in foreach clauses. Don’t use case expressions in order by, group by, or filter by clauses.
Example
1q = load "data";
2q = foreach q generate xInt, (case xInt % 3
3 when 0 then "3n"
4 when 1 then "3n+1"
5 else "3n+2"
6end) as modThree;Example
1q = load "data";
2q = foreach q generate price, (case
3 when price < 1000 then "category1"
4 when price >= 1000 and price < 2000 then "category2"
5 else "category3"
6end) as priceLevel;Handling Null Values
1q = load "data";
2q = foreach q generate Mea1, (case Mea1
3 when 0 then "Type1"
4 when 1 then "Type2"
5 else "Other"
6end) as Category;1q = load “data��;
2q = foreach q generate Mea1, (case
3 when Mea1 is null then "Is Null"
4 else "Is Not Null"
5end) as Category;Best Practices for Working with Dates
Example
1q = load "data/dates";
2q = foreach q generate OrderDate, (case
3 when toDate(OrderDate_epoch_secs) < toDate("2/1/2015", "M/d/yyyy") and
4toDate(OrderDate_epoch_secs) >= toDate("1/1/2015", "M/d/yyyy") then "Jan"
5 else "Other"
6end) as Month;