title: Define Sessions not only with time limit
body: <p>I've been trying to group an events-like table (user, action, event_time) into sessions .</p>
  
  <p>But the common "idle time" approach is not enough.</p>
  
  <p>I need to check if the user was idle for more than an period X of time (ok), BUT if the user started the game, then it will very likely be idle for a long time and every action between start game and end game, regardless of the time interval, should not be considered a new session. But when the game finishes, a new session shows up:</p>
  
  <p>For example (idle time 5 min)</p>
  
  <pre>
  
  
  | action        | user | event_at     | new_session? (desired output) |
  |---------------|------|--------------|-------------------------------|
  | random1       | 1    | 1 sec        | 1                             |
  | random3       | 1    | 30 sec       | 0                             |
  | random4       | 1    | 6:00 min     | 1                             |
  | random5       | 1    | 7:00  min    | 0                             |
  | game_started  | 1    | 7:30 min     | 0                             |
  | random2       | 1    | 20:00 min    | 0                             |
  | random5       | 1    | 27:00 min    | 0                             |
  | game_finished | 1    | 35:00 min    | 0                             |
  | random5       | 1    | 35:30 min    | 1                             |
  
  
  </pre>
  
  <p>The problem are those random actions between the game_start and game_finish. I cannot tell SQL to ignore them and not count them as a new session when using the idle time logic (- that is needed for the part not between start and finish).</p>
  
  <p>in "proper" programming language I could just add a flag after the "for" or "while" finds "game started" and tell it to ignore anything until "game_finished" is found. But in SQL this is simply not that easy, even using an auxiliary column.</p>
  
  <p>Any Ideas ?
  Thanks in advance!</p>