Case
Note
case
is currently experimental and may change behavior in the near future
Info
case
was previously (PRQL 0.4 to 0.5) called switch
and renamed to case
in PRQL 0.6.0.
PRQL uses case
for both SQL’s CASE
and IF
statements. Here’s an example:
PRQL
from employees
derive distance = case [
city == "Calgary" => 0,
city == "Edmonton" => 300,
]
SQL
SELECT
*,
CASE
WHEN city = 'Calgary' THEN 0
WHEN city = 'Edmonton' THEN 300
ELSE NULL
END AS distance
FROM
employees
If no condition is met, the value takes a null
value. To set a default, use a true
condition:
PRQL
from employees
derive distance = case [
city == "Calgary" => 0,
city == "Edmonton" => 300,
true => "Unknown",
]
SQL
SELECT
*,
CASE
WHEN city = 'Calgary' THEN 0
WHEN city = 'Edmonton' THEN 300
ELSE 'Unknown'
END AS distance
FROM
employees