I have a requirement where I need to know when the sum(value)
reaches certain point and calculate duration. Below is the sample table.
create table sample (dt timestamp, value real);
insert into sample values
('2019-01-20 00:29:43 ',0.29)
,('2019-01-20 00:35:06 ',0.31)
,('2019-01-20 00:35:50 ',0.41)
,('2019-01-20 00:36:32 ',0.26)
,('2019-01-20 00:37:20 ',0.33)
,('2019-01-20 00:41:30 ',0.42)
,('2019-01-20 00:42:28 ',0.35)
,('2019-01-20 00:43:14 ',0.52)
,('2019-01-20 00:44:18 ',0.25);
Now my requirement is to calculate the cumulative sum of following rows to see when the sum(value)
reaches above 1.0. That can require just 1 row or n rows. Once that row is reached, I need to calculate time difference between current row and the row where sum(value)
reaches above 1.0.
Essentially my desired output is in below format.
For the 1st row, cumulative sum(value)
is reached at the 3rd row.
For the 2nd row, cumulative sum(value)
is reached at the 4th row etc.
dt | value | sum(value)| time_at_sum(value)_1| Duration
---------------------+--------+------------------------------------------
2019-01-20 00:29:43| 0.29 | 1.01 | 2019-01-20 00:35:50 | 00:06:07
2019-01-20 00:35:06| 0.31 | 1.31 | 2019-01-20 00:37:20 | 00:02:14
2019-01-20 00:35:50| 0.41 | 1.00 | 2019-01-20 00:37:20 | 00:01:30
2019-01-20 00:36:32| 0.26 | 1.01 | 2019-01-20 00:41:30 | 00:04:58
2019-01-20 00:37:20| 0.33 | 1.10 | 2019-01-20 00:42:28 | 00:05:08
2019-01-20 00:41:30| 0.42 | 1.29 | 2019-01-20 00:43:14 | 00:01:44
2019-01-20 00:42:28| 0.35 | 1.12 | 2019-01-20 00:44:18 | 00:01:50
2019-01-20 00:43:14| 0.52 | NULL | - | -
2019-01-20 00:44:18| 0.25 | NULL | - | -
Anyone has ideas or pointers on how to deal with above requirement?