问题描述
a b c b c
0 0 1 2 3 4
1 5 6 7 8 9
我想将其转换为嵌套表:
a b c
0 0 1 2
1 3 4
2 5 6 7
3 8 9
我设法将其转换为这种格式
a b c
0 0 1 2
1 0 3 4
2 5 6 7
3 5 8 9
但是有更好的方法将其从原始csv转换为所需格式吗?
解决方法
以下是用于BigQuery标准SQL
#standardSQL
WITH `I have managed to transform it to this format` AS (
-- replace below with whatever query you used to get to this format
SELECT 0 a,1 b,2 c UNION ALL
SELECT 0,3,4 UNION ALL
SELECT 5,6,7 UNION ALL
SELECT 5,8,9
)
SELECT a,ARRAY_AGG(b) b,ARRAY_AGG(c) c
FROM `I have managed to transform it to this format`
GROUP BY a
有输出
Row a b c
1 0 1 2
3 4
2 5 6 7
8 9
根据您最近的评论The column names in BQ [actually] are a b1 c1 b2 c2
更新
在这种情况下,它很简单
#standardSQL
SELECT a,[b1,b2] b,[c1,c2] c
FROM `project.dataset.table`
您可以使用问题中的示例数据来测试,玩转上面的示例
#standardSQL
WITH `project.dataset.table` AS (
SELECT 0 a,1 b1,2 c1,3 b2,4 c2 UNION ALL
SELECT 5,7,9
)
SELECT a,c2] c
FROM `project.dataset.table`
有输出
Row a b c
1 0 1 2
3 4
2 5 6 7
8 9