oracle 无法获取项目规划SQL问题的所需输出?

bvn4nwqk  于 2023-08-03  发布在  Oracle
关注(0)|答案(1)|浏览(86)

项目规划问题如下:
输入:

projects (task_id, start_date,end_date)
                 1, 1-1-2023,  2-1-2023
                 2, 2-1-2023,  3-1-2023
                 3, 3-1-2023,  4-1-2023
                 4, 7-1-2023,  8-1-2023
                 5, 10-1-2023, 11-1-2023
                 6, 11-1-2023, 12-1-2023

字符串
预期输出:

1-1-2023,  4-1-2023
                 7-1-2023,  8-1-2023
                 10-1-2023, 12-1-2023


基本上,这个想法是有项目完成的日期范围,只要日期是连续的,它们就会落在同一个范围内,否则new_range等。
我尝试了下面的sql,但无法获得所需的结果(缺少一些条件来获得不同的最小start_dates,甚至尝试了group by end_Date)

select
(SELECT 
min(case when p.start_date<p1.start_date  then p.start_date END) from dual),
p1.end_date 
from projects p
cross join 
(Select distinct start_date,end_date from projects  where 
 end_date not in (Select start_date from projects))
 p1
 order by 1,2;

vktxenjb

vktxenjb1#

最后,可以通过以下方式来实现:

select
start_date,end_date from 
(select start_date,rank() over (order by start_date) rn from projects 
where start_date not in (Select end_Date from projects )
) a
join
(select end_date, rank() over (order by end_date) rn  from projects 
where end_date not in (SELECT start_date from projects)
) b on
 a.rn=b.rn
 order by abs(start_date-end_date),start_date
 ;

字符串

相关问题