9999-13-31 . .
, 9999-12-31. - ; 9999-12-31, . 8999-12-31; . {:-) , , . ( 9999-12-31, .)
, . , 2016 " " 2017-01-01 ( ), 2017 " " 2017-01-01. , , - . , 2016-08-31 , 2016-09-01, ; ( , 2016-08-31 ).
The OP did not indicate how to interpret end dates here. I assume that they are described in the last paragraph; otherwise, the solution can be easily adapted (but for this you will need to first add from 1 to the end dates, and then subtract 1 at the end - this is exactly one of those cases when 9999-12-31 is not a good placeholder for the "unknown". )
Decision
with m as
(
select group_id, start_date,
max(end_date) over (partition by group_id order by start_date
rows between unbounded preceding and 1 preceding) as m_time
from inputs
union all
select group_id, NULL, max(end_date) from inputs group by group_id
),
n as
(
select group_id, start_date, m_time
from m
where start_date > m_time or start_date is null or m_time is null
),
f as
(
select group_id, start_date,
lead(m_time) over (partition by group_id order by start_date) as end_date
from n
)
select * from f where start_date is not null
;
Output (with data provided):
GROUP_ID START_DATE END_DATE
---------- ---------- ----------
1 2016-01-01 2020-01-01
1 2022-08-31 2030-12-31
2 2010-03-01 2017-01-01
3 2001-01-01 8999-12-31
source
share