从开始日期开始,每30天保留一次最长日期

时间:2017-08-29 19:33:36

标签: sql vertica

我试图根据开始日期每30天保留一次最长日期。为简单起见,我在一个ID上测试了它,但实际数据包含许多ID。

我的数据看起来像这样:

ID     date
A      15JAN2016
A      11MAR2016
A      13MAY2016
A      25MAY2016
A      26MAY2016
A      08JUN2016
A      09JUN2016
A      10JUN2016

结束表应如下所示:

ID      Max_srv_dt_1    Max_srv_dt_2   Max_srv_dt_3  Max_srv_dt_4
A       15Jan2016       11Mar2016      13May2016     10JUN2016

我的代码:

 SELECT c1.ID, 
        MIN_SRV_DT_1, 
        Max_SRV_DT_2, 
        Max(C2.date) OVER (PARTITION BY c2.ID ORDER BY C2.date+30) Max_SRV_DT_3
 FROM
    (SELECT c1.ID, MIN_SRV_DT_1, max(C2.date) OVER (PARTITION BY c2.ID ORDER BY C2.date+30) Max_SRV_DT_2
       FROM 
          (SELECT c1.ID, MIN(C1.date) MIN_SRV_DT_1
           FROM max_ep_test C1
           GROUP BY c1.ID) C1
       LEFT OUTER JOIN max_ep_test C2  
         ON c1.ID = c2.ID
        AND C2.date < C1.MIN_SRV_DT_1 + 30
      ) C1
      LEFT OUTER JOIN max_ep_test C2
        ON c1.ID = c2.ID
       AND C2.date > max_srv_dt_2 + 30

我得到类似下面的内容,并且无法弄清楚如何更改参数以实际采用正确的日期:

ID   min_srv_dt_1   max_srv_dt_2   max_srv_dt_3
A    15JAN2016      15JAN2016      11MAR2016
A    15JAN2016      15JAN2016      13MAR2016
A    15JAN2016      15JAN2016      13MAY2016
A    15JAN2016      15JAN2016      25MAY2016
A    15JAN2016      15JAN2016      26MARY016
A    15JAN2016      15JAN2016      08JUN2016
A    15JAN2016      15JAN2016      09JUN2016
A    15JAN2016      15JAN2016      10JUN2016

1 个答案:

答案 0 :(得分:1)

您可以使用datediff()计算任意日期和最早日期之间的差异。其余的只是聚合:

select id, max(date)
from (select et.*,
             datediff('day', min(date) over (partition by id), date) as datediff_day
      from max_ep_test et
     ) et
group by floor(datediff_day / 30);