N - 要分组的小时数
DATETIME列值(例如,2011-10-08 21:23:43)
注意:对于Mysql DB
GROUP BY date( `DATETIME` ) , N* floor( date_format( `DATETIME` , '%H' ) /N ))
这个小组是按小时公式吗?
按日/月分组的任何公式?
答案 0 :(得分:0)
SELECT MONTH(dt) as m, DAY(dt) as d, COUNT(*) as cnt
FROM (
SELECT '2011-10-11 10:00:00' as dt
UNION ALL
SELECT '2011-10-11 20:00:00' as dt
UNION ALL
SELECT '2011-10-12 10:00:00' as dt
UNION ALL
SELECT '2011-10-12 20:00:00' as dt
UNION ALL
SELECT '2011-10-13 10:00:00' as dt
UNION ALL
SELECT '2011-10-13 20:00:00' as dt
UNION ALL
SELECT '2011-11-13 10:00:00' as dt
UNION ALL
SELECT '2011-11-13 20:00:00' as dt
) as dates
GROUP BY m, d;
结果:
+------+------+-----+
| m | d | cnt |
+------+------+-----+
| 10 | 11 | 2 |
| 10 | 12 | 2 |
| 10 | 13 | 2 |
| 11 | 13 | 2 |
+------+------+-----+