Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You just need to figure out something unique to group by that will give you those 5 minute intervals.</p> <p>If you take the minute in the time, and divide that by 5, then you have something unique. For instance, you can GROUP BY CAST(EXTRACT(MINUTE FROM logTime) / 5 AS unsigned) and that would give you your 5 minute intervals unique. The /5 will divide without rounding by default.</p> <p>To actually write your working query, try it in stages.</p> <p>First, let's get just the minute:</p> <pre><code>mysql&gt; SELECT userid, EXTRACT(MINUTE FROM loggedtime) as minute from test; +--------+--------+ | userid | minute | +--------+--------+ | 1 | 45 | | 2 | 4 | | 2 | 5 | | 4 | 20 | | 3 | 20 | | 6 | 30 | | 7 | 35 | +--------+--------+ 7 rows in set (0.00 sec) </code></pre> <p>Now, let's try making a column value that's unique for each 5 minute interval you can group by. That's the minute divided by 5 with no rounding (the above value / 5):</p> <pre><code>mysql&gt; select userid, EXTRACT(MINUTE FROM loggedtime) AS minute, CAST(EXTRACT(MINUTE FROM loggedtime) / 5 AS unsigned) FROM test; +--------+--------+-------------------------------------------------------+ | userid | minute | CAST(EXTRACT(MINUTE FROM loggedtime) / 5 AS unsigned) | +--------+--------+-------------------------------------------------------+ | 1 | 45 | 9 | | 2 | 4 | 1 | | 2 | 5 | 1 | | 4 | 20 | 4 | | 3 | 20 | 4 | | 6 | 30 | 6 | | 7 | 35 | 7 | +--------+--------+-------------------------------------------------------+ 7 rows in set (0.01 sec) </code></pre> <p>Finally, we group by that unique column. This query uses MIN() and MAX() to show first and last timestamp in that interval, but you could also calculate the cutoff time if you wanted it exactly like your question.</p> <pre><code>mysql&gt; SELECT COUNT(*) AS user_count, MIN(loggedtime) AS first_time, MAX(loggedtime) AS last_time FROM test GROUP BY CAST(EXTRACT(MINUTE FROM loggedtime) / 5 AS unsigned) ORDER BY last_time; +------------+---------------------+---------------------+ | user_count | first_time | last_time | +------------+---------------------+---------------------+ | 2 | 2012-03-05 10:04:19 | 2012-03-05 10:05:09 | | 2 | 2012-03-05 10:20:20 | 2012-03-05 10:20:24 | | 1 | 2012-03-05 10:30:59 | 2012-03-05 10:30:59 | | 1 | 2012-03-05 10:35:59 | 2012-03-05 10:35:59 | | 1 | 2012-03-05 10:45:59 | 2012-03-05 10:45:59 | +------------+---------------------+---------------------+ 5 rows in set (0.00 sec) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload