I have a table1
with timestamps, grouped by an added “Period ID”:
Period ID (uuid) Created At (timestamptz)
A 2017-04-11 11:13:47.997+00
A 2017-04-11 14:42:51.843+00
B 2017-05-21 15:18:12.973+00
B 2017-05-21 15:28:41.054+00
B 2017-05-21 15:28:57.577+00
C 2017-06-11 22:48:00.637+00
D 2017-07-15 14:45:52.343+00
D 2017-07-15 14:47:53.343+00
E 2017-08-25 16:22:57.612+00
F 2017-09-16 00:10:18.577+00
I need to create a new ID “Period ID 2” as a subset of Period ID, such that:
- A UUID is populated in the first row in the new column “Period ID 2”.
- A new Period ID 2 is generated for each different Period ID value (Ex: All timestamps with Period ID “A” can have same “Period ID 2”, timestamps for Period ID “B” can all have the same “Period ID 2”. BUT the “Period ID 2” for A and B cannot be the same )
- For records with the same “Period ID”, if the difference between consecutive timestamps is more than 10 mins, then a new “Period ID 2” should be generated. (Example : for the records with “Period ID” = B, the interval between the first and second timestamp is more than 10 minutes, so a new “Period ID 2” will be generated for the second value. The interval between 2nd and 3rd is less than 10 mins, so the 2nd and 3rd records will have the same “Period ID 2”).
So, the output should be like this:
Period ID (uuid) Created At (timestamptz) Period2 (uuid)
A 2017-04-11 11:13:47.997+00 1
A 2017-04-11 14:42:51.843+00 2
B 2017-05-21 15:18:12.973+00 3
B 2017-05-21 15:28:41.054+00 4
B 2017-05-21 15:28:57.577+00 4
C 2017-06-11 22:48:00.637+00 5
D 2017-07-15 14:45:52.343+00 6
D 2017-07-15 14:47:53.343+00 6
E 2017-08-25 16:22:57.612+00 7
F 2017-09-16 00:10:18.577+00 8
I can add the column, but I am not sure how to populate it. I looked into the following flow: ALTER TABLE
, CREATE FUNCTION
(for trigger function), CREATE TRIGGER
, ... But I wasn’t able to query it properly. Is there any other way to approach this problem?