この質問は、私が最近投稿した別の質問と非常に関連していますが、解決が少し複雑になるため、新しい質問を投稿しています。オラクルの忍者やロックスターの助けを求めていますが、それは彼らの専門知識に挑戦し、実践するのに良いことだと思います。
基本的に、TableAとTableBの2つのテーブルがあります。
-- For TableA
CREATE TABLE TableA
(
ID VARCHAR2(10),
LOCN VARCHAR2(10),
START_DATE DATE,
END_DATE DATE
)
STORAGE (
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
NOMONITORING
/
-- Populate TableA
INSERT INTO TableA(ID, LOCN, START_DATE, END_DATE)
VALUES('1P1', '01', TO_DATE('02/04/1996', 'MM/DD/YYYY'), TO_DATE('02/22/1996', 'MM/DD/YYYY');
INSERT INTO TableA(ID, LOCN, START_DATE, END_DATE)
VALUES('1P1', '01', TO_DATE('02/23/1996', 'MM/DD/YYYY'), TO_DATE('05/28/2002', 'MM/DD/YYYY');
INSERT INTO TableA(ID, LOCN, START_DATE, END_DATE)
VALUES('1P1', '01', TO_DATE('05/29/2002', 'MM/DD/YYYY'), TO_DATE('05/03/2005', 'MM/DD/YYYY');
INSERT INTO TableA(ID, LOCN, START_DATE, END_DATE)
VALUES('1P1', '01', TO_DATE('05/04/2005', 'MM/DD/YYYY'), TO_DATE('05/04/2005', 'MM/DD/YYYY');
INSERT INTO TableA(ID, LOCN, START_DATE, END_DATE)
VALUES('1P2', '30', TO_DATE('01/31/1996', 'MM/DD/YYYY'), TO_DATE('02/06/1996', 'MM/DD/YYYY');
INSERT INTO TableA(ID, LOCN, START_DATE, END_DATE)
VALUES('1P2', '02', TO_DATE('02/07/1996', 'MM/DD/YYYY'), TO_DATE('02/13/1996', 'MM/DD/YYYY');
INSERT INTO TableA(ID, LOCN, START_DATE, END_DATE)
VALUES('1P2', '02', TO_DATE('02/14/1996', 'MM/DD/YYYY'), TO_DATE('01/01/2099', 'MM/DD/YYYY');
INSERT INTO TableA(ID, LOCN, START_DATE, END_DATE)
VALUES('1P3', '03', TO_DATE('02/07/1996', 'MM/DD/YYYY'), TO_DATE('02/13/1996', 'MM/DD/YYYY');
INSERT INTO TableA(ID, LOCN, START_DATE, END_DATE)
VALUES('1P3', '03', TO_DATE('02/14/1996', 'MM/DD/YYYY'), TO_DATE('01/01/2099', 'MM/DD/YYYY');
INSERT INTO TableA(ID, LOCN, START_DATE, END_DATE)
VALUES('1S4', '42', TO_DATE('11/06/2001', 'MM/DD/YYYY'), TO_DATE('01/01/2099', 'MM/DD/YYYY');
INSERT INTO TableA(ID, LOCN, START_DATE, END_DATE)
VALUES('3S4', '42', TO_DATE('11/06/2001', 'MM/DD/YYYY'), TO_DATE('01/01/2099', 'MM/DD/YYYY');
-- For TableB
CREATE TABLE TableB
(
ID VARCHAR2(10),
POSTING VARCHAR2(20),
DESCRIPTION VARCHAR2(100),
OTHER_ID VARCHAR2(10),
START_DATE DATE,
END_DATE DATE
)
STORAGE (
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
NOMONITORING
/
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('1P1', 'PROFESSOR', 'Sch 1 Quad 1 Area', 'P1', '02/04/1996', '01/01/2099');
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('1P2', 'PROFESSOR', 'Sch 1 Quad 2 Area', 'P2', '01/31/1996', '01/01/2099');
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('1P3', 'PROFESSOR', 'Sch 1 Quad 3 Area', 'P3', '02/05/1996', '01/01/2099');
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('1S4', 'SUPERVISOR', 'Sch 1 CO Supervisor 4', '1S4', '02/05/1996', '03/18/2002');
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('1S4', 'SUPERINTENDENT', 'Sch 1 CD Superintendent', '1S4', '03/19/2002', '06/09/2009');
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('1S4', 'SUPERVISOR', 'Sch 1 CO Supervisor 4', '1S4', '06/10/2009', '01/01/2099');
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('2S5', 'SUPERVISOR', 'Sch 2 CAO Supervisor 5', '2S5', '10/26/2002', '06/09/2009');
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('2S5', 'SUPERINTENDENT', 'Sch 2 CAO Superintendent 5', '2S5', '06/10/2009', '07/14/2009');
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('2S5', 'SUPERINTENDENT', 'Sch 2 CAO Superintendent 5', 'S5', '07/15/2009', '01/01/2099');
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('3S4', 'SUPERVISOR', 'Sch 3 CO Supervisor 4', '3S4', '02/05/1996', '03/18/2002');
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('3S4', 'SUPERINTENDENT', 'Sch 3 CD Superintendent', '3S4', '03/19/2002', '06/09/2009');
INSERT INTO TableB(ID, POSTING, DESCRIPTION, OTHER_ID, START_DATE, END_DATE)
VALUES('3S4', 'SUPERVISOR', 'Sch 3 CO Supervisor 4', '3S4', '06/10/2009', '01/01/2099');
プロセスは次のようになります。TableAでは、同じID、LOCNを持ち、START_DATEとEND_DATEの日付が連続しているすべてのレコードが結合されます。
ID LOCN START_DATE END_DATE
1P1 01 02/04/1996 05/04/2005
1P2 30 01/31/1996 02/06/1996
1P2 02 02/07/1996 01/01/2099
1P3 03 02/07/1996 01/01/2099
1S4 42 11/06/2001 01/01/2099
3S4 42 11/06/2001 01/01/2099
TableBでは、同じID、POSTING、OTHER_ID、および連続するSTART_DATEとEND_DATEを持つすべてのレコードも結合されます。(とにかく、この表から組み合わせることができるデータはないと思います)。
ID POSTING DESCRIPTION OTHER_ID START_DATE END_DATE
1P1 PROFESSOR Sch 1 Quad 1 Area P1 02/04/1996 01/01/2099
1P2 PROFESSOR Sch 1 Quad 2 Area P2 01/31/1996 01/01/2099
1P3 PROFESSOR Sch 1 Quad 3 Area P3 02/05/1996 01/01/2099
1S4 SUPERVISOR Sch 1 CO Supervisor 4 1S4 02/05/1996 03/18/2002
1S4 SUPERINTENDENT Sch 1 CD Superintendent 1S4 03/19/2002 06/09/2009
1S4 SUPERVISOR Sch 1 CO Supervisor 4 1S4 06/10/2009 01/01/2099
2S5 SUPERVISOR Sch 2 CAO Supervisor 5 2S5 10/26/2002 06/09/2009
2S5 SUPERINTENDENT Sch 2 CAO Superintendent 5 2S5 06/10/2009 07/14/2009
2S5 SUPERINTENDENT Sch 2 CAO Superintendent 5 S5 07/15/2009 01/01/2099
3S4 SUPERVISOR Sch 3 CO Supervisor 4 3S4 02/05/1996 03/18/2002
3S4 SUPERINTENDENT Sch 3 CD Superintendent 3S4 03/19/2002 06/09/2009
3S4 SUPERVISOR Sch 3 CO Supervisor 4 3S4 06/10/2009 01/01/2099
IDに基づいてTableAとTableBのレコードをマージします。LOCN列はテーブルBに追加され、テーブルAの日付範囲に基づいてのみ繰り越されます。結果のデータは次のようになります。
ID UNIT_TYPE DESCRIPTION OTHER_ID START_DATE END_DATE LOCN
1P1 PROFESSOR Sch 1 Quad 1 Area P1 02/04/1996 05/04/2005 01
1P1 PROFESSOR Sch 1 Quad 1 Area P1 05/05/2005 01/01/2099 {NULL}
1P2 PROFESSOR Sch 1 Quad 2 Area P2 01/31/1996 02/06/1996 30
1P2 PROFESSOR Sch 1 Quad 2 Area P2 02/07/1996 01/01/2099 02
1P3 PROFESSOR Sch 1 Quad 3 Area P3 02/05/1996 02/06/1996 {NULL}
1P3 PROFESSOR Sch 1 Quad 3 Area P3 02/07/1996 01/01/2099 03
1S4 SUPERVISOR Sch 1 CO Supervisor 4 1S4 02/05/1996 11/05/2001 {NULL}
1S4 SUPERVISOR Sch 1 CO Supervisor 4 1S4 11/06/2001 03/18/2002 42
1S4 SUPERINTENDENT Sch 1 CD Superintendent 1S4 03/19/2002 06/09/2009 42
1S4 SUPERVISOR Sch 1 CO Supervisor 4 1S4 06/10/2009 01/01/2099 42
2S5 SUPERVISOR Sch 2 CAO Supervisor 5 2S5 10/26/2002 06/09/2009 {NULL}
2S5 SUPERINTENDENT Sch 2 CAO Superintendent 5 2S5 06/10/2009 07/14/2009 {NULL}
2S5 SUPERINTENDENT Sch 2 CAO Superintendent 5 S5 07/15/2009 01/01/2099 {NULL}
3S4 SUPERVISOR Sch 3 CO Supervisor 4 3S4 02/05/1996 11/05/2001 {NULL}
3S4 SUPERVISOR Sch 3 CO Supervisor 4 3S4 11/06/2001 03/18/2002 42
3S4 SUPERINTENDENT Sch 3 CD Superintendent 3S4 03/19/2002 06/09/2009 42
3S4 SUPERVISOR Sch 3 CO Supervisor 4 3S4 06/10/2009 01/01/2099 42
これを解決するために実行可能なアプローチを聞きたいです。たくさんあります。
追加:これは、TableAのレコードを折りたたむためにこれまでに作成したクエリです。
SELECT ID, LOCN, TO_CHAR(MIN(START_DATE), 'MM/DD/YYYY') START_DATE, TO_CHAR(MAX(END_DATE), 'MM/DD/YYYY') END_DATE
FROM
(
SELECT ID, LOCN, START_DATE, END_DATE, MAX(GRP) OVER (ORDER BY ID, START_DATE) GRP
FROM
(
SELECT ID, LOCN,
CASE WHEN START_DATE - LAG(END_DATE) OVER (PARTITION BY ID, LOCN ORDER BY START_DATE ASC) <= 1 THEN
NULL
ELSE
ROWNUM
END GRP,
START_DATE,
NVL(END_DATE, SYSDATE) END_DATE
FROM TableA
ORDER BY ID ASC, START_DATE ASC
)
)
GROUP BY ID, LOCN, GRP
ORDER BY ID ASC, START_DATE ASC;