case(X;V1;R1;V2;R2;...;D)
Returns the value corresponding to the case that a given value matches; if no cases match, returns the specified default value.
Syntax
case(X;V1;R1;V2;R2;...;D)
Input
Argument | Type | Description |
---|---|---|
X |
any simple type | A scalar value or the name of a column |
V[N] |
any simple type | The value (or expression that evaluates to a value) to test for equality to
X .The values specified for all instances of A scalar value or the name of a column |
R[N] |
any simple type | The value to return if V[N] matches
X .The values specified for all instances of A scalar value or the name of a column |
D |
any simple type | The default return value (if X doesn't match any of the
instances of V ).The value specified for A scalar value or the name of a column |
X
, a series of
C
and R
pairs, and one D
argument).Return Value
R
and D
) corresponding to the following:- If
X
isV1
, returnR1
; else ifX
isV2
, returnR2
; and so on. - If
X
does not match any of the values specified byV[N]
, returnD
.
If any of the values corresponding to C[N]
are N/A, an
error is returned.
Sample Usage
X |
V1 |
R1 |
V2 |
R2 |
V3 |
R3 |
D |
case(X;V1;R1;V2;R2;V3;R3;D) |
---|---|---|---|---|---|---|---|---|
2 | 1 | 'one' | 2 | 'two' | 3 | 'three' | 'too many' | 'two' |
99 | 1 | 'one' | 2 | 'two' | 3 | 'three' | 'too many' | 'too many' |
Example
Let's use the table pub.demo.retail.item to
illustrate the use of
case(X;V1;R1;V2;R2;...;D)
.
<willbe name="location" value="case(store;1;'New York';2;'Miami';3;'London';'Not a valid location')"/>
This will give us the following new column:
Since, in our example, there are no values in the store
column that are not 1, 2, or 3, the default value ('Not a
valid location
') is never used.