简体   繁体   English

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

[英]BigQuery unnest to new columns instead of new rows

I have a BigQuery table with nested and repeated columns that I'm having trouble unnesting to columns instead of rows.我有一个 BigQuery 表,其中包含嵌套和重复的列,我无法取消嵌套到列而不是行。

This is what it the table looks like:这就是表格的样子:

  {
    "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"
      }
    ]
  },

What I want is to return a table with each one of the nested fields as an individual column so I have a clean dataframe to work with:我想要的是将每个嵌套字段作为单独的列返回一个表,这样我就有了一个干净的 dataframe 可以使用:

  {
    "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"
  },

I tried using UNNEST as below, however it creates new rows with only one set of additional columns, so not what I'm looking for.我尝试如下使用 UNNEST,但是它创建的新行只有一组附加列,所以不是我要找的。

SELECT * 
FROM table,
UNNEST(Matches) as items

Any solution for this?有什么解决办法吗? Thank you in advance.先感谢您。

Consider below approach考虑以下方法

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 applied to sample data in y our question - output is如果应用于 y 中的示例数据,我们的问题 - output 是

在此处输入图像描述

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.如果没有匹配项,则整行不包含在 output 表中,而我想保留该记录及其所具有的字段。 How can I modify to keep all records?我怎样才能修改以保留所有记录?

use left join instead as in below使用 left join 代替,如下所示

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM