我有多个可能的数据表(为支持并行处理而创建)以搜索以查找匹配项,另一个表包含要排除的条目。表格定义如下,以及一些示例数据:
CREATE TABLE HOUSEHOLD_1
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_1_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_1 VALUES (1, 1, 'H');
INSERT INTO HOUSEHOLD_1 VALUES (2, 1, 'S');
INSERT INTO HOUSEHOLD_1 VALUES (3, 1, 'D');
CREATE TABLE HOUSEHOLD_2
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_2_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_2 VALUES (4, 4, 'H');
INSERT INTO HOUSEHOLD_2 VALUES (5, 4, 'S');
INSERT INTO HOUSEHOLD_2 VALUES (6, 4, 'D');
CREATE TABLE HOUSEHOLD_3
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_3_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_3 VALUES (7, 7, 'H');
INSERT INTO HOUSEHOLD_3 VALUES (8, 7, 'S');
INSERT INTO HOUSEHOLD_3 VALUES (9, 7, 'D');
CREATE TABLE HOUSEHOLD_DELETIONS
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_DELETIONS_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_DELETIONS VALUES (9, 7, 'D');
CREATE TABLE CLOSED_ACCOUNTS
(
PERSON_ID NUMBER NOT NULL ENABLE,
CONSTRAINT CLOSED_ACCOUNTS_PK PRIMARY KEY (PERSON_ID) ENABLE
);
INSERT INTO CLOSED_ACCOUNTS VALUES (3);
INSERT INTO CLOSED_ACCOUNTS VALUES (6);
INSERT INTO CLOSED_ACCOUNTS VALUES (9);
INSERT INTO CLOSED_ACCOUNTS VALUES (10);
我需要在CLOSED_ACCOUNTS中找到PUSE_ID值,这些值在HOUSEHOLD_1,HOUSEHOLD_2或HOUSEHOLD_3中具有匹配的PERSON_ID,但在HOUSEHOLD_DELETIONS中没有PERSON_ID。根据上面的数据,我应该只找到PERSON_ID值3和6.我已经尝试过我知道的SQL技巧,但我没有成功,所以任何帮助都会受到赞赏。提前谢谢。
答案 0 :(得分:0)
SET操作应该对您有用:
SQL> select person_id from CLOSED_ACCOUNTS
2 intersect (
3 select person_id from HOUSEHOLD_1
4 union
5 select person_id from HOUSEHOLD_2
6 union
7 select person_id from HOUSEHOLD_3
8 )
9 minus
10 select person_id from HOUSEHOLD_DELETIONS
11 /
PERSON_ID
----------
3
6