我正在尝试在Google BigQuery上合并具有相同架构的两个数据库。
我在这里关注合并示例:https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#merge_statement
但是,我的表具有嵌套列,即“ service.id”或“ service.description”
到目前为止,我的代码是:
MERGE combined_table
USING table1
ON table1.id = combined_table.id
WHEN NOT MATCHED THEN
INSERT(id, service.id, service.description)
VALUES(id, service.id, service.description)
但是,我得到了错误消息:Syntax error: Expected ")" or "," but got "."
,在.id
行的INSERT(...)
下有一个红色的弯曲下划线。
这是表的一部分架构的视图:
[
{
"name": "id",
"type": "STRING"
},
{
"name": "service",
"type": "RECORD",
"fields": [
{
"name": "id",
"type": "STRING"
},
{
"name": "description",
"type": "STRING"
}
]
},
{
"name": "cost",
"type": "FLOAT"
}
...
]
如何正确构造此INSERT(...)语句,以便可以包括嵌套列?
答案 0 :(得分:2)
语法错误:预期为“)”或“,”,但显示为“。”
看起来您的方向正确,请在文档中注意如何将值插入REPEATED列中,
您需要定义结构来指导BigQuery期望什么,例如:
STRUCT<created DATE, comment STRING>
这是文档中的完整示例
MERGE dataset.DetailedInventory T
USING dataset.Inventory S
ON T.product = S.product
WHEN NOT MATCHED AND quantity < 20 THEN
INSERT(product, quantity, supply_constrained, comments)
-- insert values like this
VALUES(product, quantity, true, ARRAY<STRUCT<created DATE, comment STRING>>[(DATE('2016-01-01'), 'comment1')])
WHEN NOT MATCHED THEN
INSERT(product, quantity, supply_constrained)
VALUES(product, quantity, false)
答案 1 :(得分:1)
我找到了答案。
当引用STRUCT的顶层时,BigQuery也会引用所有嵌套列。因此,如果我想插入const list = {"data":[{"type":"A","date":"2018-05","value":"153"},{"type":"B","date":"2018-05","value":"888"},{"type":"C","date":"2018-05","value":"999"},{"type":"D","date":"2018-05","value":"555"},{"type":"A","date":"2018-06","value":"148"},{"type":"B","date":"2018-06","value":"222"},{"type":"C","date":"2018-06","value":"555"},{"type":"D","date":"2018-06","value":"666"},{"type":"A","date":"2018-07","value":"156"},{"type":"B","date":"2018-07","value":"111"},{"type":"C","date":"2018-07","value":"333"},{"type":"D","date":"2018-07","value":"999"}],"number":"111-111"};
// const dates = [...new Set(list.data.map(item => item.date))];
const dates = Array.from(list.data.map(item => item.date));
console.log(dates);
// const types = [...new Set(list.data.map(item => item.type))];
const types = Array.from(list.data.map(item => item.type));
console.log(types)
const res = dates.map(date => {
const obj = {};
types.map(type => {
obj[type] = list.data.filter(item => item.date === date && item.type === type)[0].value;
});
obj.date = date;
return obj;
});
console.log(res);
及其所有子列(service.id和service.description),则只需在service
语句中包含service
。
以下代码有效:
INSERT(...)
这将合并所有子列,包括...
WHEN NOT MATCHED THEN
INSERT(id, service)
VALUES(id, service)
和service.id
。