CASE WHEN
All functions > CONDITIONAL > CASE WHEN
Returns the result of the first condition that evaluates to true
Syntax
CASE [ { FIRST | UNIQUE | MULTI } [ subject | ( subject, subject ... ) ] ] WHEN condition_or_cells THEN result { WHEN condition_or_cells THEN result ... } [ ELSE default ] ENDNotes
- Write this as
CASE WHEN … THEN … [ELSE …] ENDin FeatureQL (same shape as familiar SQL). Conditions are evaluated in order; the first one that is TRUE selects itsTHENvalue. - If no
WHENis TRUE andELSEis omitted, the result is NULL (typed with the common type of the branch values—useNULL::<type>or typedNULL(...)where inference needs a type). - Evaluation short-circuits: once a condition is TRUE, later branches are not evaluated.
- Every
THENexpression and theELSEexpression (if present) must be type-compatible. - Decision tables: a policy keyword after
CASEmakes the compiler check the arms.UNIQUE: arms never overlap.FIRST: the first matching arm wins and every arm can win.MULTI: returnsARRAY(T)of every matching value, in arm order. WithoutELSE,UNIQUEandFIRSTmust cover every input,NULLincluded. - Grid form: after the policy, name the inputs once (
CASE UNIQUE (age, country)) and write one cell per input in each arm. In a cell,_alone matches any value, a condition on_tests that input (_ >= 18), and a constant means equality ('FR'). - Under a policy, each condition compares one input with constants. See Decision tables for the checks, the allowed conditions and every error and warning.
FIRST,UNIQUEandMULTIare keywords only directly afterCASE. For a simpleCASEon a feature with one of those names, writeCASE (first) WHEN ….
Examples
Searched CASE
FeatureQL
RETURN
-- Second WHEN is first true branch
f1 := CASE
WHEN FALSE THEN 1
WHEN TRUE THEN 2
WHEN FALSE THEN 3
ELSE 999
END,
-- No WHEN true, ELSE applies
f2 := CASE
WHEN FALSE THEN 1
WHEN FALSE THEN 2
ELSE 999
END,
-- First true branch short-circuits
f3 := CASE
WHEN TRUE THEN 1
WHEN TRUE THEN 2
ELSE 999
END,
-- No match and no ELSE → NULL
f4 := CASE
WHEN FALSE THEN 1
WHEN FALSE THEN 2
END
;Result
| f1 BIGINT | f2 BIGINT | f3 BIGINT | f4 BIGINT |
|---|---|---|---|
| 2 | 999 | 1 | null |
String results
FeatureQL
RETURN
-- Expression in WHEN
f1 := CASE
WHEN 1 = 1 THEN 'yes'
ELSE 'no'
END,
-- First matching inequality
f2 := CASE
WHEN 10 > 5 THEN 'big'
WHEN 10 > 20 THEN 'huge'
ELSE 'small'
END,
-- Single WHEN with ELSE
f3 := CASE
WHEN TRUE THEN 42
ELSE 0
END
;Result
| f1 VARCHAR | f2 VARCHAR | f3 BIGINT |
|---|---|---|
| yes | big | 42 |
Decision tables
FeatureQL
RETURN
-- UNIQUE: arms proven disjoint
f1 := CASE UNIQUE 25
WHEN _ < 18 THEN 'minor'
WHEN _ >= 18 THEN 'adult'
ELSE 'unknown'
END,
-- FIRST grid: the exception comes first
f2 := CASE FIRST ('FR', 16)
WHEN ('FR', _ < 18) THEN 'blocked'
WHEN ('FR', _ ) THEN 'domestic'
ELSE 'international'
END,
-- MULTI: every matching value, in arm order
f3 := CASE MULTI 5000
WHEN _ >= 100 THEN 'large'
WHEN _ >= 1000 THEN 'huge'
ELSE 'none'
END,
-- MULTI with no match returns ARRAY(<else>)
f4 := CASE MULTI 5
WHEN _ >= 100 THEN 'large'
WHEN _ >= 1000 THEN 'huge'
ELSE 'none'
END
;Result
| f1 VARCHAR | f2 VARCHAR | f3 ARRAY | f4 ARRAY |
|---|---|---|---|
| adult | blocked | [large, huge] | [none] |