I have a table in PostgreSQL 9.2 that looks like this (simplified):
CREATE TABLE my_features
(
id integer NOT NULL,
feature_id integer NOT NULL,
begin_
Here is an observation. If there are overlapping time periods for a feature, then at least one time period overlaps with the preceding one as defined by begin_time
. (You can look at this the other way. If there are no such overlaps then there is always a gap between one time frame and the next and nothing overlaps.)
This leads to the following query for determining overlaps:
select f.feature_id
from (select f.feature_id,
(case when lag(end_time) over (partition by feature_id order by begin_time) > begin_time
then 1 else 0
end) as HasOverlap
from my_features f
) f
group by f.feature_id
having max(HaxOverlap) = 1;