我在Bigquery中创建了一个嵌套表。输入需要是JSON。我需要输入以下示例:
{" store_nbr":" 1234"," sls_dt":" 2014-01-01 00:00:00",& #34;项目":[{" sku":" 3456"," sls_amt":" 9.99",&#34 ;折扣":[{" disc_nbr":" 1"" disc_amt":" 0.99"},{&#34 ; disc_nbr":" 2"" disc_amt":" 1.00"}]]
我在大查询中有扁平的表格。我已经阅读过nest可以帮助重复json的旋转。我在另一个线程中看到了一个非常好的例子。
Nest multiple repeated fields in BigQuery
但这只适用于一个级别的嵌套。我需要做两个级别"项目" - > "折扣&#34 ;.任何建议表示赞赏。
答案 0 :(得分:3)
请尝试以下
你可以抛光细微差别,但总的来说它应该给你良好的开端!
SELECT *
FROM JS(
( // input table
SELECT store_nbr, sls_dt, NEST(CONCAT(STRING(item_sku), '|', STRING(sls_amt), '|', STRING(discounts))) AS items
FROM (
SELECT store_nbr, sls_dt, item_sku, sls_amt, GROUP_CONCAT(CONCAT(STRING(disc_nbr), ',', STRING(disc_amt)), ';') AS discounts
FROM
(SELECT 1234 AS store_nbr, "2014-01-01 00:00:00" AS sls_dt, 3456 AS item_sku, 9.99 AS sls_amt, 1 AS disc_nbr, 0.99 AS disc_amt),
(SELECT 1234 AS store_nbr, "2014-01-01 00:00:00" AS sls_dt, 3456 AS item_sku, 9.99 AS sls_amt, 2 AS disc_nbr, 1.00 AS disc_amt),
(SELECT 1234 AS store_nbr, "2014-01-01 00:00:00" AS sls_dt, 2345 AS item_sku, 7.99 AS sls_amt, 1 AS disc_nbr, 0.59 AS disc_amt),
(SELECT 1234 AS store_nbr, "2014-01-01 00:00:00" AS sls_dt, 4567 AS item_sku, 7.99 AS sls_amt, 1 AS disc_nbr, 0.59 AS disc_amt),
(SELECT 1234 AS store_nbr, "2014-01-01 00:00:00" AS sls_dt, 4567 AS item_sku, 7.99 AS sls_amt, 2 AS disc_nbr, 0.69 AS disc_amt),
(SELECT 1234 AS store_nbr, "2014-01-01 00:00:00" AS sls_dt, 4567 AS item_sku, 7.99 AS sls_amt, 3 AS disc_nbr, 0.79 AS disc_amt),
(SELECT 2345 AS store_nbr, "2014-01-02 00:00:00" AS sls_dt, 3456 AS item_sku, 9.99 AS sls_amt, 1 AS disc_nbr, 0.99 AS disc_amt),
(SELECT 2345 AS store_nbr, "2014-01-02 00:00:00" AS sls_dt, 3456 AS item_sku, 9.99 AS sls_amt, 2 AS disc_nbr, 1.00 AS disc_amt),
(SELECT 2345 AS store_nbr, "2014-01-02 00:00:00" AS sls_dt, 4567 AS item_sku, 7.99 AS sls_amt, 1 AS disc_nbr, 0.59 AS disc_amt),
GROUP BY store_nbr, sls_dt, item_sku, sls_amt
) GROUP BY store_nbr, sls_dt
),
store_nbr, sls_dt, items, // input columns
"[ // output schema
{'name': 'store_nbr', 'type': 'INTEGER'},
{'name': 'sls_dt', 'type': 'STRING'},
{'name': 'items', 'type': 'RECORD',
'mode': 'REPEATED',
'fields': [
{'name': 'sku', 'type': 'STRING'},
{'name': 'sls_amt', 'type': 'FLOAT'},
{'name': 'discounts', 'type': 'RECORD',
'mode': 'REPEATED',
'fields': [
{'name': 'disc_nbr', 'type': 'INTEGER'},
{'name': 'disc_amt', 'type': 'FLOAT'}
]
}]
}]",
"function(row, emit) { // function
var items = [];
for (var i = 0; i < row.items.length; i++) {
x = row.items[i].split('|');
var discounts = [];
y = x[2].split(';');
for (var j = 0; j < y.length; j++) {
discount = y[j].split(',');
discounts.push({disc_nbr:parseInt(discount[0]), disc_amt:parseFloat(discount[1])})
}
items.push({sku:x[0], sls_amt:parseFloat(x[1]), discounts: discounts});
};
emit({
store_nbr: row.store_nbr,
sls_dt: row.sls_dt,
items: items
});
}"
)
结果如下
预期架构