SELECT event_time, runningDifference(event_time), (runningDifference(event_time) > 900) OR (runningDifference(event_time) <= 0) AS is_new,
device_id
FROM apps_screensORDER BY device_id ASC, event_time ASC┌──────────event_time─┬───diff─┬─is_new─┬─device─┐│ 2016-12-08 10:35:40 │ 0 │ 1 │ id1 ││ 2016-12-08 10:35:53 │ 13 │ 0 │ id1 ││ 2017-01-22 09:02:19 │ 0 │ 1 │ id2 ││ 2017-01-22 09:02:29 │ 10 │ 0 │ id2 ││ 2017-01-22 09:03:11 │ 42 │ 0 │ id2 ││ 2017-01-22 09:03:59 │ 48 │ 0 │ id2 ││ 2017-01-26 16:57:05 │ 0 │ 1 │ id2 ││ 2017-01-26 16:57:32 │ 27 │ 0 │ id2 │
SELECT device_id, event_time, runningAccumulate(s) AS session_idFROM( SELECT device_id, event_time, sumState(is_new) AS s FROM ( SELECT event_time, (runningDifference(event_time) > 900) OR (runningDifference(event_time) <= 0) AS is_new, device_id FROM apps_screens ORDER BY device_id ASC, event_time ASC ) GROUP BY device_id, event_time ORDER BY device_id ASC, event_time ASC)
┌─device_id─┬──────────event_time─┬─session_id─┐│ id1 │ 2016-12-08 10:35:40 │ 1 ││ id1 │ 2016-12-08 10:35:53 │ 1 ││ id2 │ 2017-01-22 09:02:19 │ 2 ││ id2 │ 2017-01-22 09:02:29 │ 2 ││ id2 │ 2017-01-22 09:03:11 │ 2 ││ id2 │ 2017-01-22 09:03:59 │ 2 ││ id2 │ 2017-01-26 16:57:05 │ 3 ││ id2 │ 2017-01-26 16:57:32 │ 3 │└───────────┴─────────────────────┴────────────┘