简体   繁体   中英

how to get slice of an array in AWS Athena?

I have an array of unknown length in AWS Athena. I want to get all elements expect for the first one and concatenate into a string.

I can do with a known length, but I don't see how for unknown length. In this example:

select this_arr, second, array_join(myslice, ' ') as myslice_joined
from
(select this_arr, element_at(this_arr, 2) as second, slice(this_arr, 2, 4) as myslice
from 
(select array ['one','two','three', 'four'] as this_arr));

在此处输入图像描述

What I want is myslice_joined. I could use slice because I knew it had four elements, but what if it's more? Slice does not take a -1 as the last element, as you can do elsewhere.

You can use cardinality to determine the array length:

select this_arr,
    second,
    array_join(myslice, ' ') as myslice_joined
from (
        select this_arr,
            element_at(this_arr, 2) as second,
            slice(this_arr, 2, cardinality(this_arr)) as myslice
        from (
                select array [ 'one', 'two', 'three', 'four' ] as this_arr
            )
    );

Output:

this_arr second myslice_joined
[one, two, three, four] two two three four

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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