Having this table at hand:
SELECT * FROM mutable LIMIT 10;
user_id | session_id | timestamp
---------+--------------------+------------------------
180 | 179020080820120904 | 2008-08-20 12:09:04+01
180 | 179020080820120904 | 2008-08-20 12:09:07+01
180 | 179020080820120904 | 2008-08-20 12:10:35+01
180 | 179020080820120904 | 2008-08-20 12:10:37+01
180 | 179020080820120904 | 2008-08-20 12:10:39+01
180 | 179020080820120904 | 2008-08-20 12:10:41+01
180 | 179020080820120904 | 2008-08-20 12:10:43+01
180 | 179020080820120904 | 2008-08-20 12:10:45+01
180 | 179020080820120904 | 2008-08-20 12:10:47+01
180 | 179020080820120904 | 2008-08-20 12:10:49+01
(10 rows)
So I want to modify the table by adding a column t_evolution that shows keeping track of the duration from one record occurrence to the next, considering the timestamp columns, like so:
+---------------------------------------------------------+--------------------+
| user_id | session_id | timestamp | t_evolution |
+---------------------------------------------------------+--------------------+
| 180 | 179020080820120904 | 2008-08-20 12:09:04+01 | 0 |
| 180 | 179020080820120904 | 2008-08-20 12:09:07+01 | 3 |
| 180 | 179020080820120904 | 2008-08-20 12:10:35+01 | 92 |
| 180 | 179020080820120904 | 2008-08-20 12:10:37+01 | 94 |
| 180 | 179020080820120904 | 2008-08-20 12:10:39+01 | 96 |
| 180 | 179020080820120904 | 2008-08-20 12:10:41+01 | 98 |
| 180 | 179020080820120904 | 2008-08-20 12:10:43+01 | 100 |
| 180 | 179020080820120904 | 2008-08-20 12:10:45+01 | 102 |
| 180 | 179020080820120904 | 2008-08-20 12:10:47+01 | 104 |
| 180 | 179020080820120904 | 2008-08-20 12:10:49+01 | 106 |
+---------------------------------------------------------+--------------------+
(10 rows)