我试图将数组值解除到BigQuery / PSQL中的不同行。
这就是我现在所拥有的::
select
';' + convert(varchar, emp_id),
';' + convert(varchar, emp_dob),
';' + emp_ssid
from ...
我想转换它,如下面BigQuery中所示。
Col_1 col_2
A array: {1,2,3,4,5}
B array: {1,2,3}
C array: {4,5,6}
答案 0 :(得分:1)
使用standard SQL(取消选中“显示选项”下的“使用旧版SQL”框),您可以执行以下操作:
WITH MyTable AS (
SELECT 'A' AS Col_1, [1, 2, 3, 4, 5] AS col_2
UNION ALL SELECT 'B', [1, 2, 3]
UNION ALL SELECT 'C', [4, 5, 6])
SELECT Col_1, col_2 FROM MyTable t, t.col_2;
+-------+-------+
| Col_1 | col_2 |
+-------+-------+
| A | 1 |
| A | 2 |
| A | 3 |
| A | 4 |
| A | 5 |
| B | 1 |
| B | 2 |
| B | 3 |
| C | 4 |
| C | 5 |
| C | 6 |
+-------+-------+
这里“逗号”运算符相当于交叉连接,它具有使重复变平的效果。正如Pavan在上面指出的那样,您也可以使用FLATTEN
运算符在遗留SQL中完成此操作,尽管语法不同。
答案 1 :(得分:0)
如果您受BigQuery Legacy SQL的约束 - 请尝试以下
SELECT col_1, SPLIT(REPLACE(REPLACE(SPLIT(col_2, ':'), '{', ''), '}', ''), ',') AS elem
FROM
(SELECT 'A' AS col_1, 'array: {1,2,3,4,5}' AS col_2),
(SELECT 'B' AS col_1, 'array: {1,2,3}' AS col_2),
(SELECT 'C' AS col_1, 'array: {4,5,6}' AS col_2)
HAVING elem <> 'array'
col_1 elem
A 1
A 2
A 3
A 4
A 5
B 1
B 2
B 3
C 4
C 5
C 6