[英]Oracle SQL: Return only values which are contained in a field from another table
I am trying to write an Oracle SQL query for to return a set of values which are currently configured to do a job. 我正在尝试编写一个Oracle SQL查询,以返回一组当前配置为执行工作的值。
I have two tables: 我有两个表:
VALUES
A
B
C
D
JOB, STRINGS_TO_SEARCH
1 {r, e, 'Hello' A w: B, xyz}
2 {ok, D }
The configuration of each job is stored in a single string. 每个作业的配置都存储在单个字符串中。 How would I write a query which returns only the values which have been configured for a job?
我将如何编写查询,该查询仅返回已为作业配置的值?
Is this even possible using built-in Oracle functions? 使用内置的Oracle函数甚至可能吗? It seems like I may be asking for a dynamic query to be generated...
似乎我可能正在要求生成动态查询...
create table T_VALUES (
VAL varchar2(20)
);
insert into T_VALUES values ('A');
insert into T_VALUES values ('B');
insert into T_VALUES values ('C');
insert into T_VALUES values ('D');
create table T_JOBS (
JOB_ID int,
STR_LIST varchar2(50)
);
insert into T_JOBS values (1, 'r, e, ''Hello'' A w: B, xyz');
insert into T_JOBS values (2, 'ok, D ');
select
job_id,
val
from
T_VALUES
natural join (
select
job_id,
regexp_substr(str_list, '[^ ,]+', 1, occ) as val
from
T_JOBS
cross join (
select level as occ from dual
connect by level <= (select max(length(str_list)) from T_JOBS)
)
)
order by 1, 2
EDIT : 编辑:
Improved version (using Ben's idea) 改进版本(使用Ben的想法)
create table T_VALUES (
VAL varchar2(20)
);
insert into T_VALUES values ('A');
insert into T_VALUES values ('B');
insert into T_VALUES values ('C');
insert into T_VALUES values ('D');
create table T_JOBS (
JOB_ID int,
STR_LIST varchar2(50)
);
insert into T_JOBS values (1, 'junk, values=A:NotA:B:r:e, more_junk');
insert into T_JOBS values (2, 'ok, values=D ');
create type nt_str as table of varchar2(50);
select
j.job_id,
v.val
from
T_JOBS j,
table(
cast(
multiset(
select
regexp_substr(
regexp_substr(j.str_list, 'values=([^, ]+)', 1, 1, 'i', 1),
'[^:]+', 1, level)
from dual
connect by
regexp_substr(
regexp_substr(j.str_list, 'values=([^, ]+)', 1, 1, 'i', 1),
'[^:]+', 1, level) is not null
) as nt_str
)
) t
join T_VALUES v
on v.val = t.column_value
order by 1, 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.