sql - 2个逗号分隔字符串的Oracle SQL交集
问题描述
我可以应用什么功能?
询问:
Select x, f(y) from table where y like '%ab%cd%ef';
示例表(y按字母顺序排序)
x. y
1 ab
2 ab,cd
3 cd,ef
4 ab,ef,gh,yu
5 de,ef,rt
预期输出:
输出:
x y
1 ab
2 ab,cd
3 cd,ef
4 ab,ef
5 ef
解决方案
使用regexp_substr
带有connect by level
表达式的函数作为
with tab(x,y) as
(
select 1,'ab' from dual union all
select 2,'ab,cd' from dual union all
select 3,'cd,ef' from dual union all
select 4,'ab,ef,gh,yu' from dual union all
select 5,'de,ef,rt' from dual
), tab2 as
(
Select x, regexp_substr(y,'[^,]+',1,level) as y
from tab
connect by level <= regexp_count(y,',') + 1
and prior x = x
and prior sys_guid() is not null
), tab3 as
(
select x, y
from tab2
where y like '%ab%'
or y like '%cd%'
or y like '%ef%'
)
select x, listagg(y,',') within group (order by y) as y
from tab3
group by x;
X Y
1 ab
2 ab,cd
3 cd,ef
4 ab,ef
5 ef
推荐阅读
- python - 我应该如何在 Python 中编写一个函数来检查用户是否获取了一些 url?
- regex - 如何使用部分查询字符串从弹性搜索中获取文档?
- c# - Unity c#错误四元数在控制台中无法识别
- angular - 具有服务参数的组件的角度单元测试
- r - 大文件处理 - 使用 chunked::read_csv_chunked 和 dplyr::filter 时出错
- android - 当我在自定义视图类中使用 R.styleable 时,我得到一个红色的 Unresolved reference: styleable
- amazon-web-services - Fargate 部署在上线之前多次重启
- json - JSON 加载了缺少的键/值对
- javascript - Github 页面上的 Javascript 和 HTML?
- java - springboot webflux couchbase API未显示结果