【问题标题】:Update a struct column in bigquery using another table in bigquery having string columns使用 bigquery 中具有字符串列的另一个表更新 bigquery 中的结构列
【发布时间】:2022-12-22 00:17:38
【问题描述】:

我有一个带有模式的表 A

ID -> type(String)
experience -> type(Array of Struct)

A.experience 列有 6 个字符串类型的子字段。

我希望使用另一个表 B(可以根据 ID 连接)更新 A.experience,并且还有 6 列对应于 A.experience 中的键。

表 B 中的所有列都是 String 类型。

这是我试过的

update  A
set A.experience = (company,starts_at,ends_at,eid,title,location) 
from B
where A.id = B.id;

任何帮助,将不胜感激。谢谢!

【问题讨论】:

  • 这回答了你的问题了吗? Update struct or nested field in bigquery
  • @Jaytiger 我可以利用你的帮助;我可以在表 A 中增加 6 列,然后更新 A.experience = STRUCT(A.1,A.2...A.6) ;稍后我将删除这 6 列。这就是我现在能想到的
  • 有什么理由创建额外的 6 列来更新 A.experience 吗?我认为您可以使用 B 的列直接更新 A.experience。
  • @Jaytiger 我无法将 B 的值分配给 A 的 Struct 列。所以我想到了创建额外临时列的解决方法。然后我可以使用 STRUCT() 来赋值。似乎有点矫枉过正,但我​​找不到其他任何东西。我什至无法更新结构的单个条目,这是另一个问题。
  • @AlmightyHeathcliff 如果您可以添加一些示例数据或至少添加问题中两个表的架构,那会更好。

标签: google-bigquery


【解决方案1】:

不确定我清楚地理解你的问题,但你似乎想这样做。如果我错了,请告诉我。

CREATE TEMP TABLE A AS
SELECT 'id1' id, 
       [STRUCT('C1'AS company, '2010-01-01 11:00:00' AS starts_at, '2010-01-01 12:00:00' AS ends_at, 'eid1' AS eid, 'title1' AS title, 'loc1' AS location),
        STRUCT('C2', '2011-01-01 11:00:00', '2011-02-02 13:00:00', 'eid2', 'title2', 'loc2')] AS experience;

CREATE TEMP TABLE B AS 
SELECT 'id1' AS id, 'C2_new' company, '2011-02-01 11:00:00' starts_at, '2011-03-02 13:00:00' ends_at, 'eid2_new' eid, 'title_new' title, 'loc2_new' location;

UPDATE A
   SET A.experience = ARRAY(SELECT AS STRUCT B.company, B.starts_at, B.ends_at, B.eid, B.title, B.location FROM UNNEST(A.experience))
  FROM B
 WHERE A.id = B.id;
 
SELECT * FROM A;

【讨论】:

  • 感谢您的回答,A.experience 列实际上是一个struct 数组。我修改了您的解决方案并运行了它,但我收到此错误: Value of type STRUCT<company STRING, starts_at STRING, ends_at STRING, ...> cannot be assigned to A.profile_experience, which has type ARRAY<STRUCT<company STRING, starts_at STRING, ends_at STRING, ...>>
  • 我明白了,那什么模式表已 ?是吗STRUCT<公司 STRING, starts_at STRING, ends_at STRING, ...>你想更新所有结构具有相同结构值 B 的 A.expereince 数组的 s?
  • 正如 JayTiger 所提到的,如果是这种情况,则可能是 SET A.experience = [B.experience] 这将解决错误。
猜你喜欢
  • 2021-06-01
  • 2022-12-12
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多