title: Sql group by records within certain time range starting with the value of first record in the group
body: <p>I have table (my_table) which has a column (time_field). I need to group records within a range of 5 seconds. (<a href="https://i.stack.imgur.com/4LeiU.png" rel="nofollow noreferrer">Original Image</a>):</p>
  
  <pre><code>+ ---- + ------------------- +
  | Cost | Time_field          |
  + ---- + ------------------- +
  | 0.2  | 2017-04-20 00:34:09 |
  | 0.3  | 2017-04-20 00:34:11 |
  | 0.4  | 2017-04-20 00:34:13 |
  | 0.4  | 2017-04-20 00:34:17 |
  | 0.5  | 2017-04-20 00:34:19 |
  | 0.3  | 2017-04-20 00:34:26 |
  + ---- + ------------------- +
  </code></pre>
  
  <p>I expect the following result when I do the group by records within 5 seconds range (<a href="https://i.stack.imgur.com/aRk21.png" rel="nofollow noreferrer">Original Image</a>).</p>
  
  <pre><code>+ --------- +
  | Sum(cost) |
  + --------- +
  | 0.9       | ---&gt; records from 00:34:09,11,13
  | 0.9       | ---&gt; records from 00:24:17,19
  | 0.3       | ---&gt; records from 00:24:26
  + --------- +
  </code></pre>
  
  <p>Looking for MySql solution but any generic approach is appreciated as well.</p>