一尘不染

DateTime按日期和小时分组

mysql

我有一个称为 activity_dt 的日期时间,数据如下所示:

2/5/2013 9:24:00 AM
2/7/2013 7:17:00 AM

如何按日期和小时分组?


阅读 903

收藏
2020-05-17

共1个答案

一尘不染

SQL Server:

SELECT [activity_dt], count(*)
FROM table1
GROUP BY DATEPART(day, [activity_dt]), DATEPART(hour, [activity_dt]);

甲骨文:

SELECT [activity_dt], count(*)
FROM table1
GROUP BY TO_CHAR(activity_dt, 'DD'), TO_CHAR(activity_dt, 'hh');

MySQL的:

SELECT [activity_dt], count(*)
FROM table1
GROUP BY hour( activity_dt ) , day( activity_dt )
2020-05-17