darkk SELECT node_id time AS to FROM AS now WHERE NOT EXISTS SELECT FR

 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
darkk=> SELECT node_id, 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 min(time) FROM t AS foo WHERE foo.time > now.time ));
node_id | to
---------+----
1 | 2
2 | 4
1 | 6
3 | 7
1 | 10
(5 rows)
darkk=> SELECT node_id, time AS "from" 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 ));
node_id | from
---------+------
1 | 1
2 | 3
1 | 5
3 | 7
1 | 8
(5 rows)
darkk=> select * from t;
time | node_id
------+---------
1 | 1
2 | 1
3 | 2
4 | 2
5 | 1
6 | 1
7 | 3
8 | 1
9 | 1
10 | 1