如何分解基本上是数组数组的字符串并将其存储在红移中的单独行中?
How to explode an string which is basically an array of arrays and store it in separate rows in redshift?
我有一个 table 像 :
id
col2
col_3
col_4
array_string
1
..
..
..
[[h1,r1],[h2,r2],[h3,r3]]
我想要 o/p 作为
id
col2
col_3
col_4
col_h
col_r
1
..
..
..
h1
r1
1
..
..
..
h2
r2
1
..
..
..
h3
r3
列 array_string
存储为字符串,而不是超级或 json 类型数据类型,它可以包含任意数量的元素(例如,这里我使用的数组只有三元)
我尝试了split_part
、string_to_array
但仍然无法解决问题。
任何帮助将不胜感激。
这是一个 Postgres 解决方案,可能无法在 Redhsift 上运行或可能需要修改。
这是如何扩展字符串的说明:
with t(a) as
(
select jsonb_array_elements(regexp_replace(
'[[h1,r1],[h2,r2],[h3,r3]]', '\y', '"', 'g')::jsonb)
)
select a->>0 col_h, a->>1 col_r from t;
col_h
col_r
h1
r1
h2
r2
h3
r3
整个查询将是
select t.id, t.col2, t.col_3, t.col_4, l.*
from the_table t
cross join lateral
(
with t2(a) as
(
select jsonb_array_elements(regexp_replace(t.array_string,'\y','"','g')::jsonb)
)
select a->>0 col_h, a->>1 col_r from t2
) l;
您需要使用 json_parse() - https://docs.aws.amazon.com/redshift/latest/dg/JSON_PARSE.html
将字符串转换为“超级”数据类型
然后使用此过程现在支持的 PartiQL 语法取消嵌套数组 - https://docs.aws.amazon.com/redshift/latest/dg/query-super.html
您的案例看起来与某些示例几乎相同,因此我不会在这里重复它们。
我有一个 table 像 :
id | col2 | col_3 | col_4 | array_string |
---|---|---|---|---|
1 | .. | .. | .. | [[h1,r1],[h2,r2],[h3,r3]] |
我想要 o/p 作为
id | col2 | col_3 | col_4 | col_h | col_r |
---|---|---|---|---|---|
1 | .. | .. | .. | h1 | r1 |
1 | .. | .. | .. | h2 | r2 |
1 | .. | .. | .. | h3 | r3 |
列 array_string
存储为字符串,而不是超级或 json 类型数据类型,它可以包含任意数量的元素(例如,这里我使用的数组只有三元)
我尝试了split_part
、string_to_array
但仍然无法解决问题。
任何帮助将不胜感激。
这是一个 Postgres 解决方案,可能无法在 Redhsift 上运行或可能需要修改。
这是如何扩展字符串的说明:
with t(a) as
(
select jsonb_array_elements(regexp_replace(
'[[h1,r1],[h2,r2],[h3,r3]]', '\y', '"', 'g')::jsonb)
)
select a->>0 col_h, a->>1 col_r from t;
col_h | col_r |
---|---|
h1 | r1 |
h2 | r2 |
h3 | r3 |
整个查询将是
select t.id, t.col2, t.col_3, t.col_4, l.*
from the_table t
cross join lateral
(
with t2(a) as
(
select jsonb_array_elements(regexp_replace(t.array_string,'\y','"','g')::jsonb)
)
select a->>0 col_h, a->>1 col_r from t2
) l;
您需要使用 json_parse() - https://docs.aws.amazon.com/redshift/latest/dg/JSON_PARSE.html
将字符串转换为“超级”数据类型然后使用此过程现在支持的 PartiQL 语法取消嵌套数组 - https://docs.aws.amazon.com/redshift/latest/dg/query-super.html
您的案例看起来与某些示例几乎相同,因此我不会在这里重复它们。