选择字符串值之前和之后的数字-Presto SQL

时间:2019-03-20 00:19:52

标签: sql presto

我正在尝试从单个列创建2个新列。

我的数据如下:

userid:5438888,locationid:84646646478,property:g

我想为userid新建一个列,为locationid新建一个列。行数更多,在整个数据集中,useridlocationid的长度并不总是相同。

我假设有一种方法可以在:之后和,之前拆分文本,但是我不确定如何在字符串中执行两次此操作。我不在乎字符串的属性部分。仅useridlocationid

1 个答案:

答案 0 :(得分:1)

您应该可以使用split_to_map()函数来做到这一点:

WITH data(attribution_site_id) AS (
    VALUES 'userid:5438888,locationid:84646646478,property:g'
),
t AS (
    SELECT split_to_map(attribution_site_id, ',',':') map
    FROM data
)
SELECT element_at(map, 'userid') as userid,
       element_at(map, 'locationid') as locationid
FROM t

产生:

 userid  | locationid
---------+-------------
 5438888 | 84646646478