HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Datetime progressive sum PostgreSQL

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlsumdatetimeprogressive

Problem

my first question here, but I looked in many other places and could not find the answer to my problem.

id | track_seg_id |        time         
----+--------------+---------------------
  1 |            0 | 2020-05-23 07:45:32
  2 |            0 | 2020-05-23 07:46:04
  3 |            0 | 2020-05-23 07:46:53
  4 |            0 | 2020-05-23 07:46:58
  5 |            0 | 2020-05-23 07:47:03
  6 |            0 | 2020-05-23 07:47:08
  7 |            0 | 2020-05-23 07:47:11
  8 |            0 | 2020-05-23 07:47:16
  9 |            0 | 2020-05-23 07:47:20
 10 |            0 | 2020-05-23 07:47:23


What I want to do is to get a progressive sum of seconds from the first to the next row.

id | track_seg_id |        time         |        prog         
----+--------------+---------------------+---------------------
  1 |            0 | 2020-05-23 07:45:32 | 0
  2 |            0 | 2020-05-23 07:46:04 | 32
  3 |            0 | 2020-05-23 07:46:53 | 81
  4 |            0 | 2020-05-23 07:46:58 | 86
  5 |            0 | 2020-05-23 07:47:03 | 91
  6 |            0 | 2020-05-23 07:47:08 | 96
  7 |            0 | 2020-05-23 07:47:11 | 99
  8 |            0 | 2020-05-23 07:47:16 | 104
  9 |            0 | 2020-05-23 07:47:20 | 108
 10 |            0 | 2020-05-23 07:47:23 | 111


I hit different resources, calculate cumulative sum running total and calculating cumulative sum , but I wasn't able to solve this problem.

Solution

I think you mean “seconds”, not “minutes”, and your values are slightly off.

If my suspicion is right, you could do it with the window function first_value and use extract to extract the number of seconds:

SELECT *,
       EXTRACT(epoch FROM
               time - first_value(time) OVER (ORDER BY time)
              )
FROM mytable;

 id | track_seg_id |        time         | date_part 
----+--------------+---------------------+-----------
  1 |            0 | 2020-05-23 07:45:32 |         0
  2 |            0 | 2020-05-23 07:46:04 |        32
  3 |            0 | 2020-05-23 07:46:53 |        81
  4 |            0 | 2020-05-23 07:46:58 |        86
  5 |            0 | 2020-05-23 07:47:03 |        91
  6 |            0 | 2020-05-23 07:47:08 |        96
  7 |            0 | 2020-05-23 07:47:11 |        99
  8 |            0 | 2020-05-23 07:47:16 |       104
  9 |            0 | 2020-05-23 07:47:20 |       108
 10 |            0 | 2020-05-23 07:47:23 |       111
(10 rows)

Code Snippets

SELECT *,
       EXTRACT(epoch FROM
               time - first_value(time) OVER (ORDER BY time)
              )
FROM mytable;

 id | track_seg_id |        time         | date_part 
----+--------------+---------------------+-----------
  1 |            0 | 2020-05-23 07:45:32 |         0
  2 |            0 | 2020-05-23 07:46:04 |        32
  3 |            0 | 2020-05-23 07:46:53 |        81
  4 |            0 | 2020-05-23 07:46:58 |        86
  5 |            0 | 2020-05-23 07:47:03 |        91
  6 |            0 | 2020-05-23 07:47:08 |        96
  7 |            0 | 2020-05-23 07:47:11 |        99
  8 |            0 | 2020-05-23 07:47:16 |       104
  9 |            0 | 2020-05-23 07:47:20 |       108
 10 |            0 | 2020-05-23 07:47:23 |       111
(10 rows)

Context

StackExchange Database Administrators Q#269331, answer score: 6

Revisions (0)

No revisions yet.