3

我的表 activity_count 中有以下数据

Activitydate        |count
2013-12-20:18:25:45  10
2013-12-20:18:23:40  20
2013-12-20:17:25:45   5
2013-12-20:17:25:45  10
2013-12-20:17:25:45  10

我想得到每个小时的总计数,即结果应该如下

Activitydate        |count
2013-12-20:18:00:00   30
2013-12-20:17:00:00   25
4

4 回答 4

2

如果您正在使用mySQL以下尝试:

SELECT CONCAT(LEFT(DATE(activitydate),10),' ',HOUR(activitydate),':00:00') as DateHour,
SUM(count) as TotalCount
FROM activity_count
GROUP BY DATE(activitydate),HOUR(activitydate)

见我的演示

但是,如果MSSQL尝试这个:

SELECT CAST(CAST(activitydate as DATE) AS nvarchar(15))+' '+CAST(datepart(HOUR,activitydate) as CHAR(2))+':00:00' as DateHour,
SUM([count]) as TotalCount
FROM activity_count
GROUP BY CAST(CAST(activitydate as DATE) AS nvarchar(15)),DATEPART(HOUR, activitydate)

参见 MSSQL演示

于 2013-10-21T14:00:44.670 回答
1

请试试这个

  select cast(cast(Activitydate as date) as datetime)+cast(datepart(hour,Activitydate),
  count(*)
   from activity_count 
  group by cast(cast(Activitydate as date) as datetime)+cast(datepart(hour,Activitydate)
于 2013-10-21T13:58:02.590 回答
0

试用HOUR功能

 select 
Activitydate,        
count(*) as 'count'
from activity_count
GROUP BY HOUR(Activitydate)
于 2013-10-21T13:44:41.250 回答
0

小时(时间)

 HOUR(time)

返回时间的小时。对于时间值,返回值的范围是 0 到 23。但是,TIME 值的范围实际上要大得多,因此 HOUR 可以返回大于 23 的值。

于 2013-10-21T13:45:52.930 回答