ORACLE - 如何检查起始日期和截止日期之间的两个日期?
ORACLE - How to check two dates between from-date and to-date?
我有一个 table,其中包含有关销售周期的信息,现在我需要知道某些行是否相互重叠。例如:如果有“01/12/2016”到“31/01/2017”的时间段,我想知道某些行是否重叠,例如“15/12/2016”到“28/02/2017”。你有什么想法吗?
NUMBER(5) DATE DATE
Period No. from date to-date
-----------------------------------------
9891 01/06/2016 31/07/2016
9892 01/08/2016 30/09/2016
9893 01/09/2016 31/10/2016 -- This row is overlapped
9894 01/11/2016 31/12/2016
9895 15/12/2016 28/02/2017 -- This row is overlapped
9896 01/03/2017 31/05/2017
我知道检查两个日期之间的一个日期的逻辑(WHERE v_date_1 BETWEEN
v_date_2 AND v_date_3) 但我不知道如何检查两个日期之间的两个日期!。谢谢
不确定您希望输出哪种格式。这是执行此操作的一种方法。
alter session set nls_date_format = 'dd/mm/yyyy';
with
test_data ( period_no, from_dt, to_dt ) as (
select 9891, to_date('01/06/2016'), to_date('31/07/2016') from dual union all
select 9892, to_date('01/08/2016'), to_date('30/09/2016') from dual union all
select 9893, to_date('01/09/2016'), to_date('31/10/2016') from dual union all
select 9894, to_date('01/11/2016'), to_date('31/12/2016') from dual union all
select 9895, to_date('15/12/2016'), to_date('28/02/2017') from dual union all
select 9896, to_date('01/03/2017'), to_date('31/05/2017') from dual
)
-- End of simulated table (for testing purposes only, not part of the solution).
-- SQL query begins BELOW THIS LINE.
select a.period_no as period_a, a.from_dt as from_dt_a, a.to_dt as to_dt_a,
b.period_no as period_b, b.from_dt as from_dt_b, b.to_dt as to_dt_b
from test_data a
join
test_data b
on a.period_no < b.period_no
and a.to_dt >= b.from_dt
and b.to_dt >= a.from_dt
;
PERIOD_A FROM_DT_A TO_DT_A PERIOD_B FROM_DT_B TO_DT_B
---------- ---------- ---------- ---------- ---------- ----------
9892 01/08/2016 30/09/2016 9893 01/09/2016 31/10/2016
9894 01/11/2016 31/12/2016 9895 15/12/2016 28/02/2017
我们将 table 连接到自身,因为我们想比较不同的行(在相同的 table 中而不是在不同的 table 中,但概念是相同的 - 因为这样比较,你加入了tables,即使是两个相同的副本table)。这就是所谓的 "self join".
然后:有两种时间间隔不能重叠的方式:第一个在第二个开始之前结束,或者第二个在第一个开始之前结束。现在 NEGATE 这个条件(记住 "or" 的否定是 "and")并且你在 JOIN 子句中得到两个额外的条件。
我有一个 table,其中包含有关销售周期的信息,现在我需要知道某些行是否相互重叠。例如:如果有“01/12/2016”到“31/01/2017”的时间段,我想知道某些行是否重叠,例如“15/12/2016”到“28/02/2017”。你有什么想法吗?
NUMBER(5) DATE DATE
Period No. from date to-date
-----------------------------------------
9891 01/06/2016 31/07/2016
9892 01/08/2016 30/09/2016
9893 01/09/2016 31/10/2016 -- This row is overlapped
9894 01/11/2016 31/12/2016
9895 15/12/2016 28/02/2017 -- This row is overlapped
9896 01/03/2017 31/05/2017
我知道检查两个日期之间的一个日期的逻辑(WHERE v_date_1 BETWEEN v_date_2 AND v_date_3) 但我不知道如何检查两个日期之间的两个日期!。谢谢
不确定您希望输出哪种格式。这是执行此操作的一种方法。
alter session set nls_date_format = 'dd/mm/yyyy';
with
test_data ( period_no, from_dt, to_dt ) as (
select 9891, to_date('01/06/2016'), to_date('31/07/2016') from dual union all
select 9892, to_date('01/08/2016'), to_date('30/09/2016') from dual union all
select 9893, to_date('01/09/2016'), to_date('31/10/2016') from dual union all
select 9894, to_date('01/11/2016'), to_date('31/12/2016') from dual union all
select 9895, to_date('15/12/2016'), to_date('28/02/2017') from dual union all
select 9896, to_date('01/03/2017'), to_date('31/05/2017') from dual
)
-- End of simulated table (for testing purposes only, not part of the solution).
-- SQL query begins BELOW THIS LINE.
select a.period_no as period_a, a.from_dt as from_dt_a, a.to_dt as to_dt_a,
b.period_no as period_b, b.from_dt as from_dt_b, b.to_dt as to_dt_b
from test_data a
join
test_data b
on a.period_no < b.period_no
and a.to_dt >= b.from_dt
and b.to_dt >= a.from_dt
;
PERIOD_A FROM_DT_A TO_DT_A PERIOD_B FROM_DT_B TO_DT_B
---------- ---------- ---------- ---------- ---------- ----------
9892 01/08/2016 30/09/2016 9893 01/09/2016 31/10/2016
9894 01/11/2016 31/12/2016 9895 15/12/2016 28/02/2017
我们将 table 连接到自身,因为我们想比较不同的行(在相同的 table 中而不是在不同的 table 中,但概念是相同的 - 因为这样比较,你加入了tables,即使是两个相同的副本table)。这就是所谓的 "self join".
然后:有两种时间间隔不能重叠的方式:第一个在第二个开始之前结束,或者第二个在第一个开始之前结束。现在 NEGATE 这个条件(记住 "or" 的否定是 "and")并且你在 JOIN 子句中得到两个额外的条件。