在BIGQUERY中的一列中插入多个记录类型的数据。

huangapple go评论134阅读模式
英文:

Insert several data in BIGQUERY in a column of type RECORD

问题

我在BigQuery中有一张具有以下结构的表:

CREATE TABLE MY_TABLE (
  name STRING,
  values STRUCT<model STRING, group BOOL>
)

我想要在与相同名称相关的values列中插入多条数据。

这是我正在执行的操作:

INSERT INTO MY_TABLE  (name ,values)
VALUES (
  'export',
  STRUCT('model1', false)
),
(
  'export',
  STRUCT('model2', true)
)

这是我得到的结果:

name values.model values.group
export model1 false
export model2 true

这是我想要的:

name values.model values.group
export model1 false
model2 true

如何在name列的记录类型列中插入多个数据,而不必重复所有数据?我需要每个名称都有一个记录,但包含多个值。我不知道是否这是创建表以实现此目的的正确方式。

英文:

I have a table in bigquery with the following structure:

CREATE TABLE MY_TABLE (
  name STRING,
  values STRUCT&lt;model STRING, group BOOL&gt;
)

What I want is to insert several data in the values column related to the same name.

This is what I'm doing:

INSERT INTO MY_TABLE  (name ,values)
VALUES (
  &#39;export&#39;,
  STRUCT(&#39;model1&#39;, false)
),
(
  &#39;export&#39;,
  STRUCT(&#39;model2&#39;, true)
)

This is what I get:

name values.model values.group
export model1 false
export model2 true

This is what I want:

name values.model values.group
export model1 false
model2 true

How can I insert several data for in the RECORS type column for the name column without having to repeat all the data? I need a record for each name, but that contains several values.
I don't know if this is the correct way to create the table to achieve this.

答案1

得分: 0

以下是您要求的代码部分的翻译:

CREATE TEMP TABLE MY_TABLE (
  name STRING,
  values ARRAY<STRUCT<model STRING, `group` BOOL>>;
);

INSERT INTO MY_TABLE (name, values)
VALUES ('export', [STRUCT('model1', false), STRUCT('model2', true)]),
       ('import', [STRUCT('model3', true), STRUCT('model4', false)])
;

SELECT * FROM MY_TABLE;

查询结果

在BIGQUERY中的一列中插入多个记录类型的数据。

或者,

CREATE TEMP TABLE MY_TABLE AS
SELECT 'export' AS name, 
       [STRUCT('model1' AS model, false AS `group`), ('model2', true)] AS values
 UNION ALL
SELECT 'import', [('model3', true), ('model4', false)] AS values;
英文:

You might consider below.

CREATE TEMP TABLE MY_TABLE (
  name STRING,
  values ARRAY&lt;STRUCT&lt;model STRING, `group` BOOL&gt;&gt;
);

INSERT INTO MY_TABLE (name ,values)
VALUES ( &#39;export&#39;, [STRUCT(&#39;model1&#39;, false), STRUCT(&#39;model2&#39;, true)] ),
       ( &#39;import&#39;, [STRUCT(&#39;model3&#39;, true), STRUCT(&#39;model4&#39;, false)] )
;

SELECT * FROM MY_TABLE;

Query results

在BIGQUERY中的一列中插入多个记录类型的数据。

or,

CREATE TEMP TABLE MY_TABLE AS
SELECT &#39;export&#39; AS name, 
       [STRUCT(&#39;model1&#39; AS model, false AS `group`), (&#39;model2&#39;, true)] AS values
 UNION ALL
SELECT &#39;import&#39;, [(&#39;model3&#39;, true), (&#39;model4&#39;, false)] AS values;

huangapple
  • 本文由 发表于 2023年3月7日 21:30:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定