Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Athena: Convert a comma delimited string into rows

In AWS Athena, I want to write a query like this:

SELECT some_function('row1,row2,row3');

And get back

row1
row2
row3

How do I do this?

I know I can write this instead, but it's less convenient for me:

select * from (values ('row1'), ('row2'), ('row3'))
like image 330
Daniel Kaplan Avatar asked Dec 08 '22 11:12

Daniel Kaplan


1 Answers

You can use the split function to convert the string to an array, and then UNNEST to convert the array to rows. For example:

WITH t AS (
    SELECT 'row1,row2,row3' AS data
)
SELECT value
FROM t
CROSS JOIN UNNEST(split(t.data, ',')) as x(value)
 value 
-------
 row1  
 row2  
 row3  
(3 rows)
like image 103
Dain Sundstrom Avatar answered Dec 27 '22 12:12

Dain Sundstrom