Bigquery: Least but not NULL
Let’s start with a simple SQL statement that returns the smallest value from each of several columns (similar to min
only not across observations, but columns):
SELECT LEAST(date1, date2, date3) as min_date FROM tbl
In BigQuery it works nicely till you encounter NULL values, then it will allways pick the NULL. Unfortunately it does not provide a IGNORE NULLS option such as ARRAY_AGG or others do. However, similar behaviour can be archieved with the following UDF:
CREATE TEMP FUNCTION LEAST_ARRAY(arr ANY TYPE) AS ((
SELECT min(a) FROM UNNEST(arr) a WHERE a is not NULL
));
Then you just have to wrap the columns in an array and call the UDF:
SELECT LEAST_ARRAY([date1, date2, date3]) min_date FROM tbl