连续值之和

sum of consecutive values

我在 Teradata table 中有以下数据。我的要求是连续 sum between OFFLOAD_SECONDS <5 MInutes(300seconds) 并忽略 OFFLOAD_SECONDS 值 OFFLOAD_SECONDS >=5Minutes 并进行滚动求和直到我们得到 offload_seconds >=5 minutes.

我的当前数据。

DATE        number     Time                    OFFLOAD_SECONDS     
2019/03/14     461     2019/03/14 18:24:53     ?     
2019/03/14     461     2019/03/14 18:25:32     39     
2019/03/14     461     2019/03/14 18:32:31     419
2019/03/14     461     2019/03/14 18:32:35     4     
2019/03/14     461     2019/03/14 18:32:52     17     
2019/03/14     461     2019/03/14 18:33:00     8     
2019/03/14     461     2019/03/14 18:33:08     8     
2019/03/14     461     2019/03/14 18:45:53     765 

输出

Date        Number   consecutive sum of OFFLOAD_SECONDS 
3/14/2019   461      39
3/14/2019   461      37

这个查询应该给你预期的输出。让我知道。

http://sqlfiddle.com/#!17/4a4fb/3

    select distinct date
      , sum(case when offload_seconds>=300 then 0 else offload_seconds end) over (partition by col_sum)  as con_sum
    from 
    (
      select date, number, time, offload_seconds
      , sum(case when offload_seconds>=300 then 1 else 0 end) over( rows between unbounded preceding and current row) as col_sum
      from tab1
    ) a
    qualify con_sum<>0

或者,这个带有额外 select 包装上面的 SQL 的较长版本也有效:

http://sqlfiddle.com/#!17/4a4fb/13

 select dt, nm, con_sum 
 from (

    select distinct dt, nm
      , sum(case when offload_seconds>=300 then 0 else offload_seconds end) over (partition by col_sum)  as con_sum
    from 
    (
      select dt, nm, tm, offload_seconds
      , sum(case when offload_seconds>=300 then 1 else 0 end) over( rows between unbounded preceding and current row) as col_sum
      from tab2
    ) a
) t
where t.con_sum <>0;