Oracle分层查询性能问题

时间:2016-11-11 20:24:40

标签: oracle

使用具有以下要求的查询苦苦挣扎。是否有可能在不干扰分层格式的情况下重构此查询?

create table TableA
(
Id             VARCHAR2(16) not null,
Suffix         VARCHAR2(3) not null,
Effective_Date DATE not null,
Status         CHAR(1)
);
alter table TableA
add constraint TableA_PK primary key (ID, SUFFIX, EFFECTIVE_DATE)
using index; 

create table TableB
(
Id                  VARCHAR2(16) not null,
Suffix              VARCHAR2(2) not null,
Eligibility_Month   DATE not null
);

create index TableB_IDX1 on TableB (LAST_DAY(ELIGIBILITY_MONTH));
alter table TableB
add constraint TableB_PK primary key (ID, SUFFIX, ELIGIBILITY_MONTH)
using index;

insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('121', '00', to_date('01-01-2015', 'dd-mm-yyyy'), '0');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('121', '00', to_date('01-07-2015', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('121', '00', to_date('01-07-2016', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('121', '00', to_date('01-10-2016', 'dd-mm-yyyy'), '0');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '00', to_date('01-01-2016', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '00', to_date('01-05-2016', 'dd-mm-yyyy'), '0');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '00', to_date('01-07-2016', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '00', to_date('01-08-2016', 'dd-mm-yyyy'), '0');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '00', to_date('01-09-2016', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '00', to_date('01-01-2017', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '01', to_date('01-01-2016', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '01', to_date('01-05-2016', 'dd-mm-yyyy'), '0');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '01', to_date('01-07-2016', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '01', to_date('01-08-2016', 'dd-mm-yyyy'), '0');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '01', to_date('01-09-2016', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('164', '01', to_date('01-01-2017', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('204', '00', to_date('01-01-2014', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('204', '00', to_date('01-01-2016', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('204', '00', to_date('01-01-2017', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('204', '01', to_date('01-01-2014', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('204', '01', to_date('01-01-2017', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('256', '00', to_date('01-01-2014', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('256', '00', to_date('01-01-2015', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('256', '00', to_date('01-01-2016', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('256', '00', to_date('01-08-2016', 'dd-mm-yyyy'), '0');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('256', '01', to_date('01-01-2015', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('256', '01', to_date('01-01-2016', 'dd-mm-yyyy'), '1');
insert into TableA (ID, SUFFIX, EFFECTIVE_DATE, STATUS)
values ('256', '01', to_date('01-08-2016', 'dd-mm-yyyy'), '0');

insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-01-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-02-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-03-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-04-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-05-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-06-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-07-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-08-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-09-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-10-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-11-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-12-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-01-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-02-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-03-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-04-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-05-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-06-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-07-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-08-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-09-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-10-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-11-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('121', '00', to_date('01-12-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-01-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-01-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-02-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-02-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-03-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-03-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-04-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-04-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-05-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-05-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-06-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-06-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-07-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-07-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-08-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-08-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-09-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-09-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-10-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-10-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-11-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-11-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '00', to_date('01-12-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('164', '01', to_date('01-12-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-01-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-01-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-02-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-02-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-03-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-03-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-04-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-04-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-05-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-05-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-06-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-06-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-07-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-07-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-08-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-08-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-09-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-09-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-10-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-10-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-11-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-11-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-12-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-12-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-01-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-01-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-02-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-02-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-03-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-03-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-04-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-04-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-05-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-05-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-06-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-06-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-07-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-07-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-08-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-08-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-09-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-09-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-10-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-10-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-11-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-11-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-12-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-12-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-01-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-01-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-02-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-02-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-03-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-03-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-04-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-04-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-05-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-05-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-06-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-06-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-07-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-07-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-08-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-08-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-09-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-09-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-10-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-10-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-11-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-11-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '01', to_date('01-12-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('204', '00', to_date('01-12-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-01-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-01-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-02-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-02-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-03-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-03-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-04-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-04-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-05-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-05-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-06-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-06-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-07-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-07-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-08-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-08-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-09-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-09-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-10-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-10-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-11-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-11-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-12-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-12-2014', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-01-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-01-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-02-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-02-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-03-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-03-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-04-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-04-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-05-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-05-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-06-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-06-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-07-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-07-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-08-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-08-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-09-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-09-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-10-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-10-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-11-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-11-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-12-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-12-2015', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-01-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-01-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-02-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-02-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-03-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-03-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-04-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-04-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-05-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-05-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-06-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-06-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-07-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-07-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-08-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-08-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-09-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-09-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-10-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-10-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-11-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-11-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '00', to_date('01-12-2016', 'dd-mm-yyyy'));
insert into TableB (ID, SUFFIX, ELIGIBILITY_MONTH)
values ('256', '01', to_date('01-12-2016', 'dd-mm-yyyy'));

---我们现在查询

SELECT ID,
       SUFFIX,
       ELIGIBILITY_MONTH,
       MAX(MEM.START_DATE) AS MAX_EFFECTIVE_DATE
  FROM (SELECT MEM.ID,
               MEM.SUFFIX,
               MEM.ELIGIBILITY_MONTH,
               MIN(CONNECT_BY_ROOT MEM.START_DATE) START_DATE,
               MEM.END_DATE
          FROM (SELECT *
                  FROM (SELECT MH.ID,
                               MH.SUFFIX,
                               MH.STATUS,
                               BRMC1.ELIGIBILITY_MONTH,
                               TRUNC(MH.EFFECTIVE_DATE) AS START_DATE,
                               TRUNC(LEAD(MH.EFFECTIVE_DATE)
                                     OVER(PARTITION BY MH.ID,
                                          MH.SUFFIX,
                                          BRMC1.ELIGIBILITY_MONTH ORDER BY
                                          MH.EFFECTIVE_DATE) - 1) AS END_DATE
                          FROM TableA MH
                          JOIN TableB BRMC1
                            ON MH.ID = BRMC1.ID
                           AND MH.SUFFIX = BRMC1.SUFFIX
                           AND TRUNC(MH.EFFECTIVE_DATE) <=
                               LAST_DAY(BRMC1.ELIGIBILITY_MONTH)
                         WHERE MH.STATUS IN ('0', '1'))
                 WHERE STATUS = '1') MEM
         WHERE CONNECT_BY_ISLEAF = 1
         GROUP BY MEM.ID,MEM.SUFFIX,MEM.ELIGIBILITY_MONTH,
                  MEM.START_DATE,
                  MEM.END_DATE
        CONNECT BY PRIOR MEM.ID = MEM.ID
               AND PRIOR MEM.SUFFIX = MEM.SUFFIX
               AND PRIOR MEM.END_DATE = MEM.START_DATE - 1) MEM
 GROUP BY ID, SUFFIX, ELIGIBILITY_MONTH

最终输出应返回个人ID和后缀的每个资格月的最长生效日期。

请帮我解决这个问题。

0 个答案:

没有答案