不太确定从哪里开始查询,但我有两个表,TEST_A 和 TEST_B。
TEST_B 包含特定 ID 的特定日期范围,而 TEST_A 包含具有提供的 ASSIGNMENT 值的 ID 的日期范围。
下面是用于创建和填充表的 DDL。
CREATE TABLE TEST_A
(
ID VARCHAR2(5),
START_DATE DATE,
END_DATE DATE,
ASSIGNMENT VARCHAR2(25)
)
STORAGE (
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
NOMONITORING;
CREATE TABLE TEST_B
(
ID VARCHAR2(5),
START_DATE DATE,
END_DATE DATE
)
STORAGE (
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
NOMONITORING;
以及填充表格的脚本:
INSERT INTO TEST_A(ID, START_DATE, END_DATE, ASSIGNMENT)
VALUES('A', TO_DATE('01/01/2010', 'MM/DD/YYYY'), TO_DATE('01/31/2010', 'MM/DD/YYYY'), 'Lot A');
INSERT INTO TEST_A(ID, START_DATE, END_DATE, ASSIGNMENT)
VALUES('A', TO_DATE('02/01/2010', 'MM/DD/YYYY'), TO_DATE('02/15/2010', 'MM/DD/YYYY'), 'Lot A');
INSERT INTO TEST_A(ID, START_DATE, END_DATE, ASSIGNMENT)
VALUES('A', TO_DATE('02/18/2010', 'MM/DD/YYYY'), TO_DATE('02/28/2010', 'MM/DD/YYYY'), 'Lot C');
INSERT INTO TEST_A(ID, START_DATE, END_DATE, ASSIGNMENT)
VALUES('A', TO_DATE('03/01/2010', 'MM/DD/YYYY'), TO_DATE('03/31/2010', 'MM/DD/YYYY'), 'Lot D');
INSERT INTO TEST_A(ID, START_DATE, END_DATE, ASSIGNMENT)
VALUES('B', TO_DATE('08/01/2010', 'MM/DD/YYYY'), TO_DATE('08/31/2010', 'MM/DD/YYYY'), 'Lot E');
INSERT INTO TEST_A(ID, START_DATE, END_DATE, ASSIGNMENT)
VALUES('B', TO_DATE('09/15/2010', 'MM/DD/YYYY'), TO_DATE('09/30/2010', 'MM/DD/YYYY'), 'Lot E');
INSERT INTO TEST_A(ID, START_DATE, END_DATE, ASSIGNMENT)
VALUES('C', TO_DATE('09/15/2010', 'MM/DD/YYYY'), TO_DATE('09/30/2010', 'MM/DD/YYYY'), 'Lot E');
INSERT INTO TEST_B(ID, START_DATE, END_DATE)
VALUES('A', TO_DATE('01/01/2010', 'MM/DD/YYYY'), TO_DATE('12/31/2099', 'MM/DD/YYYY'));
INSERT INTO TEST_B(ID, START_DATE, END_DATE)
VALUES('B', TO_DATE('08/01/2010', 'MM/DD/YYYY'), TO_DATE('12/31/2099', 'MM/DD/YYYY'));
INSERT INTO TEST_B(ID, START_DATE, END_DATE)
VALUES('C', TO_DATE('01/01/2010', 'MM/DD/YYYY'), TO_DATE('12/31/2099', 'MM/DD/YYYY'));
从数据中,我需要按作业对 TEST_A 中的这些记录进行分组,并填补其间缺失的天数。每个 ID 的记录还应涵盖表 TEST_B 中提供的整个开始和结束日期。为了进一步解释,我需要的结果数据如下所示:
ID START_DATE END_DATE ASSIGNMENT
A 01/01/2010 02/15/2010 Lot A
A 02/16/2010 02/17/2010 {NULL}
A 03/01/2010 03/31/2010 Lot D
A 04/01/2010 12/31/2099 {NULL}
B 08/01/2010 08/31/2010 Lot E
B 09/01/2010 09/14/2010 {NULL}
B 09/15/2010 09/30/2010 Lot E
B 10/01/2010 12/31/2099 {NULL}
C 01/01/2010 09/14/2010 {NULL}
C 09/15/2010 09/30/2010 Lot E
C 10/01/2010 12/31/2099 {NULL}
感谢有关为此构建查询的任何帮助。想到了 LAG 函数,但我不确定如何正确注销它。谢谢。
另外:如果存储过程将提供处理此问题的灵活性,那么这仍然是一个可接受的解决方案。