[英]Replace brackets and splitting a column into multiple rows based on a delimiter in Postgres
I have a table with column with separated by ';'.我有一个表,列用';'分隔。 The data looks like this:
数据如下所示:
row_id col
1 p.[D389R;D393_W394delinsRD]
2 p.[D390R;D393_W394delinsRD]
3 p.D389R
4. p.[D370R;D393_W394delinsRD]
I would like replace the '[]' brackets whereever they are and fetch the text.我想替换 '[]' 括号并获取文本。 Later, I would like to split the string be ';'
后来,我想将字符串拆分为 ';' and concatenate 'p.'
并连接 'p.' to the splitted text (if it is not there) and create a new row.
到拆分的文本(如果它不存在)并创建一个新行。
The expected output is:预期的输出是:
row_id new_col
1 p.D389R
2 p.D393_W394delinsRD
3 p.D390R
4 p.D393_W394delinsRD
5 p.D389R
6 p.D370R
7 p.D393_W394delinsRD
I have tried below query to get the desired output.我试过下面的查询来获得所需的输出。
SELECT *,
CASE
WHEN regexp_split_to_table(regexp_replace(col, '\[|\]', '', 'g'), E';') NOT LIKE 'p.[%'
THEN 'p.' || (regexp_split_to_table(regexp_replace(col, '\[|\]', '', 'g'), E';'))[1]
ELSE regexp_split_to_table(regexp_replace(col, '\[|\]', '', 'g'), E';')[2]
END AS new_col
FROM table;
Any suggestions would be really helpful.任何建议都会非常有帮助。
I would first remove the constant values ( p.[
and ]
) from the string and then unnest it.我会首先从字符串中删除常量值(
p.[
和]
),然后取消嵌套。
with clean as (
select row_id, regexp_replace(col, '^p\.(\[){0,1}|\]$', '', 'g') as col
from the_table
)
select row_id, 'p.'|| t.c
from clean c
cross join unnest(string_to_array(c.col, ';')) as t(c)
The CTE ( with ...
) isn't really necessary, but that way the unnest(...)
stays readable. CTE(
with ...
)并不是必需的,但这样unnest(...)
保持可读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.