如何使用 SQL 将具有多列数组的 table 展平?
How to use SQL to flatten a table with arrays in multiple columns?
如何使用数字和字母列中的数组转换此 table:
id | num | letter
-----+-----------+-----------
111 | [1, 2] | [a, b]
111 | [3, 4] | [c, d]
222 | [5, 6, 7] | [e, f, g]
进入这个table
id | num | letter
-----+-----+--------
111 | 1 | a
111 | 2 | b
111 | 3 | c
111 | 4 | d
222 | 5 | e
222 | 6 | f
222 | 7 | g
附录:
这里有一些 sql 可以尝试执行转换
with test as(
select * from (
values
(111, array[1,2], array['a','b']),
(111, array[3,4], array['c','d']),
(222, array[5,6,7], array['e','f', 'g'])
) as t (id, num, letter)
)
select
*
from test
PrestoDB 似乎支持 unnest()
多个参数:
select t.id, u.n, u.l
from test cross join
unnest(num, letter) as u(n, l)
如何使用数字和字母列中的数组转换此 table:
id | num | letter
-----+-----------+-----------
111 | [1, 2] | [a, b]
111 | [3, 4] | [c, d]
222 | [5, 6, 7] | [e, f, g]
进入这个table
id | num | letter
-----+-----+--------
111 | 1 | a
111 | 2 | b
111 | 3 | c
111 | 4 | d
222 | 5 | e
222 | 6 | f
222 | 7 | g
附录: 这里有一些 sql 可以尝试执行转换
with test as(
select * from (
values
(111, array[1,2], array['a','b']),
(111, array[3,4], array['c','d']),
(222, array[5,6,7], array['e','f', 'g'])
) as t (id, num, letter)
)
select
*
from test
PrestoDB 似乎支持 unnest()
多个参数:
select t.id, u.n, u.l
from test cross join
unnest(num, letter) as u(n, l)