Query for condition over time Say we have a table setup like this:

+---------+--------------+---------------+
| id text | time integer | value decimal |
+---------+--------------+---------------+
|       1 |            1 |            10 |
|       1 |            2 |            10 |
|       1 |            3 |             7 |
|       1 |            4 |             8 |
|       1 |            5 |             6 |
|       1 |            6 |             5 |
|       1 |            7 |             4 |
|       1 |            8 |             3 |
|       1 |            9 |             7 |
|       1 |           10 |            11 |
|       1 |           11 |             3 |
|       1 |           12 |             8 |
|       1 |           13 |             2 |
|       1 |           14 |             4 |
|       1 |           15 |             1 |
+---------+--------------+---------------+


Would it be possible to select the starting and ending times for when id is under the same condition for a certain amount of time? Say we wanted to look at ID 1 where the value is &lt; 10 for at least seconds. So the resulting query would return:

+------------+----------+
| start_time | end_time |
+------------+----------+
|         11 |       15 |
|          3 |        9 |
+------------+----------+
A slight variation of Vérace very detailed answer. The idea is to enumerate the rows per id, and per id plus, if the condition holds. I named the difference grp. If grp changes it means that there is a change to whether the condition holds. I.e. min and max grouped by grp will apply to intervals where condition either holds or fails.

I used a having clause for the duration condition. 

select id, min(the_time), max(the_time) -- min and max per interval
from (
    select id, the_value, the_time
         -- grp is an id for each interval
         , row_number() over (partition by id order by the_time) 
         - row_number() over (partition by id
                                 , case when the_value &lt; 10 
                                        then 1
                                        else 0 
                                   end 
                              order by the_time) as grp
    from tab
) as x
where the_value &lt; 10 -- intervals where condition holds
group by id, grp
having max(the_time) - min(the_time) &gt;= 4; -- duration


The added sample data results in:

1   3   9
1   11  15


It is possible to determine the condition in one place by nesting the query, but I'm not sure whether that is useful or not:

select id, MIN(the_time) AS min, MAX(the_time) AS max -- min and max per interval
from (
    select id, the_value, the_time, condition
         -- grp is an id for each interval
         , row_number() over (partition by id order by the_time) 
         - row_number() over (partition by id, condition 
                              order by the_time) as grp
   from (
       select id, the_value, the_time, the_value &lt; 10 as condition
       from tab
   ) as a
) as x
where condition -- intervals where condition holds
group by id, grp
having max(the_time) - min(the_time) &gt;= 4 -- duration
ORDER BY id, min;