Suppose that I have this table in PostgreSQL 9.4:
CREATE TABLE trajs (
id serial NOT NULL PRIMARY KEY,
obj_id integer,
traj_id integer,
geom geometry,
t timestamp without time zone
);
Here is what the data looks like:
obj_id, traj_id, geom, t
1 1 point '2016-04-29 09:35:00'
1 1 point '2016-04-29 09:40:00'
1 1 point '2016-04-29 09:55:00'
2 1 point '2016-04-29 09:35:00'
2 1 point '2016-04-29 09:42:00'
2 1 point '2016-04-29 09:50:00'
3 1 point '2016-04-29 09:35:00'
3 1 point '2016-04-29 09:41:00'
3 1 point '2016-04-29 09:43:00'
For each 'obj_id', can I iterate 'traj_id' depending on the 't' distance between the previous?
An example: for each 6 minutes of difference (between the first object with a time, like 3rd column and 1st in that table), this should be a new traj_id (+1) - and restart for a new obj_id. Here is what result should look like:
obj_id, traj_id, geom, t
1 1 point '2016-04-29 09:35:00'
1 1 point '2016-04-29 09:40:00'
1 2 point '2016-04-29 09:55:00'
2 1 point '2016-04-29 09:35:00'
2 2 point '2016-04-29 09:42:00'
2 3 point '2016-04-29 09:50:00'
3 1 point '2016-04-29 09:35:00'
3 2 point '2016-04-29 09:41:00'
3 2 point '2016-04-29 09:43:00'
I started doing this, but my solution doesn't work. And I can't see how it can iterate by every time that the condition is met.
update trajs t
set traj_id = traj_id+1
where t.t > (select ant.t + interval '6 minutes'
from trajs ant
where t.obj_id=ant.obj_id);
How to solve this?
CREATE TABLEstatement) and your version of Postgres with a question like this. And the question is not clear to me. Clarify by adding the expected result. Your explanation sounds lineINSERT, but your code usesUPDATE? – Erwin Brandstetter Jun 22 '16 at 02:11UPDATE, I was doing that way because I have to edit the traj_id column that already has values. When I said new traj_id values, I wanted to say that it have to change it, adding 1. Look to the result table. – Adelson Araújo Jun 22 '16 at 13:03