title: MySQL Query GROUP BY and COUNT based on input interval + taking in account two table colums as interval
body: <p>I'm writing a query that will fetch results from a logging table, resulting in a overview of how many visitors that were logged on my webpage at a specified interval, taking in account their actual login time (login_time, logout_time) and the input GROUP by parameter. (DAY(),HOUR() or MINUTE())</p>
  
  <p>I've twisted my brain around this for a few days on and off. Really need some positive input from anyone interested.</p>
  
  <p><strong>Input</strong></p>
  
  <p>So basicly what I would input in my query would be what time interval I would like to search by. Specifying how the result would be grouped like. (DAY(),HOUR() or MINUTE())</p>
  
  <p><strong>Example data</strong></p>
  
  <pre><code>ID| userID | login_time          | logout_time
  ------------------------------------------------------
  1 | 1      | 06.11.2010 16:57:16 | 06.11.2010 16:34:11
  2 | 2      | 06.11.2010 16:47:11 | 06.11.2010 19:55:15
  3 | 3      | 06.11.2010 16:33:16 | 06.11.2010 16:53:33
  4 | 4      | 06.11.2010 16:13:25 | 06.11.2010 18:54:54
  5 | 5      | 06.11.2010 16:02:16 | 06.11.2010 16:34:11
  6 | 6      | 06.11.2010 16:00:11 | 06.11.2010 17:55:19
  7 | 6      | 06.11.2010 19:00:11 | 06.11.2010 22:55:19
  8 | 6      | 06.11.2010 20:00:11 | 06.11.2010 23:55:19
  9 | 6      | 06.11.2010 20:00:11 | 06.11.2010 21:55:19
  9 | 6      | 06.11.2010 09:00:11 | 06.11.2010 10:00:19
  </code></pre>
  
  <p><strong>Prefered result</strong></p>
  
  <p>Example Input:  Between 06.11.2010 <strong>16:30:00</strong> and 06.11.2010 <strong>16:35:00</strong> and grouped by MINUTE()</p>
  
  <pre><code>Count | Date
  ---------------------------
  5     | 06.11.2010 16:30:00
  5     | 06.11.2010 16:31:00
  5     | 06.11.2010 16:32:00
  4     | 06.11.2010 16:33:00
  5     | 06.11.2010 16:34:00
  3     | 06.11.2010 16:35:00
  </code></pre>
  
  <p>Example Input:  Between 06.11.2010 <strong>00:30:00</strong> and 06.11.2010 <strong>23:35:00</strong> and grouped by HOUR()</p>
  
  <pre><code>Count | Date
  ---------------------------
  1     | 06.11.2010 09:00:00
  1     | 06.11.2010 10:00:00
  </code></pre>
  
  <p>--> PS: I don't mind having a gap here if there is intervals missing (ie. no data)</p>
  
  <pre><code>6     | 06.11.2010 16:00:00
  3     | 06.11.2010 17:00:00
  2     | 06.11.2010 18:00:00
  2     | 06.11.2010 19:00:00
  3     | 06.11.2010 20:00:00
  3     | 06.11.2010 21:00:00
  2     | 06.11.2010 22:00:00
  1     | 06.11.2010 23:00:00
  </code></pre>
  
  <p>Maybe you could illustrate a suitable and effective query that will yield the expected results I have in mind? I'll leave my cluttering code out from this post for the moment.</p>