How to exclude NULLs from ARRAY so query won't fail

前端 未结 3 695
栀梦
栀梦 2020-12-31 01:26

ARRAY_AGG aggregate function includes NULLs in the arrays it builds. When such arrays are part of query result, query fails with error:

相关标签:
3条回答
  • 2020-12-31 02:08

    Another interesting use-case would be if you do not want to lose that NULL elements but rather want to substitute it with some default value. For example -999

    Below will do this

    #standardSQL
    SELECT ARRAY_AGG(IFNULL(x,-999)) FROM UNNEST([1,NULL,2,3]) x   
    

    And in case if you want distinct elements only -

    #standardSQL
    SELECT ARRAY_AGG(DISTINCT IFNULL(x,-999)) FROM UNNEST([1,NULL,2,3,1,NULL]) x
    
    0 讨论(0)
  • 2020-12-31 02:14

    Glad you asked! BigQuery supports IGNORE NULLS and RESPECT NULLS modifiers in some of the aggregate functions, including ARRAY_AGG, so your query becomes

    #standardSQL
    SELECT ARRAY_AGG(x IGNORE NULLS) FROM UNNEST([1,NULL,2,3]) x
    

    and it passes producing [1,2,3]. More details are in the documentation.

    0 讨论(0)
  • 2020-12-31 02:23

    Complementing @Mikhail Berlyant's answer, sometimes you want to keep the NULL values somehow but can't use a placeholder. For example, if you want a boolean array with NULLs, where you can't substitute NULL for true/false.

    A nice workaround is wrapping the values inside a STRUCT, then constructing the array:

    #standardSQL
    SELECT ARRAY_AGG(STRUCT(x)) AS struct_array FROM UNNEST([1,NULL,2,3]) x
    
    0 讨论(0)
提交回复
热议问题