简体   繁体   English

使用 unnest 函数在 Presto 中提取数组元素

[英]Extracting Array elements in Presto w/o using unnest function

I have a requirement around this data where I need to extract array elements but I still want to keep them grouped, which means I can not use unnest function.我对这些数据有一个要求,我需要提取数组元素,但我仍然希望将它们分组,这意味着我不能使用 unnest 函数。 Below is the sample data:下面是示例数据:

[ 
  { "emp_id": 8291828, "name": "bruce", }, 
  { "emp_id": 8291823, "name": "Rolli" }
]

My data is in the same format as above,ie (array(row(emp_id varchar, name varchar))) what I need is to get rid of the array, so that data look like我的数据与上面的格式相同,即(array(row(emp_id varchar, name varchar)))我需要的是摆脱数组,使数据看起来像

{ "emp_id": 8291828, "name": "bruce", }, 
{ "emp_id": 8291823, "name": "Rolli" }

Would appreciate if anyone can help me on this.如果有人可以帮助我,我将不胜感激。

You could use element_at If you have a sequence table (1,2,3, ..).如果您有序列表 (1,2,3, ..),则可以使用element_at

with numbers as 
(
    select * from 
    (
    Values
    (1),(2),(3)
    ) as x(i)
)
,emp as
(
    select *
    from (
        values
        (ARRAY[cast(ROW(8291828,'bruce') as row(emp_id bigint, name varchar)), cast(row(8291823,'Rolli') as row(emp_id bigint, name varchar))])
    ) as emp (records)
)
select 
element_at(emp.records,i) record
from numbers n
cross join emp
where n.i <= cardinality(emp.records);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM