BigQuery UDF: How to sum(case when)

18 Views Asked by At

I'm analyzing a table and want to write a UDF for this.

CREATE TEMP FUNCTION null_sum(col_name STRING) AS (
  (
    SELECT 
     sum(case when col_name < 0  then 1 else 0 end ) as imputed_nulls,
    from `project.dataset.table_20240210`
  )
);

SELECT null_sum("my_col") as my_col_zeros;

But this leads to an error:

No matching signature for operator < for argument types: STRING, INT64. Supported signature: ANY < ANY; failed to parse CREATE [TEMP] FUNCTION statement

How do I get BQ to parse the column name not just as a string? BTW this UDF works if I change the temp function to look for NULLS.

0

There are 0 best solutions below