title: SQL Create many windows of data based on start/end condition in Redshift
body: <p>I am trying to filter SQL data to only select between two timestamps which are based on conditions. The idea is to identify and filter the data only to the windows of data created based on the below conditions. However, if I understand correctly, data is not processed &quot;linearly&quot; so what would be the proper approach for filtering these windows? Additionally, would there be some way to prevent windows from overlapping?</p>
  <ul>
  <li><p>The beginning timestamp is the 2 hours prior to the timestamp of a row that meets some conditions. Here is what I currently have for this:</p>
  <pre><code> WHERE timestamp &gt; DATEADD(HOUR, -2, (SELECT some_value1 
                                        FROM table1 
                                        WHERE some_value1 = cond1 
                                          AND some_value2 = cond2)
  </code></pre>
  </li>
  <li><p>The ending timestamp is when another condition is met as follows:</p>
  <pre><code>AND timestamp &lt; (SELECT some_value1 
                   FROM table1 
                   WHERE some_value1 = cond1 
                     AND (some_value2 = cond2 OR somevalue2 = cond3))
  </code></pre>
  </li>
  </ul>