我正在尝试从单个列创建2个新列。
我的数据如下:
userid:5438888,locationid:84646646478,property:g
我想为userid
新建一个列,为locationid
新建一个列。行数更多,在整个数据集中,userid
和locationid
的长度并不总是相同。
我假设有一种方法可以在:
之后和,
之前拆分文本,但是我不确定如何在字符串中执行两次此操作。我不在乎字符串的属性部分。仅userid
和locationid
。
答案 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