regex - 根据特殊字符的存在和 Postgres 中的字符串匹配将列值拆分为多行
问题描述
我在 Postgres11 中有下表
col1 col2 col3
NCT00065442 APC-Placebo apc-placebo
NCT00135226 Placebo placebo
NCT00146640 MR Prednisone mr prednisone
NCT00146640 Placebo - IR Prednisone placebo - ir prednisone
如果字符串有安慰剂并且有“-”特殊字符,我想拆分 col3。
所需的输出是:
col1 col2 col3
NCT00065442 APC-Placebo apc
NCT00065442 APC-Placebo placebo
NCT00135226 Placebo placebo
NCT00146640 MR Prednisone mr prednisone
NCT00146640 Placebo - IR Prednisone placebo
NCT00146640 Placebo - IR Prednisone ir prednisone
到目前为止,我已经尝试过以下查询。
select *,
case when col3 ilike '%placebo%' and col3 ~* '-'
then unnest(string_to_array(col3, '-'))
else col3
end
from table
order by col1;
我还尝试通过以下方式替换 unnest(string_to_array) 函数:
UNNEST(REGEXP_SPLIT_TO_ARRAY(t.name, '\s*[-]\s*'))
解决方案
表达式unnest()
中不允许使用类似的函数。
你可以这样做:CASE
UNION ALL
select col1, col2, trim(unnest(string_to_array(col3, '-'))) col3
from tablename
where col3 like '%placebo%'
union all
select col1, col2, col3
from tablename
where col3 not like '%placebo%'
见演示。
结果:
| col1 | col2 | col3 |
| ----------- | ----------------------- | ------------- |
| NCT00065442 | APC-Placebo | apc |
| NCT00065442 | APC-Placebo | placebo |
| NCT00135226 | Placebo | placebo |
| NCT00146640 | Placebo - IR Prednisone | placebo |
| NCT00146640 | Placebo - IR Prednisone | ir prednisone |
| NCT00146640 | MR Prednisone | mr prednisone |
推荐阅读
- android - 从 android 中的同一个活动启动一个新的活动实例
- android - 为什么删除 gradle 中的 rootProject.buildDir 任务删除了模块中的构建目录
- json - 将数据从 JSON 文件加载到 html/ 控制台
- java - java中的封闭变量是什么?
- bash - Bash 脚本接受有效数字格式的变量
- angular - 带有防护的Angular 5单元测试路线
- python - 使用 kivy 复选框进行事件处理
- eclipse - 如何在 Eclipse 中完全禁用 SVN?
- angular - 离子onclick滚动到页面底部不起作用
- shopify - 任何人都可以详细说明 Shopify 默认的 _st js 对象吗?