[英]Unnest an array in AWS Redshift
該列的數據類型是什么?
Redshift 不支持 arrays,所以讓我假設這是一個 JSON 字符串。
Redshift 不提供 JSON 設置返回功能:我們需要手動取消嵌套。 這是一種方法,如果您有一個包含足夠行數的表(至少與數組中的元素一樣多) - 比如說sometable
:
select json_extract_array_element_text(t.id, n.rn) as new_id
from mytable t
inner join (select row_number() over() - 1 as rn from sometable) n
on n.rn < json_array_length(t.id)
更新(2022 年):Redshift 現在支持 arrays 並允許輕松“取消嵌套”它們。
語法就是有一個FROM the_table AS the_table_alias, the_table_alias.the_array AS the_element_alias
這是問題中提到的數據的示例:
WITH
-- some table with test data
input_data as (
SELECT array(1,2,3,10) as id
union all
SELECT array(1) as id
union all
SELECT array(2,3,4,9) as id
)
SELECT
id2
FROM
input_data AS ids,
ids.id AS id2
產生預期:
id2
---
1
2
3
4
9
1
2
3
10
有關更多詳細信息和嵌套級別更深的示例,請參見此處:https://docs.aws.amazon.com/redshift/latest/dg/query-super.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.