我想以下面的格式在BigQuery(Legacy)中创建表:
-- +---------+---------+
-- | Var1 | Var2 +
-- +---------+---------+
-- | x | 1 +
-- | | 2 +
-- | | 3 +
-- | y | 1 +
-- +---------+---------+
下面的代码可以逐行手动创建表。我正在寻找指定值1,2,3属于var1等中的x的代码。
SELECT SPLIT(Var1x) AS Var1
FROM (SELECT 'x,y' AS Var1x)
也许如果有一种方法如何将列转换为一个重复的字段可能会有所帮助。像你有y列和x1 - x5列的东西,我们可以将它们转换成只有一个x重复的列。有什么想法吗?
答案 0 :(得分:0)
#legacySQL
SELECT
Var1, NEST(Var2) AS Var2
FROM
(SELECT 'x' AS Var1, 1 AS Var2),
(SELECT 'x' AS Var1, 2 AS Var2),
(SELECT 'x' AS Var1, 3 AS Var2),
(SELECT 'y' AS Var1, 1 AS Var2)
GROUP BY Var1
请注意 - 为了保留此结构,您需要选择destination table
并检查allow large result
并在Web UI中取消选中flatten results
- 否则BigQuery Legacy SQL会自动展平大多数的outter查询输出
上面验证的另一种方法是计算行数(应该是两行而不是四行)
#legacySQL
SELECT COUNT(1) FROM (
SELECT
Var1, NEST(Var2) AS Var2
FROM
(SELECT 'x' AS Var1, 1 AS Var2),
(SELECT 'x' AS Var1, 2 AS Var2),
(SELECT 'x' AS Var1, 3 AS Var2),
(SELECT 'y' AS Var1, 1 AS Var2)
GROUP BY Var1
)
虽然上面确实回答了您的原始问题 - 我坚决建议使用标准SQL - 请参阅下面各自的版本:
#standardSQL
SELECT 'x' as Var1, [1,2,3] as Var2 UNION ALL
SELECT 'y', [1]