Sql 查询以读取 oracle 11gR2 中的硬编码值

Sql query to read the hardcoded values in oracle 11gR2

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

朋友们,我需要帮助构建一个查询,查询 return 满足 Dept_Inclusion 标准但不应该满足 Dept_Exclusion 标准的 ID。 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);

创建一个函数来评估纳入和排除标准:

--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='),
        '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

我不确定为什么有这么多人投票关闭这个问题 "not clear"。这个问题很有意义,而且您提供的样本数据比本网站上 99% 的问题都要好。我有点同意你的数据是 "highly denormalized"。但是规范化表达式并不是一件小事;保持数据原样可能很有意义。