BigQuery数组中的适当列的数组

问题描述 投票:0回答:1

我正在尝试操纵它;

id | data
1  | [[item1,2,1.99],[item2,1,4.99]]
2  | [[item1,2,1.99]]

进入此:

id | A     | B | C    |
1  | item1 | 2 | 1.99 |
   | item2 | 1 | 4.99 |
2  | item1 | 2 | 1.99 |
...'

我最近的是;

id | data |
1  | item1, 2, 1.99 |
   | item2, 1, 4.99 |
2  | item1, 2, 1.99 |

如何将字符串数组拆分为列?

这是我当前的查询;

SELECT 
   id,
   ARRAY(SELECT * FROM UNNEST(SPLIT(SUBSTR(line_items, 3 , LENGTH(line_items) - 4),'], [')) AS line_items) AS line_items
FROM raw_data.sales 

此查询的作用相同;

SELECT 
   id,
   SPLIT(SUBSTR(line_items, 3 , LENGTH(line_items) - 4),'], [') AS line_items
FROM raw_data.sales
google-bigquery
1个回答
0
投票

假设您的嵌套数组的长度恒定,您应该可以使用以下查询。现在我没有实际测试的数据,所以如果您遇到问题,请告诉我。

SELECT
id,
-- Access each element of the array and transpose it
-- column-wise
d[OFFSET(0)] AS A,
d[OFFSET(1)] AS B,
d[OFFSET(2)] AS C
FROM
raw_data.sales,
-- Unnest your nested array. d will be unnested arrays
UNNEST(data) AS d
© www.soinside.com 2019 - 2024. All rights reserved.