这个问题与我最近发布的另一个问题非常相关,但我发布了一个新问题,因为这在解决问题时提供了更多的复杂性。我正在寻求一些甲骨文忍者和摇滚明星的帮助,我觉得这是对他们专业知识的一个很好的挑战和锻炼。
基本上我有两个表,TableA 和 TableB。
-- 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;