TABLE1
ID DEPT_Inclusion(AND) DEPT_Exclusion(OR)
E1 3 AND 4 AND 5 AND 6 AND 7 1 OR 2
E2 (1 OR 2) AND 3 4 OR 5
E3 1 AND 2 AND (3 OR 4) AND 5 6 OR 7
E4 1 AND 3 AND 5 2 OR (3 AND 4)
TABLE2
ID DEPT
E1 3
E1 4
E1 5
E1 6
E1 7
E2 1
E2 3
E2 4
E3 1
E3 2
E3 3
E3 4
E3 5
E4 1
E4 3
E4 5
E4 4
Expected Result:
ID
E1
E3
朋友我需要帮助来构建查询以返回满足Dept_Inclusion标准的ID,并且它不应满足Dept_Exclusion标准。 DEPT_Inclusion和Dept_Exclusion值是硬编码的。
For example:
In the Table2
E1 satisfies the condition of Table1 inclusion and exclusion .(E1 is present in 3,4,5,6,7 and it is not present in 1 or 2)
E2 fails because it is present in Dept 4 which is a part of exclusion criteria.
E3 satisfies the condition
E4 fails because it is present in dept 3 AND 4.If it is present in only 3 and not 4 it would have satisfied the criteria.
CODE
create table TABLE1 (id varchar2(5),Dept_Inclusion varchar2(100),Dept_Exclusion varchar2(100));
insert into TABLE1 VALUES('E1','3 AND 4 AND 5 AND 6 AND 7','1 OR 2');
insert into TABLE1 VALUES('E2','(1 OR 2) AND 3','4 OR 5');
insert into TABLE1 VALUES('E3','1 AND 2 AND (3 OR 4) AND 5','6 OR 7');
insert into TABLE1 VALUES('E4','1 AND 3 AND 5','2 OR (3 AND 4)');
create table TABLE2 (id varchar2(5),Dept number);
insert into TABLE2 VALUES('E1',3);
insert into TABLE2 VALUES('E1',4);
insert into TABLE2 VALUES('E1',5);
insert into TABLE2 VALUES('E1',6);
insert into TABLE2 VALUES('E1',7);
insert into TABLE2 VALUES('E2',1);
insert into TABLE2 VALUES('E2',3);
insert into TABLE2 VALUES('E2',4);
insert into TABLE2 VALUES('E3',1);
'insert into TABLE2 VALUES('E3',2);
insert into TABLE2 VALUES('E3',4);
'insert into TABLE2 VALUES('E3',5);
insert into TABLE2 VALUES('E4',1);
'insert into TABLE2 VALUES('E4',3);
insert into TABLE2 VALUES('E4',5);
'insert into TABLE2 VALUES('E4',4);
答案 0 :(得分:0)
创建一个评估函数以评估包含和排除标准:
--This function evaluates the expression and return 1 for match, 0 or no match.
--Assumption: There are no nulls.
create or replace function is_match(p_inclusion varchar2, p_exclusion varchar2)
return number
is
v_sql varchar2(32767);
v_count number;
begin
--Create SQL statement that counts rows returns by expressions.
--Convert pseudo-code into real code.
--Each number gets changed into a query, `select id fro mtable2 where dept=EXPR`.
--Then AND is changed to INTERSECT, OR is changed to UNION ALL.
select
replace(replace(regexp_replace(replace(replace(
'
select count(*)
from
(
--Inclusions
(
%%INCLUSIONS%%
)
minus
--Exceptions
(
%%EXCLUSIONS%%
)
)',
'%%INCLUSIONS%%', p_inclusion),
'%%EXCLUSIONS%%', p_exclusion),
'([0-9]+)', 'select id from table2 where dept=\1'),
'AND', 'intersect'),
'OR', 'union all')
into v_sql
from dual;
--Execute the query.
execute immediate v_sql into v_count;
--If it returns 1 or more row then there is a match.
if v_count >= 1 then
return 1;
else
return 0;
end if;
end;
/
然后在WHERE
子句中使用该函数:
select id
from table1
where is_match(dept_inclusion, dept_exclusion) = 1;
ID
--
E1
E3
我不确定为什么这么多人投票决定关闭这个问题"不清楚"。这个问题很有意义,你提供的样本数据比本网站99%的问题都要好。我有点同意你的数据是高度非规范化的#34;但规范化表达并非易事;保持数据的方式可能很有意义。