我想在甘特图中找到最大连续范围

问题描述 投票:0回答:0

插入日期(custid,bdate,ndate) 价值观 (5,'2013-12-01','2013-12-04'), (5,'2013-11-05','2013-12-01'), (5,'2013-11-15','2013-12-24'), (5,'2010-12-05','2012-12-04'), (5,'2009-12-05','2011-12-04') ;

我想获得 '2013-11-05','2013-12-24' 因为 3 个范围交错 和“2009-12-05”、“2012-12-04”,因为这些范围也相互缠绕 我需要时间 btw 的最小值和最大值 我试过滞后+领先但到目前为止没有运气

到目前为止,这个尝试创建一个标志,然后将其范围设置为最大最小范围。

到目前为止没有运气

选择custid,bdate,ndate, lead(bdate) OVER(PARTITION BY custid ORDER BY ndate desc, bdate desc) 作为 lead_bdate, lead(ndate) OVER(PARTITION BY custid ORDER BY ndate desc, bdate desc) as lead_ndate, lag(bdate) OVER(PARTITION BY custid ORDER BY ndate desc, bdate desc) lag_bdate, lag(ndate) OVER(PARTITION BY custid ORDER BY ndate desc, bdate desc) 作为 lag_ddate

            ,
            case 
            when        lead(bdate) OVER(PARTITION BY custid
   ORDER BY ndate desc, 
            bdate desc) >=bdate then 1 
            when ndate = max(ndate) OVER(PARTITION BY custid
   ORDER BY ndate desc, 
            bdate desc) then 1          

            when bdate between 
            lead(bdate) OVER(PARTITION BY custid ORDER BY ndate desc, bdate desc) 
            and lead(ndate) OVER(PARTITION BY custid ORDER BY ndate desc, bdate desc) then 1

            when ndate between 
            lag(bdate) OVER(PARTITION BY custid ORDER BY ndate desc, bdate desc) 
            and lag(ndate) OVER(PARTITION BY custid ORDER BY ndate desc, bdate desc) then 1

            else 0 end as net_seniority_Flag
             from dateso
range lag sequential dateadd lead
© www.soinside.com 2019 - 2024. All rights reserved.