简体   繁体   English

BigQuery SQL:select 结构为单个记录,而不是记录数组

[英]BigQuery SQL: select struct as single record, not array of records

I want to create a RECORD as the outcome of a select in BigQuery standard SQL.我想创建一个 RECORD 作为 BigQuery 标准 SQL 中 select 的结果。

If I run this SQL snippet,如果我运行这个 SQL 片段,

WITH 
mock_data AS (
    SELECT 'foo1' as foo, 'bar1' as bar, 'bla1' as bla UNION ALL
    SELECT 'foo2' as foo, 'bar2' as bar, 'bla2' as bla
)
SELECT 
    *,
    STRUCT(
        m.foo as foo,
        m.bar as bar
    ) as foobar
FROM mock_data m

the output of foobar is an array of records, not a single record. foobar 的 output 是一个记录数组,不是单条记录。 查询的输出

How could I have the foobar column be a single record and not an array of records?我怎么能让foobar列成为单个记录而不是记录数组? Thanks a lot in advance!非常感谢!

The UNNEST operator flattens arrays into rows, ie it breaks an array into several rows. UNNEST运算符将 arrays 展平为行,即将数组分成几行。 STRUCT elements get broken down into multi-column records, which is what I think you require. STRUCT元素被分解成多列记录,这就是我认为你需要的。 Try something like this out:尝试这样的事情:

SELECT
   *,
   UNNEST(
      STRUCT(
        m.foo as foo,
        m.bar as bar
      ) as foobar
   )

It is a single record - not an array它是单个记录 - 不是数组

You can clearly see this in JSON Tab你可以在 JSON 选项卡中清楚地看到这一点

在此处输入图像描述

Also you can go to "Job Information" Tab也可以go到“职位信息”标签

在此处输入图像描述

and click on "Temporary Table" to see the schema of output然后单击“临时表”以查看 output 的架构

在此处输入图像描述

In the BigQuery IDE that I am using - it is even more visible在我使用的 BigQuery IDE 中 - 它更加明显

在此处输入图像描述

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

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