I'm logging queries which have been sent to my API like this:
id | timestamp
----+---------------------
1 | 2015-01-19 18:01:47
2 | 2015-01-19 20:41:37
3 | 2015-01-20 14:15:06
4 | 2015-01-21 13:02:51
5 | 2015-01-23 05:02:12
Now, I need to know how much queries have been made in the last 60 minutes, 24 hours and 30 days and group it like that:
HOURLY | DAILY | MONTHLY
-------+-------+--------
0 | 1 | 5
Is this possible to do in one query?
Edit:
I've used the suggestions from here and played a bit with the logical things until it worked the way I want it to.
SELECT SUM(CASE WHEN DATE_SUB(NOW(), interval 1 HOUR) <= `TIMESTAMP` THEN 1 ELSE 0 END) HOURLY,
SUM(CASE WHEN DATE_SUB(NOW(), interval 1 DAY) <= `TIMESTAMP` THEN 1 ELSE 0 END) DAILY,
SUM(CASE WHEN DATE_SUB(NOW(), interval 1 MONTH) <= `TIMESTAMP` THEN 1 ELSE 0 END) MONTHLY
FROM `REQUESTS`;
Thanks a lot for your help guys!