2015-02-06 69 views
0
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 

朋友我需要幫助建立一個查詢返回的ID滿足Dept_Inclusion標準,它不應該滿足Dept_Exclusion標準。 DEPT_Inclusion和Dept_Exclusion值是硬編碼的。SQL查詢來讀取硬編碼值在Oracle 11gR2中

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

您的數據非常規格化。您幾乎將僞代碼作爲列值本身。非常差的設計。現在你陷入了抽取部分字符串以處理部門的不必要的麻煩。首先對數據進行標準化。 – 2015-02-07 05:20:31

回答

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%的問題更好的樣本數據。我有點同意你的數據「非常規格化」。但是,對錶達進行規範化並不是一件微不足道的事情;保持數據的方式可能很有意義。

+0

如果我將以下行'代碼'插入到TABLE1 VALUES('E5','4 AND 5 AND 6 AND 7','1 or 2');插入TABLE2 VALUES('E5',4); 插入TABLE2 VALUES('E5',5); 插入TABLE2 VALUES('E5',6); 插入TABLE2值('E5',7);你所提供的功能是回傳E5也輸出。但它不應該顯示E5,因爲它不出現在部:3.我無法弄清楚它爲什麼返回E5.Please help.Thanks提前 – 2015-02-12 15:20:01

+0

那些插入不有與部門有關的任何事情:3? – 2015-02-12 19:31:57

+0

沒有@Jon Heller.That是如何存在的數據。 – 2015-02-12 20:18:05