I have a table like this
CREATE TABLE public.userlocation
(
datetime timestamp with time zone,
location geometry,
locationtype integer,
buffer double precision,
timebuffer double precision,
"userID" numeric,
"ID" integer NOT NULL DEFAULT nextval('"userlocation_ID_seq"'::regclass),
"time" time with time zone
)
each row has a time and then a value to make a dynamic time range, in fact If time=8
and timebuffer=15
so time range would be endTime= 8+15min
and startTime= 8-15 min
.I can do this simply using this query
select f1.*,f1.time +(f1.timebuffer::text||' minute')::INTERVAL as startTime,f1.time-(f1.timebuffer::text||' minute')::INTERVAL as endTime
everything work fine, After this stage I want to query rows that their time period is between startTime and endTime in other word they overlap. I have already found this question
PostgreSQL query to detect overlapping time ranges
But there is a difference here,I don't have start time and endtime so I have to create them using above method. so hare is my query
select f1.*,f1.time -(f1.timebuffer::text||' minute')::INTERVAL as startTime,f1.time+(f1.timebuffer::text||' minute')::INTERVAL as endTime
from userlocation f1
where exists (select f2.time -(f2.timebuffer::text||' minute')::INTERVAL as startTime,f2.time+(f2.timebuffer::text||' minute')::INTERVAL as endTime
from userlocation f2
where tsrange(f2.startTime, f2.endTime, '()') && tsrange(f1.startTime, f1.endTime, '()')
and f2.locationtype = f1.locationtype
and f2.locationtype=1
and f2."ID" <> f1."ID");
But I get this error
[2016-08-27 23:42:45] [42703] ERROR: column f2.starttime does not exist
Position: 372
I think At first I should create F2 table but I dont know how,Can you please give me some hints?