如果Key为null,则BigQuery嵌套表将从嵌套表中删除

时间:2019-11-13 08:10:25

标签: google-bigquery

简单的问题(希望如此)

我有一个嵌套表,看起来像这样:

enter image description here

并且当DWHKey为null时,此行的产品数组应如下所示(空而不是null):

"product": []

不工作((

ARRAY_AGG(pr IGNORE NULLS) as product

加入我正在做的事情:

SELECT 
                        x.SourceSystemCode
                        ...
                        ,product
                        ,payment
                    FROM (
                        SELECT 
                            he.SourceSystemCode
                            ...
                            ,ARRAY_AGG(pr IGNORE NULLS) as product
                    FROM header_table as he
                    LEFT JOIN product_table AS pr ON pr.DWHKey = he.DWHKey
                    GROUP BY he.SourceSystemCode..
                    ) x
                    JOIN (
                        SELECT 
                            he.SourceSystemCode
                            ...
                            ,ARRAY_AGG(pay IGNORE NULLS) AS payment  
                        FROM header_table as he
                        LEFT JOIN payment_table AS pay ON pay.DWHKey = he.DWHKey
                        GROUP BY he.SourceSystemCode...
                    ) y
                    ON x.DWHKey = y.DWHKey

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法:

with data as (
select '2019-1-1' CreateDate, [struct<DWHKey string, LineNumber int64>("hasKey", 1), (null, null)] product union all
select '2019-1-2', [struct<DWHKey string, LineNumber int64>(null, null), (null, null), (null, null)] union all
select '2019-1-2', [struct<DWHKey string, LineNumber int64>("hasKey", 1), (null, null), ("hasKey", 1), (null, null)] 
) 
select * 
  replace (array(select as struct * from unnest(product) where DWHKey is not null) as product) 
from data;

输出:

+------------+-----------------------------------------------------------------------------+
| CreateDate |                                   product                                   |
+------------+-----------------------------------------------------------------------------+
| 2019-1-1   |                                      [{"DWHKey":"hasKey","LineNumber":"1"}] |
| 2019-1-2   |                                                                          [] |
| 2019-1-2   | [{"DWHKey":"hasKey","LineNumber":"1"},{"DWHKey":"hasKey","LineNumber":"1"}] |
+------------+-----------------------------------------------------------------------------+