Case

Note

case is currently experimental and may change behavior in the near future

PRQL uses case for both SQL’s CASE and IF statements. Here’s an example:

PRQL

  1. from employees
  2. derive distance = case [
  3. city == "Calgary" => 0,
  4. city == "Edmonton" => 300,
  5. ]

SQL

  1. SELECT
  2. *,
  3. CASE
  4. WHEN city = 'Calgary' THEN 0
  5. WHEN city = 'Edmonton' THEN 300
  6. ELSE NULL
  7. END AS distance
  8. FROM
  9. employees

If no condition is met, the value takes a null value. To set a default, use a true condition:

PRQL

  1. from employees
  2. derive distance = case [
  3. city == "Calgary" => 0,
  4. city == "Edmonton" => 300,
  5. true => "Unknown",
  6. ]

SQL

  1. SELECT
  2. *,
  3. CASE
  4. WHEN city = 'Calgary' THEN 0
  5. WHEN city = 'Edmonton' THEN 300
  6. ELSE 'Unknown'
  7. END AS distance
  8. FROM
  9. employees