根据逗号分隔的字符串列将一行拆分为多行 [英] Split one row into multiple rows based on comma-separated string column

查看:979
本文介绍了根据逗号分隔的字符串列将一行拆分为多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下表,其中列 A(int) B(string)

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

我想创建如下输出:

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

如果有帮助,我正在亚马逊上进行雅典娜(基于presto)。我知道presto提供了将字符串拆分为数组的功能。来自presto 文档

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)→数组

在分隔符
上分割字符串并返回一个数组。

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

虽然不确定如何从此处继续。

Not sure how to proceed from here though.

推荐答案

使用嵌套 split 返回的数组。

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

这篇关于根据逗号分隔的字符串列将一行拆分为多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆