title: Group two rows and all rows in between on condition
body: <p>A table defined through the following query gives an output like shown in the screenshot below:</p>
  
  <pre><code>select
  id,
  value,
  case when value = 'foo'
  and random() &lt;= 0.5
  then 't' else null
  end as to_group
  
  from
  
  (select
  generate_series(1,100) as id,
  case when random() &lt;= 0.2 then 'foo' else 'bar' end as value
  )t1
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/1mX76.png" alt="enter image description here"></p>
  
  <p>How can I group all rows tagged with <code>'foo'</code> and <code>'t'</code> with the preceding <code>'foo'</code> row (no matter if it hast <code>to_group = 't'</code> or not) and all enclosed <code>'bar'</code> rows?<br>
  In the given example those rows are 33 - 37.</p>