BigQuery:如何从REPEATED记录中仅提取某些字段作为另一个REPEATED字段

时间:2018-11-04 22:36:24

标签: google-bigquery

这是BigQuery中的示例表:

WITH test AS (
  SELECT
    [ 
      STRUCT("Rudisha" as name, 123 as id),
      STRUCT("Murphy" as name, 124 as id),
      STRUCT("Bosse" as name, 125 as id),
      STRUCT("Rotich" as name,  126 as id)
    ] AS data

    UNION

    [
      STRUCT("Lewandowski" as name, 127 as id),
      STRUCT("Kipketer" as name, 128 as id),
      STRUCT("Berian" as name, 129 as id)
    ] AS data
)

在这里,我要提取记录字段(“数据”)中的“ id”字段作为可重复字段。因此,行数将保持不变,但只有ids字段是重复类型:

ids: [123, 124, 125, 126]
ids: [127, 128, 129]

我该怎么做?谢谢!

1 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

#standardSQL
WITH test AS (
  SELECT
    [ 
      STRUCT("Rudisha" AS name, 123 AS id),
      STRUCT("Murphy" AS name, 124 AS id),
      STRUCT("Bosse" AS name, 125 AS id),
      STRUCT("Rotich" AS name,  126 AS id)
    ] AS data
    UNION ALL SELECT
    [
      STRUCT("Lewandowski" AS name, 127 AS id),
      STRUCT("Kipketer" AS name, 128 AS id),
      STRUCT("Berian" AS name, 129 AS id)
    ] AS data
)
SELECT ARRAY(SELECT id FROM UNNEST(data)) ids
FROM test