connect by date data during the completion period

connect by complete period date data according to START_DATE, END_DATE

1. There are ID and PID relationship fields

Use prior:

a) top-down

start with id = 1
connect by level <= (end_date-start_date) and prior id = pid,
check down from root node 1;

b) bottom up

start with id = 1
connect by level <= (end_date - start_date) and id = prior pid
从1向上查;

2. There is no PID relationship field

2.1 Use level directly

connect by level <= (end_date-start_date)
Insert picture description here
There is a level duplication problem, which is caused by the parent node not being found

2.2 level + prior

connect by level <= (end_date - start_date)
and prior ID = ID
Insert picture description here

2.3 level + prior + nocycle

connect by nocycle level <= (end_date-start_date)
and prior ID = ID
can solve the cycle problem and the data duplication problem, but the interval data cannot be displayed because nocycle and id = id

2.4 Give up prior and use level
with t as(
select name
      ,start_date
      ,end_date
      -- 天数从1开始
      1 min_levelv 
      -- 天数
      ,(to_date(max(end_date), 'yyyy-MM-dd') - to_date(min(start_date), 'yyyy-MM-dd')) max_level
  from t_c
)
select to_date(start_date, 'yyyy-MM-dd') + lv - 1 date1
      ,name
      ,start_date
      ,end_date
  from t join (
  select level lv 
    from dual 
 -- level <= t表中最大天数
 connect by level <= (select max(maxlv) from t) ) t2 
  -- on条件限定每条数据可取lv值的范围
      on (t2.lv >= t.min_levelv and t2.lv <= t.max_level)

Guess you like

Origin blog.csdn.net/weixin_38230171/article/details/113118598