5

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'))
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

1 Answers1

18

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)
Dain Sundstrom
  • 2,699
  • 15
  • 14