BigQuery 取消嵌套到新列而不是新行

BigQuery unnest to new columns instead of new rows

我有一个 BigQuery table,其中包含嵌套和重复的列,我无法取消嵌套到列而不是行。

这是 table 的样子:

  {
    "dateTime": "Tue Mar 01 2022 11:11:11 GMT-0800 (Pacific Standard Time)",
    "Field1": "123456",
    "Field2": true,
    "Matches": [
      {
        "ID": "abc123",
        "FieldA": "asdfljadsf",
        "FieldB": "0.99"
      },
      {
        "ID": "def456",
        "FieldA": "sdgfhgdkghj",
        "FieldB": "0.92"
      },
      {
        "ID": "ghi789",
        "FieldA": "tfgjyrtjy",
        "FieldB": "0.64"
      }
    ]
  },

我想要的是return一个table,每个嵌套字段作为一个单独的列,所以我有一个干净的数据框可以使用:

  {
    "dateTime": "Tue Mar 01 2022 11:11:11 GMT-0800 (Pacific Standard Time)",
    "Field1": "123456",
    "Field2": true,
    "Matches_1_ID": "abc123",
    "Matches_1_FieldA": "asdfljadsf",
    "Matches_1_FieldB": "0.99", 
    "Matches_2_ID": "def456",
    "Matches_2_FieldA": "sdgfhgdkghj",
    "Matches_2_FieldB": "0.92",
    "Matches_3_ID": "ghi789",
    "Matches_3_FieldA": "tfgjyrtjy",
    "Matches_3_FieldB": "0.64"
  },

我尝试如下使用 UNNEST,但是它创建的新行只有一组附加列,所以不是我要找的。

SELECT * 
FROM table,
UNNEST(Matches) as items

有什么解决办法吗?提前谢谢你。

考虑以下方法

select * from (
  select t.* except(Matches), match.*, offset + 1 as offset 
  from your_table t, t.Matches match with offset
)
pivot (min(ID) as id, min(FieldA) as FieldA, min(FieldB) as FieldB for offset in (1,2,3))             

如果应用于你的问题中的样本数据 - 输出是

if there is are no matches, that entire row is not included in the output table, whereas I want to keep that record with the fields it does have. How can I modify to keep all records?

改用左连接,如下所示

select * from (
  select t.* except(Matches), match.*, offset + 1 as offset 
  from your_table t left join t.Matches match with offset
)
pivot (min(ID) as id, min(FieldA) as FieldA, min(FieldB) as FieldB for offset in (1,2,3))