CASE function. Definition, syntax, examples and common errors using BigQuery Standard SQL. The CASE function allows you to perform conditional statements in SQL.
The CASE function allows you to perform conditional statements in BigQuery. This is a more complex implementation of IF.
| colour | primary_colour |
|---|---|
| blue | true |
| pink | false |
| black | false |
else_result, then the query will return NULL if not matched to the resultresult and else_result must be coercible to a common supertype. To add multiple expression -> result pairings, simply add another WHEN ... THEN clause (without commas) to your function.
Using the example data above, we could use the following query to identify which are the primary colours:
CASE expr
WHEN expr_to_match THEN result
[...]
[ELSE else_result]
ENDSELECT
colour,
CASE WHEN colour IN ('blue', 'red', 'yellow') THEN true ELSE false END AS primary_colour
FROM
(
SELECT
'blue' AS colour
UNION ALL
( SELECT
'pink' AS colour)
UNION ALL
( SELECT
'black' AS colour)
) AS table_3To account for strings being of different cases, we can use UPPER and LOWER to standardise all of the cases in both our expressions to match, and the match criteria.
Using the data above, we could modify our case statement as follows:
CASE WHEN colour ='blue' THEN true
WHEN colour = 'red' THEN true
WHEN colour = 'yellow' then true
ELSE false
END AS primary_colourCASE
WHEN UPPER(colour) IN ('BLUE', 'RED', 'YELLOW') THEN true
ELSE false
END AS primary_colourAs long as your expressions are of the right type you can use comparison operators (e.g. >, <, >=) to compare dates:
CASE
WHEN date_col BETWEEN '2020-10-01' and '2021-09-30' THEN 'FY 2021'
WHEN date_col<'2020-10-01' then 'pre FY 2021'
END fiscal_dateNote: BETWEEN is inclusive so it will return true for both 2020-10-01 AND 2021-09-30.
| date_col | fiscal_date |
|---|---|
| 2020-10-01 | FY 2021 |
| 2021-09-30 | FY 2021 |
| 2019-10-01 | pre FY 2021 |
To group by the results of a CASE function, just make sure you add the name of the column to your GROUP BY clause.
| f0_ | Region |
|---|---|
| -1 | East |
| 4 | West |
To aggregate by a case column, make sure your results are numbers:
SELECT
date_col,
CASE WHEN (date_col BETWEEN '2020-10-01' AND '2021-09-30') THEN 'FY 2021' WHEN (date_col < '2020-10-01') THEN 'pre FY 2021' END AS fiscal_date
FROM
(
SELECT
'2020-10-01' AS date_col
UNION ALL
( SELECT
'2021-09-30' AS date_col)
UNION ALL
( SELECT
'2019-10-01' AS date_col)
) AS table_3| east_total | west_total |
|---|---|
| -1 | 4 |
This happens when your conditional expression is invalid. For example:
SELECT
SUM(number),
CASE WHEN city IN ('New York', 'Miami') THEN 'East' ELSE 'West' END AS Region
FROM
(
SELECT
2 AS number,
'New York' AS city
UNION ALL
( SELECT
4 AS number,
'Los Angeles' AS city)
UNION ALL
( SELECT
(-3) AS number,
'Miami' AS city)
) AS table_3
GROUP BY
RegionYou can resolve this by making sure your condition column and criteria are compatible types:
SELECT
SUM(CASE WHEN city IN ('New York', 'Miami') THEN number ELSE 0 END) AS east_total,
SUM(CASE WHEN city IN ('Los Angeles') THEN number ELSE 0 END) AS west_total
FROM
(
SELECT
2 AS number,
'New York' AS city
UNION ALL
( SELECT
4 AS number,
'Los Angeles' AS city)
UNION ALL
( SELECT
(-3) AS number,
'Miami' AS city)
) AS table_3This error occurs when your result types are different. In this example returning 1 in one case and 'hello' in another won't work.
CASE WHEN number_column = 'string' ....To resolve this, make sure your result types are compatible and coercible.
CASE WHEN CAST(number_column as STRING) = 'string' ...CASE WHEN (CAST(bq.Avg_monthly_searches AS STRING) = 'total') THEN 1 ELSE 'hello' ENDCASE WHEN (CAST(bq.Avg_monthly_searches AS STRING) = 'total') THEN '1' ELSE 'hello' END