【问题标题】:Split column values based on delimiter and matches with another column in postgres根据分隔符拆分列值并与 postgres 中的另一列匹配
【发布时间】:2020-02-05 10:05:48
【问题描述】:

我在 postgres 模式的表中有以下列。

ID              feature                    start         end
EBI-15947845    p.C29S                     29            29
EBI-15983374    p.E283C                    283           283
EBI-16057637    p.[L44D;A47D;I66D;L67D]    66            66
EBI-16057637    p.[L44D;A47D;I66D;L67D]    47            47
EBI-16057637    p.[L44D;A47D;I66D;L67D]    44            44
EBI-16057637    p.[L44D;A47D;I66D;L67D]    67            67
EBI-2266598     p.D1305_D1306delinsKK      1305          1306

我想转换“特征”列,使条目与开始和结束列的值相匹配。为更清楚起见,以下是转换后我想要的列。

ID              feature     start        end
EBI-15947845    p.C29S      29           29
EBI-15983374    p.E283C     283          283
EBI-16057637    p.I66D      66           66
EBI-16057637    p.A47D      47           47
EBI-16057637    p.L44D      44           44
EBI-16057637    p.L67D      67           67
EBI-2266598     p.D1305_D1306delinsKK      1305          1306

我可以想到一种方法,但无法实施。列值应由 ;然后使用开始/结束值对每个组件应用正则表达式匹配,并在遇到匹配时选择和替换并与 p. 连接。

任何建议都会很有帮助。

谢谢

【问题讨论】:

  • 如果startend 有不同的值怎么办?
  • @GMB 它将在决赛桌中保持原样。为了清楚起见,我在表格末尾添加了一行。

标签: regex postgresql split


【解决方案1】:

我认为您实际上不需要拆分值。据我所知,使用regexp_match() 就足够了:

select id, 
       case 
         when start = "end" and feature like 'p.[%' 
           then 'p.'||(regexp_match(feature, '([A-Z]'||start||'[A-Z])'))[1] 
         else feature
       end as feature,
       start,
       "end"
from the_table

Online example

【讨论】:

  • 太完美了!!你能解释一下添加[1]的目的吗?
  • @rshar: 因为regexp_match() 返回一个数组。
猜你喜欢
  • 2021-03-02
  • 1970-01-01
  • 2013-02-27
  • 2018-10-25
  • 2019-11-12
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多