简体   繁体   中英

MYSQL: How to group the count of rows for each given time period?

i've tried to understand other similar problems but I am kind of lost. :(

So essentially I have many transaction listings that occur all through the day and each have a timestamp

TRANSACTION TIME
-----------------

2013-10-01 01:02:03         ...
2013-10-01 08:02:03         ...
2013-10-01 11:02:03         ...
2013-10-01 20:02:03         ...

I just want a table that will tell me how many transactions happened for each given hour. So...

TIME PERIOD                                               COUNT
-------------------------------------------
2013-10-01 00:00:00 -> 2013-10-01 01:00:00                 23

etc.

Any help will be greatly appreciated :)

Just format your date to drop the minutes and seconds. When you group it, it will have the same value, so they will group together.

SELECT DATE_FORMAT(yourdate, '%Y-%m-%d %H:00:00') hr, COUNT(*) GROUP BY hr

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM