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