我正在尝试更新如下所示的bigQuery数据库中的记录:
使用以下代码:
UPDATE `tottus-chile.espacio.nested_table`
SET addresses =
ARRAY(
SELECT AS STRUCT * REPLACE('sleep' AS
address)
FROM UNNEST(addresses)
)
WHERE first_name="pauli shore" AND EXISTS (
SELECT 1 FROM UNNEST(addresses) AS h
WHERE h.status="previous"
)
但是,如图所示,它使所有地址都读为“睡眠”,而不仅仅是“上一个”。
我必须承认,关于如何最好地处理嵌套表中的更改,我感到很困惑。是否有一种通用方法可以实现仅更新嵌套数组的“上一个”部分?
答案 0 :(得分:0)
这是您想要做的吗?
UPDATE `tottus-chile.espacio.nested_table`
SET addresses =
ARRAY(SELECT AS STRUCT a.* EXCEPT (address),
(CASE WHEN a.status = 'previous' THEN 'sleep' ELSE a.address END) as address
FROM UNNEST(addresses) a
)
WHERE first_name = 'pauli shore' AND
EXISTS (SELECT 1
FROM UNNEST(addresses) AS h
WHERE h.status = 'previous'
);
这确实重新排列了结构中的列-可以使用更多编码进行固定。我认为这可能是您想要做的。
编辑:
为避免移动,您可以显式列出各列:
UPDATE `tottus-chile.espacio.nested_table`
SET addresses =
ARRAY(SELECT AS STRUCT a.status,
(CASE WHEN a.status = 'previous' THEN 'sleep' ELSE a.address END) as address,
a.* EXCEPT (status, address)
FROM UNNEST(addresses) a
)
WHERE first_name = 'pauli shore' AND
EXISTS (SELECT 1
FROM UNNEST(addresses) AS h
WHERE h.status = 'previous'
);
这只是重建结构,但有条件地重新分配address
的值。