I have a database with messages and a timestamp field. I want to select the count for each minute of the past 10 minutes. This is my current query:
SELECT date_format(timestamp, '%H:%i:%s'), COUNT(*) as count
FROM test
WHERE timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 10 MINUTE) AND NOW()
GROUP by
DATE_FORMAT(timestamp, '%i')
ORDER BY timestamp ASC
This is almost working as I want it to, showing me the count for the past 10 minutes.
However, since I'm grouping by the timestamp, for each minute except the very last, I'm getting the count from the first database entry for each minute, making my first and last result much less than the other ones.
Like in this image :
timestamp count
11:25:35 6
11:26:03 11
11:27:05 11
11:28:01 12
11:29:03 12
11:30:05 10
11:31:01 12
11:32:03 12
11:33:05 11
11:34:01 12
11:35:03 5
What I am looking for is a way to Group by minutes using something like NOW() instead of my timestamp, like this example.
timestamp count
11:25:35 11
11:26:35 12
11:27:35 11
11:28:35 11
11:29:35 11
11:30:35 11
11:31:35 11
11:32:35 11
11:33:35 11
11:34:35 11
Thank you in advance!