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

混江龙づ霸主 提交于 2019-12-10 23:55:14

问题


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.


回答1:


Use unnest on the array returned by split.

SELECT a,split_b 
FROM tbl
CROSS JOIN UNNEST(SPLIT(b,',')) AS t (split_b)


来源:https://stackoverflow.com/questions/51063730/split-one-row-into-multiple-rows-based-on-comma-separated-string-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!