Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IFNULL() in BigQuery - Standard SQL?

I would like to know how to use the IFNULL() BigQuery Standard SQL function properly. This is my current data structure. The columns named "key" and "stringColumn" store strings. Meanwhile, the column named "integerColumn" stores integers:

enter image description here

I would like to create a new column named "singleValueColumn" that takes the value of the "stringColumn" or "integerColumn" that is not null:

enter image description here

This is my BigQuery Standard SQL query:

SELECT  key,
        value.string_value as stringColumn,
        value.int_value as integerColumn,
        IFNULL(value.string_value, value.int_value) as singleValueColumn

FROM `com_skytracking_ANDROID.app_events_*`, 
      UNNEST(event_dim) as event,
      UNNEST(event.params) as event_param

WHERE event.name = "order_event"

However, when I run the query I am getting this error:

Error: No matching signature for function IFNULL for argument types: STRING, INT64. Supported signature: IFNULL(ANY, ANY) at [4:9]

Thanks for your help.

like image 786
Milton Avatar asked Apr 01 '18 08:04

Milton


People also ask

How do I check if a value is NULL in BigQuery?

BigQuery IFNULL() FunctionIf expr is NULL, return null_result. Otherwise, return expr. If expr is not NULL, null_result is not evaluated. expr and null_result can be any type and must be implicitly coercible to a common supertype.

What is Nan in BigQuery?

IS_NAN DescriptionReturns NULL for NULL inputs.

How do I filter NULL values in BigQuery?

The syntax is as follows: Select * from table_source where column is not NULL; If you want to read more about the where operator, please refer to the documentation. In addition if you want to replace the null values, you can use the IFNNULL() function.

Does BigQuery use standard SQL?

BigQuery supports the Google Standard SQL dialect, but a legacy SQL dialect is also available. If you are new to BigQuery, you should use Google Standard SQL as it supports the broadest range of functionality. For example, features such as DDL and DML statements are only supported using Google Standard SQL.


1 Answers

Check this doc. I think you need to cast the int_value as a string:

IFNULL(value.string_value, CAST(value.int_value AS STRING)) AS singleValueColumn
like image 100
Xiaoxia Lin Avatar answered Oct 19 '22 15:10

Xiaoxia Lin