我有以下数据集
color_code fav_color_code color_code_name fav_color_name
1|2 5 blue|white black
3|4 7|9 green|red pink|yellow
我需要将 color_code 的第一个值连接到 color_code_name 的第一个值,将 color_code 的第二个值连接到 color_code_name 的第二个值等。
我的结果应该如下所示:
code color
1 blue
2 white
5 black
3 green
4 red
7 pink
9 yellow
我正在使用下面的代码,但它正在交叉连接,因为我没有 id 可以加入。有人可以帮我得到预期的结果吗?
SELECT
t1.code AS code,
t2.color AS color,
FROM
(
SELECT
c.value :: varchar AS code,
row_number() over(
order by
code
) AS rownum
FROM
table,
lateral flatten (
input => split(color_code, '|')
) c
UNION
SELECT
d.value :: varchar AS code,
row_number() OVER(
ORDER BY
code
) AS rownum
FROM
table,
lateral flatten (
input => split(fav_color_code, '|')
) d
) t1
JOIN (
SELECT
f.value :: varchar AS color,
row_number() OVER(
ORDER BY
color
) AS rownum
FROM
table,
lateral flatten (
input => split(color_code_name, '|')
) f
UNION
SELECT
g.value :: varchar AS color,
row_number() OVER(
ORDER BY
color
) AS rownum
FROM
table,
lateral flatten (
input => split(fav_color_name, '|')
) g
) t2 ON (t1.rownum = t2.rownum)
ORDER BY
t1.color
````````````````````````````````````````````````````````
Thanks in advance