How to Write a Case Statement

The case statement statement in BigQuery is really easy to use, let’s take a look to the next example:

WITH employee AS (
   SELECT 'Smith' as last_name, 90000 as salary, 'human resources' as department UNION ALL
   SELECT 'Graue', 120000, 'accounting' UNION ALL
   SELECT 'Perez', 235000, 'accounting' UNION ALL
   SELECT 'Dunden',95000, 'engineering' UNION ALL
   SELECT 'Gritz', 140000, 'IT' 
)
select 
  case
    when salary >= 100000 and salary<200000 then '100k'
    when salary >= 200000 then 'more than 200k'
    else 'under_100k'
  end AS salary_level,
  case
    when department IN ('human resources','accounting') then 'admin'
    when department IN ('engineering','IT') then 'tech'
    else 'other'
  end AS tasks
from employee;

IN THIS PAGE