I have data about people traveling in different countries like this:
country | begintimestamp | distance
Germany | 2015-01-01 00:00:00 | 100
Germany | 2015-01-01 01:12:13 | 30
France | 2015-01-01 02:13:14 | 40
France | 2015-01-01 03:14:15 | 20
Spain | 2015-01-01 04:15:16 | 10
France | 2015-01-01 05:16:17 | 30
France | 2015-01-01 05:17:18 | 5
Germany | 2015-01-01 06:18:19 | 3
What I need is to be able to receive a result like this - the distance
of consecutive rows summed with the earliest begintimestamp
:
country | begintimestamp | distance
Germany | 2015-01-01 00:00:00 | 130 // 100+30, the distance of two first rows summed.
France | 2015-01-01 02:13:14 | 60 // 40+20
Spain | 2015-01-01 04:15:16 | 10 //
France | 2015-01-01 05:16:17 | 35 // 30+5
Germany | 2015-01-01 06:18:19 | 3
I've tried to play around with PG window functions but have not been able to come up with anything that would lead me closer to the result.