SELECT st, ed
FROM (
SELECT dt as st,
LEAD(dt) OVER (ORDER BY dt) - CASE LEAD(type) OVER (ORDER BY dt) WHEN 1 THEN 1 ELSE 0 END AS ed,
SUM(CASE WHEN table_type = 1 THEN type ELSE 0 END) OVER (ORDER BY dt) AS num_range1,
SUM(CASE WHEN table_type = 2 THEN type ELSE 0 END) OVER (ORDER BY dt) AS num_range2
FROM (
SELECT dt, type, 1 AS table_type
FROM table1
UNPIVOT (dt FOR type IN (st1 AS 1, ed1 As -1))
UNION ALL
SELECT dt, type, 2
FROM table2
UNPIVOT (dt FOR type IN (st2 AS 1, ed2 As -1))
ORDER BY dt
)
)
WHERE num_range1 > 0
AND num_range2 = 0;
其中,对于示例数据:
CREATE TABLE table1 (st1, ed1) AS
SELECT DATE '2020-06-05', DATE '2020-08-15' FROM DUAL UNION ALL
SELECT DATE '2020-09-01', DATE '2020-09-15' FROM DUAL;
CREATE TABLE table2 (st2, ed2) AS
SELECT DATE '2020-05-01', DATE '2020-06-10' FROM DUAL UNION ALL
SELECT DATE '2020-07-01', DATE '2020-07-03' FROM DUAL UNION ALL
SELECT DATE '2020-08-01', DATE '2020-08-13' FROM DUAL;
1条答案
按热度按时间72qzrwbm1#
UNPIVOT
日期,然后您可以使用分析函数来查找时间序列每个部分的边界,并使用每个表的开始和结束数量之间的差异来计算重叠(和非重叠):其中,对于示例数据:
输出:
| ST|艾德|
| --------------|--------------|
| 2020年6月10日00时00分|2020年6月30日00时00分|
| 2020年7月3日00时00分|2020年7月31日00时00分|
| 2020-08-13 00:00:00|2020-08-15 00:00:00|
| 2020年9月1日00时00分|2020-09-15 00:00:00|
fiddle