Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split one row into multiple rows based on comma-separated string column

I have a table like below with columns A(int) and B(string):

A   B
1   a,b,c
2   d,e
3   f,g,h

I want to create an output like below:

A    B
1    a
1    b
1    c
2    d
2    e
3    f
3    g
3    h

If it helps, I am doing this in Amazon Athena (which is based on presto). I know that presto gives a function to split a string into an array. From presto docs:

split(string, delimiter) → array
Splits string on delimiter and returns an array.

Not sure how to proceed from here though.

like image 665
ishan3243 Avatar asked Jun 27 '18 13:06

ishan3243


People also ask

How do I split a comma separated string into multiple rows?

In Data Services, the word_ext() function is capable of taking the n-th word of a string, so word_ext('green,huge,expensive', 2, ',') = 'huge'. So for our use case all we need is to call the function multiple times.

How do you split a single text cell into multiple rows using comma delimited?

In the Split Cells dialog box, select Split to Rows or Split to Columns in the Type section as you need. And in the Specify a separator section, select the Other option, enter the comma symbol into the textbox, and then click the OK button.


1 Answers

Use unnest on the array returned by split.

SELECT a,split_b 
FROM tbl
CROSS JOIN UNNEST(SPLIT(b,',')) AS t (split_b)
like image 100
Vamsi Prabhala Avatar answered Oct 19 '22 05:10

Vamsi Prabhala