darkk select from order by time time node_id ------ --------- 10 10 ro

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
darkk=> select * from t order by time;
time | node_id
------+---------
1 | 1
2 | 1
3 | 2
4 | 2
5 | 1
6 | 1
7 | 3
8 | 1
9 | 1
10 | 1
(10 rows)
darkk=>
SELECT
node_id,
time AS "from",
(SELECT
max(time)
FROM t as future
WHERE
future.time >= now.time
AND NOT EXISTS (
SELECT 1 FROM t AS foo
WHERE foo.node_id != now.node_id AND foo.time BETWEEN now.time AND future.time)
) AS "to"
FROM t AS now
WHERE NOT EXISTS (
SELECT 1 FROM t AS past
WHERE past.node_id = now.node_id AND past.time = (SELECT max(time) FROM t AS foo WHERE foo.time < now.time ))
ORDER BY "from";
node_id | from | to
---------+------+----
1 | 1 | 2
2 | 3 | 4
1 | 5 | 6
3 | 7 | 7
1 | 8 | 10
(5 rows)