I'm trying to group data into sessions that are separated by inactivity, or gaps in the timestamp column, in postgres. (For reference, this is referred to as a 'session window' in the kSQL kafka streams world.)
For example, say I have a series of measurements coming in from an IoT device. Each row has a timestamp and a value, but the device does not always send data, only when needed, so there are gaps of inactivity.
timestamp | value
2021-10-29 11:05:00 | 100
2021-10-29 11:04:00 | 101
2021-10-29 09:05:00 | 99
2021-10-29 09:03:00 | 100
Now I want to query this data to group the rows that occur within a 10 minute session window (separated by no rows for a certain amount of time).
The query would look like:
select
max(timestamp) as "end",
min(timestamp) as "start",
avg(value) as "average"
from table
group by
session_window(timestamp, 'interval 10 minutes') -- <- how to do this part?
And the output should look like:
end | start | average
2021-10-29 11:05:00 | 2021-10-29 11:04:00 | 100.5
2021-10-29 09:05:00 | 2021-10-29 09:03:00 | 99.5