BigQuery:从多个嵌套列中选择*替换

时间:2018-03-19 15:16:34

标签: google-bigquery

我试图删除嵌套在多个记录中的字段。在this问题之后,我尝试了以下操作(删除了包含路径a.b.d的列d),但收到错误:Unrecognized name: b at [6:68]

WITH T AS (
  SELECT * from unnest([struct(struct(struct(10 as c,'test' as d) as b,'field2' as field2) as a)])

)

select * replace ((select as struct a.* replace( (select as struct b.* except (d)) as b)) as a)  from T;

1 个答案:

答案 0 :(得分:3)

从我在您的问题中理解的内容 - 您的原始行如下

   
STRUCT(STRUCT(STRUCT(10 AS c,'test' AS d) AS b,'field2' AS field2) AS a) original_struct  

你需要把它送到

STRUCT(STRUCT(STRUCT(10 AS c) AS b,'field2' AS field2) AS a) original_struct 

下面是BigQuery Standard SQL为此完成

#standardSQL
WITH t AS (
  SELECT STRUCT(STRUCT(STRUCT(10 AS c,'test' AS d) AS b,'field2' AS field2) AS a) original_struct 
) 
SELECT 
  original_struct,
  (
    SELECT AS STRUCT (
      SELECT AS STRUCT (
        SELECT AS STRUCT * EXCEPT(d) 
        FROM UNNEST([original_struct.a.b]) b
      ) b, 
      original_struct.a.field2
    ) a
  ) new_struct
FROM t

原始结构看起来像

Row original_struct.a.b.c   original_struct.a.b.d   original_struct.a.field2    
1   10                      test                    field2   

和新结构

Row new_struct.a.b.c    new_struct.a.field2  
1   10                  field2   

因此,要进一步将其应用于您的特定情况 - 请参阅下面的示例

#standardSQL
WITH t AS (
  SELECT 1 id, 2 x, 3 y, STRUCT(STRUCT(STRUCT(10 AS c,'test' AS d) AS b,'field2' AS field2) AS a) xyz, 4 v, 5 w 
) 
SELECT * REPLACE (
  (
    SELECT AS STRUCT (
      SELECT AS STRUCT (
        SELECT AS STRUCT * EXCEPT(d) 
        FROM UNNEST([xyz.a.b]) b
      ) b, 
      xyz.a.field2
    ) a
  ) AS xyz)
FROM t